catalog.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815
  1. /**
  2. * catalog.c: set of generic Catalog related routines
  3. *
  4. * Reference: SGML Open Technical Resolution TR9401:1997.
  5. * http://www.jclark.com/sp/catalog.htm
  6. *
  7. * XML Catalogs Working Draft 06 August 2001
  8. * http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
  9. *
  10. * See Copyright for the status of this software.
  11. *
  12. * Daniel.Veillard@imag.fr
  13. */
  14. #define IN_LIBXML
  15. #include "libxml.h"
  16. #ifdef LIBXML_CATALOG_ENABLED
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #ifdef HAVE_SYS_STAT_H
  20. #include <sys/stat.h>
  21. #endif
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #elif defined (_WIN32)
  25. #include <io.h>
  26. #endif
  27. #ifdef HAVE_FCNTL_H
  28. #include <fcntl.h>
  29. #endif
  30. #include <libxml/xmlmemory.h>
  31. #include <libxml/hash.h>
  32. #include <libxml/uri.h>
  33. #include <libxml/parserInternals.h>
  34. #include <libxml/catalog.h>
  35. #include <libxml/xmlerror.h>
  36. #include <libxml/threads.h>
  37. #include "private/buf.h"
  38. #include "private/error.h"
  39. #define MAX_DELEGATE 50
  40. #define MAX_CATAL_DEPTH 50
  41. #ifdef _WIN32
  42. # define PATH_SEPARATOR ';'
  43. #else
  44. # define PATH_SEPARATOR ':'
  45. #endif
  46. /**
  47. * TODO:
  48. *
  49. * macro to flag unimplemented blocks
  50. * XML_CATALOG_PREFER user env to select between system/public preferred
  51. * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
  52. *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
  53. *> values "system" and "public". I have made the default be "system" to
  54. *> match yours.
  55. */
  56. #define TODO \
  57. xmlGenericError(xmlGenericErrorContext, \
  58. "Unimplemented block at %s:%d\n", \
  59. __FILE__, __LINE__);
  60. #define XML_URN_PUBID "urn:publicid:"
  61. #define XML_CATAL_BREAK ((xmlChar *) -1)
  62. #ifndef XML_XML_DEFAULT_CATALOG
  63. #define XML_XML_DEFAULT_CATALOG "file://" SYSCONFDIR "/xml/catalog"
  64. #endif
  65. #ifndef XML_SGML_DEFAULT_CATALOG
  66. #define XML_SGML_DEFAULT_CATALOG "file://" SYSCONFDIR "/sgml/catalog"
  67. #endif
  68. #if defined(_WIN32) && defined(_MSC_VER)
  69. #undef XML_XML_DEFAULT_CATALOG
  70. static char XML_XML_DEFAULT_CATALOG[256] = "file://" SYSCONFDIR "/xml/catalog";
  71. #if !defined(_WINDOWS_)
  72. void* __stdcall GetModuleHandleA(const char*);
  73. unsigned long __stdcall GetModuleFileNameA(void*, char*, unsigned long);
  74. #endif
  75. #endif
  76. static xmlChar *xmlCatalogNormalizePublic(const xmlChar *pubID);
  77. static int xmlExpandCatalog(xmlCatalogPtr catal, const char *filename);
  78. /************************************************************************
  79. * *
  80. * Types, all private *
  81. * *
  82. ************************************************************************/
  83. typedef enum {
  84. XML_CATA_REMOVED = -1,
  85. XML_CATA_NONE = 0,
  86. XML_CATA_CATALOG,
  87. XML_CATA_BROKEN_CATALOG,
  88. XML_CATA_NEXT_CATALOG,
  89. XML_CATA_GROUP,
  90. XML_CATA_PUBLIC,
  91. XML_CATA_SYSTEM,
  92. XML_CATA_REWRITE_SYSTEM,
  93. XML_CATA_DELEGATE_PUBLIC,
  94. XML_CATA_DELEGATE_SYSTEM,
  95. XML_CATA_URI,
  96. XML_CATA_REWRITE_URI,
  97. XML_CATA_DELEGATE_URI,
  98. SGML_CATA_SYSTEM,
  99. SGML_CATA_PUBLIC,
  100. SGML_CATA_ENTITY,
  101. SGML_CATA_PENTITY,
  102. SGML_CATA_DOCTYPE,
  103. SGML_CATA_LINKTYPE,
  104. SGML_CATA_NOTATION,
  105. SGML_CATA_DELEGATE,
  106. SGML_CATA_BASE,
  107. SGML_CATA_CATALOG,
  108. SGML_CATA_DOCUMENT,
  109. SGML_CATA_SGMLDECL
  110. } xmlCatalogEntryType;
  111. typedef struct _xmlCatalogEntry xmlCatalogEntry;
  112. typedef xmlCatalogEntry *xmlCatalogEntryPtr;
  113. struct _xmlCatalogEntry {
  114. struct _xmlCatalogEntry *next;
  115. struct _xmlCatalogEntry *parent;
  116. struct _xmlCatalogEntry *children;
  117. xmlCatalogEntryType type;
  118. xmlChar *name;
  119. xmlChar *value;
  120. xmlChar *URL; /* The expanded URL using the base */
  121. xmlCatalogPrefer prefer;
  122. int dealloc;
  123. int depth;
  124. struct _xmlCatalogEntry *group;
  125. };
  126. typedef enum {
  127. XML_XML_CATALOG_TYPE = 1,
  128. XML_SGML_CATALOG_TYPE
  129. } xmlCatalogType;
  130. #define XML_MAX_SGML_CATA_DEPTH 10
  131. struct _xmlCatalog {
  132. xmlCatalogType type; /* either XML or SGML */
  133. /*
  134. * SGML Catalogs are stored as a simple hash table of catalog entries
  135. * Catalog stack to check against overflows when building the
  136. * SGML catalog
  137. */
  138. char *catalTab[XML_MAX_SGML_CATA_DEPTH]; /* stack of catals */
  139. int catalNr; /* Number of current catal streams */
  140. int catalMax; /* Max number of catal streams */
  141. xmlHashTablePtr sgml;
  142. /*
  143. * XML Catalogs are stored as a tree of Catalog entries
  144. */
  145. xmlCatalogPrefer prefer;
  146. xmlCatalogEntryPtr xml;
  147. };
  148. /************************************************************************
  149. * *
  150. * Global variables *
  151. * *
  152. ************************************************************************/
  153. /*
  154. * Those are preferences
  155. */
  156. static int xmlDebugCatalogs = 0; /* used for debugging */
  157. static xmlCatalogAllow xmlCatalogDefaultAllow = XML_CATA_ALLOW_ALL;
  158. static xmlCatalogPrefer xmlCatalogDefaultPrefer = XML_CATA_PREFER_PUBLIC;
  159. /*
  160. * Hash table containing all the trees of XML catalogs parsed by
  161. * the application.
  162. */
  163. static xmlHashTablePtr xmlCatalogXMLFiles = NULL;
  164. /*
  165. * The default catalog in use by the application
  166. */
  167. static xmlCatalogPtr xmlDefaultCatalog = NULL;
  168. /*
  169. * A mutex for modifying the shared global catalog(s)
  170. * xmlDefaultCatalog tree.
  171. * It also protects xmlCatalogXMLFiles
  172. * The core of this readers/writer scheme is in xmlFetchXMLCatalogFile()
  173. */
  174. static xmlRMutexPtr xmlCatalogMutex = NULL;
  175. /*
  176. * Whether the catalog support was initialized.
  177. */
  178. static int xmlCatalogInitialized = 0;
  179. /************************************************************************
  180. * *
  181. * Catalog error handlers *
  182. * *
  183. ************************************************************************/
  184. /**
  185. * xmlCatalogErrMemory:
  186. * @extra: extra information
  187. *
  188. * Handle an out of memory condition
  189. */
  190. static void
  191. xmlCatalogErrMemory(const char *extra)
  192. {
  193. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_CATALOG,
  194. XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
  195. extra, NULL, NULL, 0, 0,
  196. "Memory allocation failed : %s\n", extra);
  197. }
  198. /**
  199. * xmlCatalogErr:
  200. * @catal: the Catalog entry
  201. * @node: the context node
  202. * @msg: the error message
  203. * @extra: extra information
  204. *
  205. * Handle a catalog error
  206. */
  207. static void LIBXML_ATTR_FORMAT(4,0)
  208. xmlCatalogErr(xmlCatalogEntryPtr catal, xmlNodePtr node, int error,
  209. const char *msg, const xmlChar *str1, const xmlChar *str2,
  210. const xmlChar *str3)
  211. {
  212. __xmlRaiseError(NULL, NULL, NULL, catal, node, XML_FROM_CATALOG,
  213. error, XML_ERR_ERROR, NULL, 0,
  214. (const char *) str1, (const char *) str2,
  215. (const char *) str3, 0, 0,
  216. msg, str1, str2, str3);
  217. }
  218. /************************************************************************
  219. * *
  220. * Allocation and Freeing *
  221. * *
  222. ************************************************************************/
  223. /**
  224. * xmlNewCatalogEntry:
  225. * @type: type of entry
  226. * @name: name of the entry
  227. * @value: value of the entry
  228. * @prefer: the PUBLIC vs. SYSTEM current preference value
  229. * @group: for members of a group, the group entry
  230. *
  231. * create a new Catalog entry, this type is shared both by XML and
  232. * SGML catalogs, but the acceptable types values differs.
  233. *
  234. * Returns the xmlCatalogEntryPtr or NULL in case of error
  235. */
  236. static xmlCatalogEntryPtr
  237. xmlNewCatalogEntry(xmlCatalogEntryType type, const xmlChar *name,
  238. const xmlChar *value, const xmlChar *URL, xmlCatalogPrefer prefer,
  239. xmlCatalogEntryPtr group) {
  240. xmlCatalogEntryPtr ret;
  241. xmlChar *normid = NULL;
  242. ret = (xmlCatalogEntryPtr) xmlMalloc(sizeof(xmlCatalogEntry));
  243. if (ret == NULL) {
  244. xmlCatalogErrMemory("allocating catalog entry");
  245. return(NULL);
  246. }
  247. ret->next = NULL;
  248. ret->parent = NULL;
  249. ret->children = NULL;
  250. ret->type = type;
  251. if (type == XML_CATA_PUBLIC || type == XML_CATA_DELEGATE_PUBLIC) {
  252. normid = xmlCatalogNormalizePublic(name);
  253. if (normid != NULL)
  254. name = (*normid != 0 ? normid : NULL);
  255. }
  256. if (name != NULL)
  257. ret->name = xmlStrdup(name);
  258. else
  259. ret->name = NULL;
  260. if (normid != NULL)
  261. xmlFree(normid);
  262. if (value != NULL)
  263. ret->value = xmlStrdup(value);
  264. else
  265. ret->value = NULL;
  266. if (URL == NULL)
  267. URL = value;
  268. if (URL != NULL)
  269. ret->URL = xmlStrdup(URL);
  270. else
  271. ret->URL = NULL;
  272. ret->prefer = prefer;
  273. ret->dealloc = 0;
  274. ret->depth = 0;
  275. ret->group = group;
  276. return(ret);
  277. }
  278. static void
  279. xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret);
  280. /**
  281. * xmlFreeCatalogEntry:
  282. * @payload: a Catalog entry
  283. *
  284. * Free the memory allocated to a Catalog entry
  285. */
  286. static void
  287. xmlFreeCatalogEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
  288. xmlCatalogEntryPtr ret = (xmlCatalogEntryPtr) payload;
  289. if (ret == NULL)
  290. return;
  291. /*
  292. * Entries stored in the file hash must be deallocated
  293. * only by the file hash cleaner !
  294. */
  295. if (ret->dealloc == 1)
  296. return;
  297. if (xmlDebugCatalogs) {
  298. if (ret->name != NULL)
  299. xmlGenericError(xmlGenericErrorContext,
  300. "Free catalog entry %s\n", ret->name);
  301. else if (ret->value != NULL)
  302. xmlGenericError(xmlGenericErrorContext,
  303. "Free catalog entry %s\n", ret->value);
  304. else
  305. xmlGenericError(xmlGenericErrorContext,
  306. "Free catalog entry\n");
  307. }
  308. if (ret->name != NULL)
  309. xmlFree(ret->name);
  310. if (ret->value != NULL)
  311. xmlFree(ret->value);
  312. if (ret->URL != NULL)
  313. xmlFree(ret->URL);
  314. xmlFree(ret);
  315. }
  316. /**
  317. * xmlFreeCatalogEntryList:
  318. * @ret: a Catalog entry list
  319. *
  320. * Free the memory allocated to a full chained list of Catalog entries
  321. */
  322. static void
  323. xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret) {
  324. xmlCatalogEntryPtr next;
  325. while (ret != NULL) {
  326. next = ret->next;
  327. xmlFreeCatalogEntry(ret, NULL);
  328. ret = next;
  329. }
  330. }
  331. /**
  332. * xmlFreeCatalogHashEntryList:
  333. * @payload: a Catalog entry list
  334. *
  335. * Free the memory allocated to list of Catalog entries from the
  336. * catalog file hash.
  337. */
  338. static void
  339. xmlFreeCatalogHashEntryList(void *payload,
  340. const xmlChar *name ATTRIBUTE_UNUSED) {
  341. xmlCatalogEntryPtr catal = (xmlCatalogEntryPtr) payload;
  342. xmlCatalogEntryPtr children, next;
  343. if (catal == NULL)
  344. return;
  345. children = catal->children;
  346. while (children != NULL) {
  347. next = children->next;
  348. children->dealloc = 0;
  349. children->children = NULL;
  350. xmlFreeCatalogEntry(children, NULL);
  351. children = next;
  352. }
  353. catal->dealloc = 0;
  354. xmlFreeCatalogEntry(catal, NULL);
  355. }
  356. /**
  357. * xmlCreateNewCatalog:
  358. * @type: type of catalog
  359. * @prefer: the PUBLIC vs. SYSTEM current preference value
  360. *
  361. * create a new Catalog, this type is shared both by XML and
  362. * SGML catalogs, but the acceptable types values differs.
  363. *
  364. * Returns the xmlCatalogPtr or NULL in case of error
  365. */
  366. static xmlCatalogPtr
  367. xmlCreateNewCatalog(xmlCatalogType type, xmlCatalogPrefer prefer) {
  368. xmlCatalogPtr ret;
  369. ret = (xmlCatalogPtr) xmlMalloc(sizeof(xmlCatalog));
  370. if (ret == NULL) {
  371. xmlCatalogErrMemory("allocating catalog");
  372. return(NULL);
  373. }
  374. memset(ret, 0, sizeof(xmlCatalog));
  375. ret->type = type;
  376. ret->catalNr = 0;
  377. ret->catalMax = XML_MAX_SGML_CATA_DEPTH;
  378. ret->prefer = prefer;
  379. if (ret->type == XML_SGML_CATALOG_TYPE)
  380. ret->sgml = xmlHashCreate(10);
  381. return(ret);
  382. }
  383. /**
  384. * xmlFreeCatalog:
  385. * @catal: a Catalog
  386. *
  387. * Free the memory allocated to a Catalog
  388. */
  389. void
  390. xmlFreeCatalog(xmlCatalogPtr catal) {
  391. if (catal == NULL)
  392. return;
  393. if (catal->xml != NULL)
  394. xmlFreeCatalogEntryList(catal->xml);
  395. if (catal->sgml != NULL)
  396. xmlHashFree(catal->sgml, xmlFreeCatalogEntry);
  397. xmlFree(catal);
  398. }
  399. /************************************************************************
  400. * *
  401. * Serializing Catalogs *
  402. * *
  403. ************************************************************************/
  404. #ifdef LIBXML_OUTPUT_ENABLED
  405. /**
  406. * xmlCatalogDumpEntry:
  407. * @entry: the catalog entry
  408. * @out: the file.
  409. *
  410. * Serialize an SGML Catalog entry
  411. */
  412. static void
  413. xmlCatalogDumpEntry(void *payload, void *data,
  414. const xmlChar *name ATTRIBUTE_UNUSED) {
  415. xmlCatalogEntryPtr entry = (xmlCatalogEntryPtr) payload;
  416. FILE *out = (FILE *) data;
  417. if ((entry == NULL) || (out == NULL))
  418. return;
  419. switch (entry->type) {
  420. case SGML_CATA_ENTITY:
  421. fprintf(out, "ENTITY "); break;
  422. case SGML_CATA_PENTITY:
  423. fprintf(out, "ENTITY %%"); break;
  424. case SGML_CATA_DOCTYPE:
  425. fprintf(out, "DOCTYPE "); break;
  426. case SGML_CATA_LINKTYPE:
  427. fprintf(out, "LINKTYPE "); break;
  428. case SGML_CATA_NOTATION:
  429. fprintf(out, "NOTATION "); break;
  430. case SGML_CATA_PUBLIC:
  431. fprintf(out, "PUBLIC "); break;
  432. case SGML_CATA_SYSTEM:
  433. fprintf(out, "SYSTEM "); break;
  434. case SGML_CATA_DELEGATE:
  435. fprintf(out, "DELEGATE "); break;
  436. case SGML_CATA_BASE:
  437. fprintf(out, "BASE "); break;
  438. case SGML_CATA_CATALOG:
  439. fprintf(out, "CATALOG "); break;
  440. case SGML_CATA_DOCUMENT:
  441. fprintf(out, "DOCUMENT "); break;
  442. case SGML_CATA_SGMLDECL:
  443. fprintf(out, "SGMLDECL "); break;
  444. default:
  445. return;
  446. }
  447. switch (entry->type) {
  448. case SGML_CATA_ENTITY:
  449. case SGML_CATA_PENTITY:
  450. case SGML_CATA_DOCTYPE:
  451. case SGML_CATA_LINKTYPE:
  452. case SGML_CATA_NOTATION:
  453. fprintf(out, "%s", (const char *) entry->name); break;
  454. case SGML_CATA_PUBLIC:
  455. case SGML_CATA_SYSTEM:
  456. case SGML_CATA_SGMLDECL:
  457. case SGML_CATA_DOCUMENT:
  458. case SGML_CATA_CATALOG:
  459. case SGML_CATA_BASE:
  460. case SGML_CATA_DELEGATE:
  461. fprintf(out, "\"%s\"", entry->name); break;
  462. default:
  463. break;
  464. }
  465. switch (entry->type) {
  466. case SGML_CATA_ENTITY:
  467. case SGML_CATA_PENTITY:
  468. case SGML_CATA_DOCTYPE:
  469. case SGML_CATA_LINKTYPE:
  470. case SGML_CATA_NOTATION:
  471. case SGML_CATA_PUBLIC:
  472. case SGML_CATA_SYSTEM:
  473. case SGML_CATA_DELEGATE:
  474. fprintf(out, " \"%s\"", entry->value); break;
  475. default:
  476. break;
  477. }
  478. fprintf(out, "\n");
  479. }
  480. /**
  481. * xmlDumpXMLCatalogNode:
  482. * @catal: top catalog entry
  483. * @catalog: pointer to the xml tree
  484. * @doc: the containing document
  485. * @ns: the current namespace
  486. * @cgroup: group node for group members
  487. *
  488. * Serializes a Catalog entry, called by xmlDumpXMLCatalog and recursively
  489. * for group entries
  490. */
  491. static void xmlDumpXMLCatalogNode(xmlCatalogEntryPtr catal, xmlNodePtr catalog,
  492. xmlDocPtr doc, xmlNsPtr ns, xmlCatalogEntryPtr cgroup) {
  493. xmlNodePtr node;
  494. xmlCatalogEntryPtr cur;
  495. /*
  496. * add all the catalog entries
  497. */
  498. cur = catal;
  499. while (cur != NULL) {
  500. if (cur->group == cgroup) {
  501. switch (cur->type) {
  502. case XML_CATA_REMOVED:
  503. break;
  504. case XML_CATA_BROKEN_CATALOG:
  505. case XML_CATA_CATALOG:
  506. if (cur == catal) {
  507. cur = cur->children;
  508. continue;
  509. }
  510. break;
  511. case XML_CATA_NEXT_CATALOG:
  512. node = xmlNewDocNode(doc, ns, BAD_CAST "nextCatalog", NULL);
  513. xmlSetProp(node, BAD_CAST "catalog", cur->value);
  514. xmlAddChild(catalog, node);
  515. break;
  516. case XML_CATA_NONE:
  517. break;
  518. case XML_CATA_GROUP:
  519. node = xmlNewDocNode(doc, ns, BAD_CAST "group", NULL);
  520. xmlSetProp(node, BAD_CAST "id", cur->name);
  521. if (cur->value != NULL) {
  522. xmlNsPtr xns;
  523. xns = xmlSearchNsByHref(doc, node, XML_XML_NAMESPACE);
  524. if (xns != NULL)
  525. xmlSetNsProp(node, xns, BAD_CAST "base",
  526. cur->value);
  527. }
  528. switch (cur->prefer) {
  529. case XML_CATA_PREFER_NONE:
  530. break;
  531. case XML_CATA_PREFER_PUBLIC:
  532. xmlSetProp(node, BAD_CAST "prefer", BAD_CAST "public");
  533. break;
  534. case XML_CATA_PREFER_SYSTEM:
  535. xmlSetProp(node, BAD_CAST "prefer", BAD_CAST "system");
  536. break;
  537. }
  538. xmlDumpXMLCatalogNode(cur->next, node, doc, ns, cur);
  539. xmlAddChild(catalog, node);
  540. break;
  541. case XML_CATA_PUBLIC:
  542. node = xmlNewDocNode(doc, ns, BAD_CAST "public", NULL);
  543. xmlSetProp(node, BAD_CAST "publicId", cur->name);
  544. xmlSetProp(node, BAD_CAST "uri", cur->value);
  545. xmlAddChild(catalog, node);
  546. break;
  547. case XML_CATA_SYSTEM:
  548. node = xmlNewDocNode(doc, ns, BAD_CAST "system", NULL);
  549. xmlSetProp(node, BAD_CAST "systemId", cur->name);
  550. xmlSetProp(node, BAD_CAST "uri", cur->value);
  551. xmlAddChild(catalog, node);
  552. break;
  553. case XML_CATA_REWRITE_SYSTEM:
  554. node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteSystem", NULL);
  555. xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name);
  556. xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value);
  557. xmlAddChild(catalog, node);
  558. break;
  559. case XML_CATA_DELEGATE_PUBLIC:
  560. node = xmlNewDocNode(doc, ns, BAD_CAST "delegatePublic", NULL);
  561. xmlSetProp(node, BAD_CAST "publicIdStartString", cur->name);
  562. xmlSetProp(node, BAD_CAST "catalog", cur->value);
  563. xmlAddChild(catalog, node);
  564. break;
  565. case XML_CATA_DELEGATE_SYSTEM:
  566. node = xmlNewDocNode(doc, ns, BAD_CAST "delegateSystem", NULL);
  567. xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name);
  568. xmlSetProp(node, BAD_CAST "catalog", cur->value);
  569. xmlAddChild(catalog, node);
  570. break;
  571. case XML_CATA_URI:
  572. node = xmlNewDocNode(doc, ns, BAD_CAST "uri", NULL);
  573. xmlSetProp(node, BAD_CAST "name", cur->name);
  574. xmlSetProp(node, BAD_CAST "uri", cur->value);
  575. xmlAddChild(catalog, node);
  576. break;
  577. case XML_CATA_REWRITE_URI:
  578. node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteURI", NULL);
  579. xmlSetProp(node, BAD_CAST "uriStartString", cur->name);
  580. xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value);
  581. xmlAddChild(catalog, node);
  582. break;
  583. case XML_CATA_DELEGATE_URI:
  584. node = xmlNewDocNode(doc, ns, BAD_CAST "delegateURI", NULL);
  585. xmlSetProp(node, BAD_CAST "uriStartString", cur->name);
  586. xmlSetProp(node, BAD_CAST "catalog", cur->value);
  587. xmlAddChild(catalog, node);
  588. break;
  589. case SGML_CATA_SYSTEM:
  590. case SGML_CATA_PUBLIC:
  591. case SGML_CATA_ENTITY:
  592. case SGML_CATA_PENTITY:
  593. case SGML_CATA_DOCTYPE:
  594. case SGML_CATA_LINKTYPE:
  595. case SGML_CATA_NOTATION:
  596. case SGML_CATA_DELEGATE:
  597. case SGML_CATA_BASE:
  598. case SGML_CATA_CATALOG:
  599. case SGML_CATA_DOCUMENT:
  600. case SGML_CATA_SGMLDECL:
  601. break;
  602. }
  603. }
  604. cur = cur->next;
  605. }
  606. }
  607. static int
  608. xmlDumpXMLCatalog(FILE *out, xmlCatalogEntryPtr catal) {
  609. int ret;
  610. xmlDocPtr doc;
  611. xmlNsPtr ns;
  612. xmlDtdPtr dtd;
  613. xmlNodePtr catalog;
  614. xmlOutputBufferPtr buf;
  615. /*
  616. * Rebuild a catalog
  617. */
  618. doc = xmlNewDoc(NULL);
  619. if (doc == NULL)
  620. return(-1);
  621. dtd = xmlNewDtd(doc, BAD_CAST "catalog",
  622. BAD_CAST "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN",
  623. BAD_CAST "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd");
  624. xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd);
  625. ns = xmlNewNs(NULL, XML_CATALOGS_NAMESPACE, NULL);
  626. if (ns == NULL) {
  627. xmlFreeDoc(doc);
  628. return(-1);
  629. }
  630. catalog = xmlNewDocNode(doc, ns, BAD_CAST "catalog", NULL);
  631. if (catalog == NULL) {
  632. xmlFreeNs(ns);
  633. xmlFreeDoc(doc);
  634. return(-1);
  635. }
  636. catalog->nsDef = ns;
  637. xmlAddChild((xmlNodePtr) doc, catalog);
  638. xmlDumpXMLCatalogNode(catal, catalog, doc, ns, NULL);
  639. /*
  640. * reserialize it
  641. */
  642. buf = xmlOutputBufferCreateFile(out, NULL);
  643. if (buf == NULL) {
  644. xmlFreeDoc(doc);
  645. return(-1);
  646. }
  647. ret = xmlSaveFormatFileTo(buf, doc, NULL, 1);
  648. /*
  649. * Free it
  650. */
  651. xmlFreeDoc(doc);
  652. return(ret);
  653. }
  654. #endif /* LIBXML_OUTPUT_ENABLED */
  655. /************************************************************************
  656. * *
  657. * Converting SGML Catalogs to XML *
  658. * *
  659. ************************************************************************/
  660. /**
  661. * xmlCatalogConvertEntry:
  662. * @entry: the entry
  663. * @catal: pointer to the catalog being converted
  664. *
  665. * Convert one entry from the catalog
  666. */
  667. static void
  668. xmlCatalogConvertEntry(void *payload, void *data,
  669. const xmlChar *name ATTRIBUTE_UNUSED) {
  670. xmlCatalogEntryPtr entry = (xmlCatalogEntryPtr) payload;
  671. xmlCatalogPtr catal = (xmlCatalogPtr) data;
  672. if ((entry == NULL) || (catal == NULL) || (catal->sgml == NULL) ||
  673. (catal->xml == NULL))
  674. return;
  675. switch (entry->type) {
  676. case SGML_CATA_ENTITY:
  677. entry->type = XML_CATA_PUBLIC;
  678. break;
  679. case SGML_CATA_PENTITY:
  680. entry->type = XML_CATA_PUBLIC;
  681. break;
  682. case SGML_CATA_DOCTYPE:
  683. entry->type = XML_CATA_PUBLIC;
  684. break;
  685. case SGML_CATA_LINKTYPE:
  686. entry->type = XML_CATA_PUBLIC;
  687. break;
  688. case SGML_CATA_NOTATION:
  689. entry->type = XML_CATA_PUBLIC;
  690. break;
  691. case SGML_CATA_PUBLIC:
  692. entry->type = XML_CATA_PUBLIC;
  693. break;
  694. case SGML_CATA_SYSTEM:
  695. entry->type = XML_CATA_SYSTEM;
  696. break;
  697. case SGML_CATA_DELEGATE:
  698. entry->type = XML_CATA_DELEGATE_PUBLIC;
  699. break;
  700. case SGML_CATA_CATALOG:
  701. entry->type = XML_CATA_CATALOG;
  702. break;
  703. default:
  704. xmlHashRemoveEntry(catal->sgml, entry->name, xmlFreeCatalogEntry);
  705. return;
  706. }
  707. /*
  708. * Conversion successful, remove from the SGML catalog
  709. * and add it to the default XML one
  710. */
  711. xmlHashRemoveEntry(catal->sgml, entry->name, NULL);
  712. entry->parent = catal->xml;
  713. entry->next = NULL;
  714. if (catal->xml->children == NULL)
  715. catal->xml->children = entry;
  716. else {
  717. xmlCatalogEntryPtr prev;
  718. prev = catal->xml->children;
  719. while (prev->next != NULL)
  720. prev = prev->next;
  721. prev->next = entry;
  722. }
  723. }
  724. /**
  725. * xmlConvertSGMLCatalog:
  726. * @catal: the catalog
  727. *
  728. * Convert all the SGML catalog entries as XML ones
  729. *
  730. * Returns the number of entries converted if successful, -1 otherwise
  731. */
  732. int
  733. xmlConvertSGMLCatalog(xmlCatalogPtr catal) {
  734. if ((catal == NULL) || (catal->type != XML_SGML_CATALOG_TYPE))
  735. return(-1);
  736. if (xmlDebugCatalogs) {
  737. xmlGenericError(xmlGenericErrorContext,
  738. "Converting SGML catalog to XML\n");
  739. }
  740. xmlHashScan(catal->sgml, xmlCatalogConvertEntry, &catal);
  741. return(0);
  742. }
  743. /************************************************************************
  744. * *
  745. * Helper function *
  746. * *
  747. ************************************************************************/
  748. /**
  749. * xmlCatalogUnWrapURN:
  750. * @urn: an "urn:publicid:" to unwrap
  751. *
  752. * Expand the URN into the equivalent Public Identifier
  753. *
  754. * Returns the new identifier or NULL, the string must be deallocated
  755. * by the caller.
  756. */
  757. static xmlChar *
  758. xmlCatalogUnWrapURN(const xmlChar *urn) {
  759. xmlChar result[2000];
  760. unsigned int i = 0;
  761. if (xmlStrncmp(urn, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1))
  762. return(NULL);
  763. urn += sizeof(XML_URN_PUBID) - 1;
  764. while (*urn != 0) {
  765. if (i > sizeof(result) - 4)
  766. break;
  767. if (*urn == '+') {
  768. result[i++] = ' ';
  769. urn++;
  770. } else if (*urn == ':') {
  771. result[i++] = '/';
  772. result[i++] = '/';
  773. urn++;
  774. } else if (*urn == ';') {
  775. result[i++] = ':';
  776. result[i++] = ':';
  777. urn++;
  778. } else if (*urn == '%') {
  779. if ((urn[1] == '2') && (urn[2] == 'B'))
  780. result[i++] = '+';
  781. else if ((urn[1] == '3') && (urn[2] == 'A'))
  782. result[i++] = ':';
  783. else if ((urn[1] == '2') && (urn[2] == 'F'))
  784. result[i++] = '/';
  785. else if ((urn[1] == '3') && (urn[2] == 'B'))
  786. result[i++] = ';';
  787. else if ((urn[1] == '2') && (urn[2] == '7'))
  788. result[i++] = '\'';
  789. else if ((urn[1] == '3') && (urn[2] == 'F'))
  790. result[i++] = '?';
  791. else if ((urn[1] == '2') && (urn[2] == '3'))
  792. result[i++] = '#';
  793. else if ((urn[1] == '2') && (urn[2] == '5'))
  794. result[i++] = '%';
  795. else {
  796. result[i++] = *urn;
  797. urn++;
  798. continue;
  799. }
  800. urn += 3;
  801. } else {
  802. result[i++] = *urn;
  803. urn++;
  804. }
  805. }
  806. result[i] = 0;
  807. return(xmlStrdup(result));
  808. }
  809. /**
  810. * xmlParseCatalogFile:
  811. * @filename: the filename
  812. *
  813. * parse an XML file and build a tree. It's like xmlParseFile()
  814. * except it bypass all catalog lookups.
  815. *
  816. * Returns the resulting document tree or NULL in case of error
  817. */
  818. xmlDocPtr
  819. xmlParseCatalogFile(const char *filename) {
  820. xmlDocPtr ret;
  821. xmlParserCtxtPtr ctxt;
  822. char *directory = NULL;
  823. xmlParserInputPtr inputStream;
  824. xmlParserInputBufferPtr buf;
  825. ctxt = xmlNewParserCtxt();
  826. if (ctxt == NULL) {
  827. xmlCatalogErrMemory("allocating parser context");
  828. return(NULL);
  829. }
  830. buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
  831. if (buf == NULL) {
  832. xmlFreeParserCtxt(ctxt);
  833. return(NULL);
  834. }
  835. inputStream = xmlNewInputStream(ctxt);
  836. if (inputStream == NULL) {
  837. xmlFreeParserInputBuffer(buf);
  838. xmlFreeParserCtxt(ctxt);
  839. return(NULL);
  840. }
  841. inputStream->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
  842. inputStream->buf = buf;
  843. xmlBufResetInput(buf->buffer, inputStream);
  844. inputPush(ctxt, inputStream);
  845. if (ctxt->directory == NULL)
  846. directory = xmlParserGetDirectory(filename);
  847. if ((ctxt->directory == NULL) && (directory != NULL))
  848. ctxt->directory = directory;
  849. ctxt->valid = 0;
  850. ctxt->validate = 0;
  851. ctxt->loadsubset = 0;
  852. ctxt->pedantic = 0;
  853. ctxt->dictNames = 1;
  854. xmlParseDocument(ctxt);
  855. if (ctxt->wellFormed)
  856. ret = ctxt->myDoc;
  857. else {
  858. ret = NULL;
  859. xmlFreeDoc(ctxt->myDoc);
  860. ctxt->myDoc = NULL;
  861. }
  862. xmlFreeParserCtxt(ctxt);
  863. return(ret);
  864. }
  865. /**
  866. * xmlLoadFileContent:
  867. * @filename: a file path
  868. *
  869. * Load a file content into memory.
  870. *
  871. * Returns a pointer to the 0 terminated string or NULL in case of error
  872. */
  873. static xmlChar *
  874. xmlLoadFileContent(const char *filename)
  875. {
  876. #ifdef HAVE_STAT
  877. int fd;
  878. #else
  879. FILE *fd;
  880. #endif
  881. int len;
  882. long size;
  883. #ifdef HAVE_STAT
  884. struct stat info;
  885. #endif
  886. xmlChar *content;
  887. if (filename == NULL)
  888. return (NULL);
  889. #ifdef HAVE_STAT
  890. if (stat(filename, &info) < 0)
  891. return (NULL);
  892. #endif
  893. #ifdef HAVE_STAT
  894. if ((fd = open(filename, O_RDONLY)) < 0)
  895. #else
  896. if ((fd = fopen(filename, "rb")) == NULL)
  897. #endif
  898. {
  899. return (NULL);
  900. }
  901. #ifdef HAVE_STAT
  902. size = info.st_size;
  903. #else
  904. if (fseek(fd, 0, SEEK_END) || (size = ftell(fd)) == EOF || fseek(fd, 0, SEEK_SET)) { /* File operations denied? ok, just close and return failure */
  905. fclose(fd);
  906. return (NULL);
  907. }
  908. #endif
  909. content = (xmlChar*)xmlMallocAtomic(size + 10);
  910. if (content == NULL) {
  911. xmlCatalogErrMemory("allocating catalog data");
  912. #ifdef HAVE_STAT
  913. close(fd);
  914. #else
  915. fclose(fd);
  916. #endif
  917. return (NULL);
  918. }
  919. #ifdef HAVE_STAT
  920. len = read(fd, content, size);
  921. close(fd);
  922. #else
  923. len = fread(content, 1, size, fd);
  924. fclose(fd);
  925. #endif
  926. if (len < 0) {
  927. xmlFree(content);
  928. return (NULL);
  929. }
  930. content[len] = 0;
  931. return(content);
  932. }
  933. /**
  934. * xmlCatalogNormalizePublic:
  935. * @pubID: the public ID string
  936. *
  937. * Normalizes the Public Identifier
  938. *
  939. * Implements 6.2. Public Identifier Normalization
  940. * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
  941. *
  942. * Returns the new string or NULL, the string must be deallocated
  943. * by the caller.
  944. */
  945. static xmlChar *
  946. xmlCatalogNormalizePublic(const xmlChar *pubID)
  947. {
  948. int ok = 1;
  949. int white;
  950. const xmlChar *p;
  951. xmlChar *ret;
  952. xmlChar *q;
  953. if (pubID == NULL)
  954. return(NULL);
  955. white = 1;
  956. for (p = pubID;*p != 0 && ok;p++) {
  957. if (!xmlIsBlank_ch(*p))
  958. white = 0;
  959. else if (*p == 0x20 && !white)
  960. white = 1;
  961. else
  962. ok = 0;
  963. }
  964. if (ok && !white) /* is normalized */
  965. return(NULL);
  966. ret = xmlStrdup(pubID);
  967. q = ret;
  968. white = 0;
  969. for (p = pubID;*p != 0;p++) {
  970. if (xmlIsBlank_ch(*p)) {
  971. if (q != ret)
  972. white = 1;
  973. } else {
  974. if (white) {
  975. *(q++) = 0x20;
  976. white = 0;
  977. }
  978. *(q++) = *p;
  979. }
  980. }
  981. *q = 0;
  982. return(ret);
  983. }
  984. /************************************************************************
  985. * *
  986. * The XML Catalog parser *
  987. * *
  988. ************************************************************************/
  989. static xmlCatalogEntryPtr
  990. xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename);
  991. static void
  992. xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer,
  993. xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup);
  994. static xmlChar *
  995. xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
  996. const xmlChar *sysID);
  997. static xmlChar *
  998. xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI);
  999. /**
  1000. * xmlGetXMLCatalogEntryType:
  1001. * @name: the name
  1002. *
  1003. * lookup the internal type associated to an XML catalog entry name
  1004. *
  1005. * Returns the type associated with that name
  1006. */
  1007. static xmlCatalogEntryType
  1008. xmlGetXMLCatalogEntryType(const xmlChar *name) {
  1009. xmlCatalogEntryType type = XML_CATA_NONE;
  1010. if (xmlStrEqual(name, (const xmlChar *) "system"))
  1011. type = XML_CATA_SYSTEM;
  1012. else if (xmlStrEqual(name, (const xmlChar *) "public"))
  1013. type = XML_CATA_PUBLIC;
  1014. else if (xmlStrEqual(name, (const xmlChar *) "rewriteSystem"))
  1015. type = XML_CATA_REWRITE_SYSTEM;
  1016. else if (xmlStrEqual(name, (const xmlChar *) "delegatePublic"))
  1017. type = XML_CATA_DELEGATE_PUBLIC;
  1018. else if (xmlStrEqual(name, (const xmlChar *) "delegateSystem"))
  1019. type = XML_CATA_DELEGATE_SYSTEM;
  1020. else if (xmlStrEqual(name, (const xmlChar *) "uri"))
  1021. type = XML_CATA_URI;
  1022. else if (xmlStrEqual(name, (const xmlChar *) "rewriteURI"))
  1023. type = XML_CATA_REWRITE_URI;
  1024. else if (xmlStrEqual(name, (const xmlChar *) "delegateURI"))
  1025. type = XML_CATA_DELEGATE_URI;
  1026. else if (xmlStrEqual(name, (const xmlChar *) "nextCatalog"))
  1027. type = XML_CATA_NEXT_CATALOG;
  1028. else if (xmlStrEqual(name, (const xmlChar *) "catalog"))
  1029. type = XML_CATA_CATALOG;
  1030. return(type);
  1031. }
  1032. /**
  1033. * xmlParseXMLCatalogOneNode:
  1034. * @cur: the XML node
  1035. * @type: the type of Catalog entry
  1036. * @name: the name of the node
  1037. * @attrName: the attribute holding the value
  1038. * @uriAttrName: the attribute holding the URI-Reference
  1039. * @prefer: the PUBLIC vs. SYSTEM current preference value
  1040. * @cgroup: the group which includes this node
  1041. *
  1042. * Finishes the examination of an XML tree node of a catalog and build
  1043. * a Catalog entry from it.
  1044. *
  1045. * Returns the new Catalog entry node or NULL in case of error.
  1046. */
  1047. static xmlCatalogEntryPtr
  1048. xmlParseXMLCatalogOneNode(xmlNodePtr cur, xmlCatalogEntryType type,
  1049. const xmlChar *name, const xmlChar *attrName,
  1050. const xmlChar *uriAttrName, xmlCatalogPrefer prefer,
  1051. xmlCatalogEntryPtr cgroup) {
  1052. int ok = 1;
  1053. xmlChar *uriValue;
  1054. xmlChar *nameValue = NULL;
  1055. xmlChar *base = NULL;
  1056. xmlChar *URL = NULL;
  1057. xmlCatalogEntryPtr ret = NULL;
  1058. if (attrName != NULL) {
  1059. nameValue = xmlGetProp(cur, attrName);
  1060. if (nameValue == NULL) {
  1061. xmlCatalogErr(ret, cur, XML_CATALOG_MISSING_ATTR,
  1062. "%s entry lacks '%s'\n", name, attrName, NULL);
  1063. ok = 0;
  1064. }
  1065. }
  1066. uriValue = xmlGetProp(cur, uriAttrName);
  1067. if (uriValue == NULL) {
  1068. xmlCatalogErr(ret, cur, XML_CATALOG_MISSING_ATTR,
  1069. "%s entry lacks '%s'\n", name, uriAttrName, NULL);
  1070. ok = 0;
  1071. }
  1072. if (!ok) {
  1073. if (nameValue != NULL)
  1074. xmlFree(nameValue);
  1075. if (uriValue != NULL)
  1076. xmlFree(uriValue);
  1077. return(NULL);
  1078. }
  1079. base = xmlNodeGetBase(cur->doc, cur);
  1080. URL = xmlBuildURI(uriValue, base);
  1081. if (URL != NULL) {
  1082. if (xmlDebugCatalogs > 1) {
  1083. if (nameValue != NULL)
  1084. xmlGenericError(xmlGenericErrorContext,
  1085. "Found %s: '%s' '%s'\n", name, nameValue, URL);
  1086. else
  1087. xmlGenericError(xmlGenericErrorContext,
  1088. "Found %s: '%s'\n", name, URL);
  1089. }
  1090. ret = xmlNewCatalogEntry(type, nameValue, uriValue, URL, prefer, cgroup);
  1091. } else {
  1092. xmlCatalogErr(ret, cur, XML_CATALOG_ENTRY_BROKEN,
  1093. "%s entry '%s' broken ?: %s\n", name, uriAttrName, uriValue);
  1094. }
  1095. if (nameValue != NULL)
  1096. xmlFree(nameValue);
  1097. if (uriValue != NULL)
  1098. xmlFree(uriValue);
  1099. if (base != NULL)
  1100. xmlFree(base);
  1101. if (URL != NULL)
  1102. xmlFree(URL);
  1103. return(ret);
  1104. }
  1105. /**
  1106. * xmlParseXMLCatalogNode:
  1107. * @cur: the XML node
  1108. * @prefer: the PUBLIC vs. SYSTEM current preference value
  1109. * @parent: the parent Catalog entry
  1110. * @cgroup: the group which includes this node
  1111. *
  1112. * Examines an XML tree node of a catalog and build
  1113. * a Catalog entry from it adding it to its parent. The examination can
  1114. * be recursive.
  1115. */
  1116. static void
  1117. xmlParseXMLCatalogNode(xmlNodePtr cur, xmlCatalogPrefer prefer,
  1118. xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup)
  1119. {
  1120. xmlChar *base = NULL;
  1121. xmlCatalogEntryPtr entry = NULL;
  1122. if (cur == NULL)
  1123. return;
  1124. if (xmlStrEqual(cur->name, BAD_CAST "group")) {
  1125. xmlChar *prop;
  1126. xmlCatalogPrefer pref = XML_CATA_PREFER_NONE;
  1127. prop = xmlGetProp(cur, BAD_CAST "prefer");
  1128. if (prop != NULL) {
  1129. if (xmlStrEqual(prop, BAD_CAST "system")) {
  1130. prefer = XML_CATA_PREFER_SYSTEM;
  1131. } else if (xmlStrEqual(prop, BAD_CAST "public")) {
  1132. prefer = XML_CATA_PREFER_PUBLIC;
  1133. } else {
  1134. xmlCatalogErr(parent, cur, XML_CATALOG_PREFER_VALUE,
  1135. "Invalid value for prefer: '%s'\n",
  1136. prop, NULL, NULL);
  1137. }
  1138. xmlFree(prop);
  1139. pref = prefer;
  1140. }
  1141. prop = xmlGetProp(cur, BAD_CAST "id");
  1142. base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
  1143. entry = xmlNewCatalogEntry(XML_CATA_GROUP, prop, base, NULL, pref, cgroup);
  1144. xmlFree(prop);
  1145. } else if (xmlStrEqual(cur->name, BAD_CAST "public")) {
  1146. entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_PUBLIC,
  1147. BAD_CAST "public", BAD_CAST "publicId", BAD_CAST "uri", prefer, cgroup);
  1148. } else if (xmlStrEqual(cur->name, BAD_CAST "system")) {
  1149. entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_SYSTEM,
  1150. BAD_CAST "system", BAD_CAST "systemId", BAD_CAST "uri", prefer, cgroup);
  1151. } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteSystem")) {
  1152. entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_SYSTEM,
  1153. BAD_CAST "rewriteSystem", BAD_CAST "systemIdStartString",
  1154. BAD_CAST "rewritePrefix", prefer, cgroup);
  1155. } else if (xmlStrEqual(cur->name, BAD_CAST "delegatePublic")) {
  1156. entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_PUBLIC,
  1157. BAD_CAST "delegatePublic", BAD_CAST "publicIdStartString",
  1158. BAD_CAST "catalog", prefer, cgroup);
  1159. } else if (xmlStrEqual(cur->name, BAD_CAST "delegateSystem")) {
  1160. entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_SYSTEM,
  1161. BAD_CAST "delegateSystem", BAD_CAST "systemIdStartString",
  1162. BAD_CAST "catalog", prefer, cgroup);
  1163. } else if (xmlStrEqual(cur->name, BAD_CAST "uri")) {
  1164. entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_URI,
  1165. BAD_CAST "uri", BAD_CAST "name",
  1166. BAD_CAST "uri", prefer, cgroup);
  1167. } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteURI")) {
  1168. entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_URI,
  1169. BAD_CAST "rewriteURI", BAD_CAST "uriStartString",
  1170. BAD_CAST "rewritePrefix", prefer, cgroup);
  1171. } else if (xmlStrEqual(cur->name, BAD_CAST "delegateURI")) {
  1172. entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_URI,
  1173. BAD_CAST "delegateURI", BAD_CAST "uriStartString",
  1174. BAD_CAST "catalog", prefer, cgroup);
  1175. } else if (xmlStrEqual(cur->name, BAD_CAST "nextCatalog")) {
  1176. entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_NEXT_CATALOG,
  1177. BAD_CAST "nextCatalog", NULL,
  1178. BAD_CAST "catalog", prefer, cgroup);
  1179. }
  1180. if (entry != NULL) {
  1181. if (parent != NULL) {
  1182. entry->parent = parent;
  1183. if (parent->children == NULL)
  1184. parent->children = entry;
  1185. else {
  1186. xmlCatalogEntryPtr prev;
  1187. prev = parent->children;
  1188. while (prev->next != NULL)
  1189. prev = prev->next;
  1190. prev->next = entry;
  1191. }
  1192. }
  1193. if (entry->type == XML_CATA_GROUP) {
  1194. /*
  1195. * Recurse to propagate prefer to the subtree
  1196. * (xml:base handling is automated)
  1197. */
  1198. xmlParseXMLCatalogNodeList(cur->children, prefer, parent, entry);
  1199. }
  1200. }
  1201. if (base != NULL)
  1202. xmlFree(base);
  1203. }
  1204. /**
  1205. * xmlParseXMLCatalogNodeList:
  1206. * @cur: the XML node list of siblings
  1207. * @prefer: the PUBLIC vs. SYSTEM current preference value
  1208. * @parent: the parent Catalog entry
  1209. * @cgroup: the group which includes this list
  1210. *
  1211. * Examines a list of XML sibling nodes of a catalog and build
  1212. * a list of Catalog entry from it adding it to the parent.
  1213. * The examination will recurse to examine node subtrees.
  1214. */
  1215. static void
  1216. xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer,
  1217. xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup) {
  1218. while (cur != NULL) {
  1219. if ((cur->ns != NULL) && (cur->ns->href != NULL) &&
  1220. (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) {
  1221. xmlParseXMLCatalogNode(cur, prefer, parent, cgroup);
  1222. }
  1223. cur = cur->next;
  1224. }
  1225. /* TODO: sort the list according to REWRITE lengths and prefer value */
  1226. }
  1227. /**
  1228. * xmlParseXMLCatalogFile:
  1229. * @prefer: the PUBLIC vs. SYSTEM current preference value
  1230. * @filename: the filename for the catalog
  1231. *
  1232. * Parses the catalog file to extract the XML tree and then analyze the
  1233. * tree to build a list of Catalog entries corresponding to this catalog
  1234. *
  1235. * Returns the resulting Catalog entries list
  1236. */
  1237. static xmlCatalogEntryPtr
  1238. xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename) {
  1239. xmlDocPtr doc;
  1240. xmlNodePtr cur;
  1241. xmlChar *prop;
  1242. xmlCatalogEntryPtr parent = NULL;
  1243. if (filename == NULL)
  1244. return(NULL);
  1245. doc = xmlParseCatalogFile((const char *) filename);
  1246. if (doc == NULL) {
  1247. if (xmlDebugCatalogs)
  1248. xmlGenericError(xmlGenericErrorContext,
  1249. "Failed to parse catalog %s\n", filename);
  1250. return(NULL);
  1251. }
  1252. if (xmlDebugCatalogs)
  1253. xmlGenericError(xmlGenericErrorContext,
  1254. "%d Parsing catalog %s\n", xmlGetThreadId(), filename);
  1255. cur = xmlDocGetRootElement(doc);
  1256. if ((cur != NULL) && (xmlStrEqual(cur->name, BAD_CAST "catalog")) &&
  1257. (cur->ns != NULL) && (cur->ns->href != NULL) &&
  1258. (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) {
  1259. parent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
  1260. (const xmlChar *)filename, NULL, prefer, NULL);
  1261. if (parent == NULL) {
  1262. xmlFreeDoc(doc);
  1263. return(NULL);
  1264. }
  1265. prop = xmlGetProp(cur, BAD_CAST "prefer");
  1266. if (prop != NULL) {
  1267. if (xmlStrEqual(prop, BAD_CAST "system")) {
  1268. prefer = XML_CATA_PREFER_SYSTEM;
  1269. } else if (xmlStrEqual(prop, BAD_CAST "public")) {
  1270. prefer = XML_CATA_PREFER_PUBLIC;
  1271. } else {
  1272. xmlCatalogErr(NULL, cur, XML_CATALOG_PREFER_VALUE,
  1273. "Invalid value for prefer: '%s'\n",
  1274. prop, NULL, NULL);
  1275. }
  1276. xmlFree(prop);
  1277. }
  1278. cur = cur->children;
  1279. xmlParseXMLCatalogNodeList(cur, prefer, parent, NULL);
  1280. } else {
  1281. xmlCatalogErr(NULL, (xmlNodePtr) doc, XML_CATALOG_NOT_CATALOG,
  1282. "File %s is not an XML Catalog\n",
  1283. filename, NULL, NULL);
  1284. xmlFreeDoc(doc);
  1285. return(NULL);
  1286. }
  1287. xmlFreeDoc(doc);
  1288. return(parent);
  1289. }
  1290. /**
  1291. * xmlFetchXMLCatalogFile:
  1292. * @catal: an existing but incomplete catalog entry
  1293. *
  1294. * Fetch and parse the subcatalog referenced by an entry
  1295. *
  1296. * Returns 0 in case of success, -1 otherwise
  1297. */
  1298. static int
  1299. xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal) {
  1300. xmlCatalogEntryPtr doc;
  1301. if (catal == NULL)
  1302. return(-1);
  1303. if (catal->URL == NULL)
  1304. return(-1);
  1305. /*
  1306. * lock the whole catalog for modification
  1307. */
  1308. xmlRMutexLock(xmlCatalogMutex);
  1309. if (catal->children != NULL) {
  1310. /* Okay someone else did it in the meantime */
  1311. xmlRMutexUnlock(xmlCatalogMutex);
  1312. return(0);
  1313. }
  1314. if (xmlCatalogXMLFiles != NULL) {
  1315. doc = (xmlCatalogEntryPtr)
  1316. xmlHashLookup(xmlCatalogXMLFiles, catal->URL);
  1317. if (doc != NULL) {
  1318. if (xmlDebugCatalogs)
  1319. xmlGenericError(xmlGenericErrorContext,
  1320. "Found %s in file hash\n", catal->URL);
  1321. if (catal->type == XML_CATA_CATALOG)
  1322. catal->children = doc->children;
  1323. else
  1324. catal->children = doc;
  1325. catal->dealloc = 0;
  1326. xmlRMutexUnlock(xmlCatalogMutex);
  1327. return(0);
  1328. }
  1329. if (xmlDebugCatalogs)
  1330. xmlGenericError(xmlGenericErrorContext,
  1331. "%s not found in file hash\n", catal->URL);
  1332. }
  1333. /*
  1334. * Fetch and parse. Note that xmlParseXMLCatalogFile does not
  1335. * use the existing catalog, there is no recursion allowed at
  1336. * that level.
  1337. */
  1338. doc = xmlParseXMLCatalogFile(catal->prefer, catal->URL);
  1339. if (doc == NULL) {
  1340. catal->type = XML_CATA_BROKEN_CATALOG;
  1341. xmlRMutexUnlock(xmlCatalogMutex);
  1342. return(-1);
  1343. }
  1344. if (catal->type == XML_CATA_CATALOG)
  1345. catal->children = doc->children;
  1346. else
  1347. catal->children = doc;
  1348. doc->dealloc = 1;
  1349. if (xmlCatalogXMLFiles == NULL)
  1350. xmlCatalogXMLFiles = xmlHashCreate(10);
  1351. if (xmlCatalogXMLFiles != NULL) {
  1352. if (xmlDebugCatalogs)
  1353. xmlGenericError(xmlGenericErrorContext,
  1354. "%s added to file hash\n", catal->URL);
  1355. xmlHashAddEntry(xmlCatalogXMLFiles, catal->URL, doc);
  1356. }
  1357. xmlRMutexUnlock(xmlCatalogMutex);
  1358. return(0);
  1359. }
  1360. /************************************************************************
  1361. * *
  1362. * XML Catalog handling *
  1363. * *
  1364. ************************************************************************/
  1365. /**
  1366. * xmlAddXMLCatalog:
  1367. * @catal: top of an XML catalog
  1368. * @type: the type of record to add to the catalog
  1369. * @orig: the system, public or prefix to match (or NULL)
  1370. * @replace: the replacement value for the match
  1371. *
  1372. * Add an entry in the XML catalog, it may overwrite existing but
  1373. * different entries.
  1374. *
  1375. * Returns 0 if successful, -1 otherwise
  1376. */
  1377. static int
  1378. xmlAddXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *type,
  1379. const xmlChar *orig, const xmlChar *replace) {
  1380. xmlCatalogEntryPtr cur;
  1381. xmlCatalogEntryType typ;
  1382. int doregister = 0;
  1383. if ((catal == NULL) ||
  1384. ((catal->type != XML_CATA_CATALOG) &&
  1385. (catal->type != XML_CATA_BROKEN_CATALOG)))
  1386. return(-1);
  1387. if (catal->children == NULL) {
  1388. xmlFetchXMLCatalogFile(catal);
  1389. }
  1390. if (catal->children == NULL)
  1391. doregister = 1;
  1392. typ = xmlGetXMLCatalogEntryType(type);
  1393. if (typ == XML_CATA_NONE) {
  1394. if (xmlDebugCatalogs)
  1395. xmlGenericError(xmlGenericErrorContext,
  1396. "Failed to add unknown element %s to catalog\n", type);
  1397. return(-1);
  1398. }
  1399. cur = catal->children;
  1400. /*
  1401. * Might be a simple "update in place"
  1402. */
  1403. if (cur != NULL) {
  1404. while (cur != NULL) {
  1405. if ((orig != NULL) && (cur->type == typ) &&
  1406. (xmlStrEqual(orig, cur->name))) {
  1407. if (xmlDebugCatalogs)
  1408. xmlGenericError(xmlGenericErrorContext,
  1409. "Updating element %s to catalog\n", type);
  1410. if (cur->value != NULL)
  1411. xmlFree(cur->value);
  1412. if (cur->URL != NULL)
  1413. xmlFree(cur->URL);
  1414. cur->value = xmlStrdup(replace);
  1415. cur->URL = xmlStrdup(replace);
  1416. return(0);
  1417. }
  1418. if (cur->next == NULL)
  1419. break;
  1420. cur = cur->next;
  1421. }
  1422. }
  1423. if (xmlDebugCatalogs)
  1424. xmlGenericError(xmlGenericErrorContext,
  1425. "Adding element %s to catalog\n", type);
  1426. if (cur == NULL)
  1427. catal->children = xmlNewCatalogEntry(typ, orig, replace,
  1428. NULL, catal->prefer, NULL);
  1429. else
  1430. cur->next = xmlNewCatalogEntry(typ, orig, replace,
  1431. NULL, catal->prefer, NULL);
  1432. if (doregister) {
  1433. catal->type = XML_CATA_CATALOG;
  1434. cur = (xmlCatalogEntryPtr)xmlHashLookup(xmlCatalogXMLFiles, catal->URL);
  1435. if (cur != NULL)
  1436. cur->children = catal->children;
  1437. }
  1438. return(0);
  1439. }
  1440. /**
  1441. * xmlDelXMLCatalog:
  1442. * @catal: top of an XML catalog
  1443. * @value: the value to remove from the catalog
  1444. *
  1445. * Remove entries in the XML catalog where the value or the URI
  1446. * is equal to @value
  1447. *
  1448. * Returns the number of entries removed if successful, -1 otherwise
  1449. */
  1450. static int
  1451. xmlDelXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *value) {
  1452. xmlCatalogEntryPtr cur;
  1453. int ret = 0;
  1454. if ((catal == NULL) ||
  1455. ((catal->type != XML_CATA_CATALOG) &&
  1456. (catal->type != XML_CATA_BROKEN_CATALOG)))
  1457. return(-1);
  1458. if (value == NULL)
  1459. return(-1);
  1460. if (catal->children == NULL) {
  1461. xmlFetchXMLCatalogFile(catal);
  1462. }
  1463. /*
  1464. * Scan the children
  1465. */
  1466. cur = catal->children;
  1467. while (cur != NULL) {
  1468. if (((cur->name != NULL) && (xmlStrEqual(value, cur->name))) ||
  1469. (xmlStrEqual(value, cur->value))) {
  1470. if (xmlDebugCatalogs) {
  1471. if (cur->name != NULL)
  1472. xmlGenericError(xmlGenericErrorContext,
  1473. "Removing element %s from catalog\n", cur->name);
  1474. else
  1475. xmlGenericError(xmlGenericErrorContext,
  1476. "Removing element %s from catalog\n", cur->value);
  1477. }
  1478. cur->type = XML_CATA_REMOVED;
  1479. }
  1480. cur = cur->next;
  1481. }
  1482. return(ret);
  1483. }
  1484. /**
  1485. * xmlCatalogXMLResolve:
  1486. * @catal: a catalog list
  1487. * @pubID: the public ID string
  1488. * @sysID: the system ID string
  1489. *
  1490. * Do a complete resolution lookup of an External Identifier for a
  1491. * list of catalog entries.
  1492. *
  1493. * Implements (or tries to) 7.1. External Identifier Resolution
  1494. * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
  1495. *
  1496. * Returns the URI of the resource or NULL if not found
  1497. */
  1498. static xmlChar *
  1499. xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
  1500. const xmlChar *sysID) {
  1501. xmlChar *ret = NULL;
  1502. xmlCatalogEntryPtr cur;
  1503. int haveDelegate = 0;
  1504. int haveNext = 0;
  1505. /*
  1506. * protection against loops
  1507. */
  1508. if (catal->depth > MAX_CATAL_DEPTH) {
  1509. xmlCatalogErr(catal, NULL, XML_CATALOG_RECURSION,
  1510. "Detected recursion in catalog %s\n",
  1511. catal->name, NULL, NULL);
  1512. return(NULL);
  1513. }
  1514. catal->depth++;
  1515. /*
  1516. * First tries steps 2/ 3/ 4/ if a system ID is provided.
  1517. */
  1518. if (sysID != NULL) {
  1519. xmlCatalogEntryPtr rewrite = NULL;
  1520. int lenrewrite = 0, len;
  1521. cur = catal;
  1522. haveDelegate = 0;
  1523. while (cur != NULL) {
  1524. switch (cur->type) {
  1525. case XML_CATA_SYSTEM:
  1526. if (xmlStrEqual(sysID, cur->name)) {
  1527. if (xmlDebugCatalogs)
  1528. xmlGenericError(xmlGenericErrorContext,
  1529. "Found system match %s, using %s\n",
  1530. cur->name, cur->URL);
  1531. catal->depth--;
  1532. return(xmlStrdup(cur->URL));
  1533. }
  1534. break;
  1535. case XML_CATA_REWRITE_SYSTEM:
  1536. len = xmlStrlen(cur->name);
  1537. if ((len > lenrewrite) &&
  1538. (!xmlStrncmp(sysID, cur->name, len))) {
  1539. lenrewrite = len;
  1540. rewrite = cur;
  1541. }
  1542. break;
  1543. case XML_CATA_DELEGATE_SYSTEM:
  1544. if (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))
  1545. haveDelegate++;
  1546. break;
  1547. case XML_CATA_NEXT_CATALOG:
  1548. haveNext++;
  1549. break;
  1550. default:
  1551. break;
  1552. }
  1553. cur = cur->next;
  1554. }
  1555. if (rewrite != NULL) {
  1556. if (xmlDebugCatalogs)
  1557. xmlGenericError(xmlGenericErrorContext,
  1558. "Using rewriting rule %s\n", rewrite->name);
  1559. ret = xmlStrdup(rewrite->URL);
  1560. if (ret != NULL)
  1561. ret = xmlStrcat(ret, &sysID[lenrewrite]);
  1562. catal->depth--;
  1563. return(ret);
  1564. }
  1565. if (haveDelegate) {
  1566. const xmlChar *delegates[MAX_DELEGATE];
  1567. int nbList = 0, i;
  1568. /*
  1569. * Assume the entries have been sorted by decreasing substring
  1570. * matches when the list was produced.
  1571. */
  1572. cur = catal;
  1573. while (cur != NULL) {
  1574. if ((cur->type == XML_CATA_DELEGATE_SYSTEM) &&
  1575. (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))) {
  1576. for (i = 0;i < nbList;i++)
  1577. if (xmlStrEqual(cur->URL, delegates[i]))
  1578. break;
  1579. if (i < nbList) {
  1580. cur = cur->next;
  1581. continue;
  1582. }
  1583. if (nbList < MAX_DELEGATE)
  1584. delegates[nbList++] = cur->URL;
  1585. if (cur->children == NULL) {
  1586. xmlFetchXMLCatalogFile(cur);
  1587. }
  1588. if (cur->children != NULL) {
  1589. if (xmlDebugCatalogs)
  1590. xmlGenericError(xmlGenericErrorContext,
  1591. "Trying system delegate %s\n", cur->URL);
  1592. ret = xmlCatalogListXMLResolve(
  1593. cur->children, NULL, sysID);
  1594. if (ret != NULL) {
  1595. catal->depth--;
  1596. return(ret);
  1597. }
  1598. }
  1599. }
  1600. cur = cur->next;
  1601. }
  1602. /*
  1603. * Apply the cut algorithm explained in 4/
  1604. */
  1605. catal->depth--;
  1606. return(XML_CATAL_BREAK);
  1607. }
  1608. }
  1609. /*
  1610. * Then tries 5/ 6/ if a public ID is provided
  1611. */
  1612. if (pubID != NULL) {
  1613. cur = catal;
  1614. haveDelegate = 0;
  1615. while (cur != NULL) {
  1616. switch (cur->type) {
  1617. case XML_CATA_PUBLIC:
  1618. if (xmlStrEqual(pubID, cur->name)) {
  1619. if (xmlDebugCatalogs)
  1620. xmlGenericError(xmlGenericErrorContext,
  1621. "Found public match %s\n", cur->name);
  1622. catal->depth--;
  1623. return(xmlStrdup(cur->URL));
  1624. }
  1625. break;
  1626. case XML_CATA_DELEGATE_PUBLIC:
  1627. if (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)) &&
  1628. (cur->prefer == XML_CATA_PREFER_PUBLIC))
  1629. haveDelegate++;
  1630. break;
  1631. case XML_CATA_NEXT_CATALOG:
  1632. if (sysID == NULL)
  1633. haveNext++;
  1634. break;
  1635. default:
  1636. break;
  1637. }
  1638. cur = cur->next;
  1639. }
  1640. if (haveDelegate) {
  1641. const xmlChar *delegates[MAX_DELEGATE];
  1642. int nbList = 0, i;
  1643. /*
  1644. * Assume the entries have been sorted by decreasing substring
  1645. * matches when the list was produced.
  1646. */
  1647. cur = catal;
  1648. while (cur != NULL) {
  1649. if ((cur->type == XML_CATA_DELEGATE_PUBLIC) &&
  1650. (cur->prefer == XML_CATA_PREFER_PUBLIC) &&
  1651. (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)))) {
  1652. for (i = 0;i < nbList;i++)
  1653. if (xmlStrEqual(cur->URL, delegates[i]))
  1654. break;
  1655. if (i < nbList) {
  1656. cur = cur->next;
  1657. continue;
  1658. }
  1659. if (nbList < MAX_DELEGATE)
  1660. delegates[nbList++] = cur->URL;
  1661. if (cur->children == NULL) {
  1662. xmlFetchXMLCatalogFile(cur);
  1663. }
  1664. if (cur->children != NULL) {
  1665. if (xmlDebugCatalogs)
  1666. xmlGenericError(xmlGenericErrorContext,
  1667. "Trying public delegate %s\n", cur->URL);
  1668. ret = xmlCatalogListXMLResolve(
  1669. cur->children, pubID, NULL);
  1670. if (ret != NULL) {
  1671. catal->depth--;
  1672. return(ret);
  1673. }
  1674. }
  1675. }
  1676. cur = cur->next;
  1677. }
  1678. /*
  1679. * Apply the cut algorithm explained in 4/
  1680. */
  1681. catal->depth--;
  1682. return(XML_CATAL_BREAK);
  1683. }
  1684. }
  1685. if (haveNext) {
  1686. cur = catal;
  1687. while (cur != NULL) {
  1688. if (cur->type == XML_CATA_NEXT_CATALOG) {
  1689. if (cur->children == NULL) {
  1690. xmlFetchXMLCatalogFile(cur);
  1691. }
  1692. if (cur->children != NULL) {
  1693. ret = xmlCatalogListXMLResolve(cur->children, pubID, sysID);
  1694. if (ret != NULL) {
  1695. catal->depth--;
  1696. return(ret);
  1697. } else if (catal->depth > MAX_CATAL_DEPTH) {
  1698. return(NULL);
  1699. }
  1700. }
  1701. }
  1702. cur = cur->next;
  1703. }
  1704. }
  1705. catal->depth--;
  1706. return(NULL);
  1707. }
  1708. /**
  1709. * xmlCatalogXMLResolveURI:
  1710. * @catal: a catalog list
  1711. * @URI: the URI
  1712. * @sysID: the system ID string
  1713. *
  1714. * Do a complete resolution lookup of an External Identifier for a
  1715. * list of catalog entries.
  1716. *
  1717. * Implements (or tries to) 7.2.2. URI Resolution
  1718. * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
  1719. *
  1720. * Returns the URI of the resource or NULL if not found
  1721. */
  1722. static xmlChar *
  1723. xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
  1724. xmlChar *ret = NULL;
  1725. xmlCatalogEntryPtr cur;
  1726. int haveDelegate = 0;
  1727. int haveNext = 0;
  1728. xmlCatalogEntryPtr rewrite = NULL;
  1729. int lenrewrite = 0, len;
  1730. if (catal == NULL)
  1731. return(NULL);
  1732. if (URI == NULL)
  1733. return(NULL);
  1734. if (catal->depth > MAX_CATAL_DEPTH) {
  1735. xmlCatalogErr(catal, NULL, XML_CATALOG_RECURSION,
  1736. "Detected recursion in catalog %s\n",
  1737. catal->name, NULL, NULL);
  1738. return(NULL);
  1739. }
  1740. /*
  1741. * First tries steps 2/ 3/ 4/ if a system ID is provided.
  1742. */
  1743. cur = catal;
  1744. haveDelegate = 0;
  1745. while (cur != NULL) {
  1746. switch (cur->type) {
  1747. case XML_CATA_URI:
  1748. if (xmlStrEqual(URI, cur->name)) {
  1749. if (xmlDebugCatalogs)
  1750. xmlGenericError(xmlGenericErrorContext,
  1751. "Found URI match %s\n", cur->name);
  1752. return(xmlStrdup(cur->URL));
  1753. }
  1754. break;
  1755. case XML_CATA_REWRITE_URI:
  1756. len = xmlStrlen(cur->name);
  1757. if ((len > lenrewrite) &&
  1758. (!xmlStrncmp(URI, cur->name, len))) {
  1759. lenrewrite = len;
  1760. rewrite = cur;
  1761. }
  1762. break;
  1763. case XML_CATA_DELEGATE_URI:
  1764. if (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))
  1765. haveDelegate++;
  1766. break;
  1767. case XML_CATA_NEXT_CATALOG:
  1768. haveNext++;
  1769. break;
  1770. default:
  1771. break;
  1772. }
  1773. cur = cur->next;
  1774. }
  1775. if (rewrite != NULL) {
  1776. if (xmlDebugCatalogs)
  1777. xmlGenericError(xmlGenericErrorContext,
  1778. "Using rewriting rule %s\n", rewrite->name);
  1779. ret = xmlStrdup(rewrite->URL);
  1780. if (ret != NULL)
  1781. ret = xmlStrcat(ret, &URI[lenrewrite]);
  1782. return(ret);
  1783. }
  1784. if (haveDelegate) {
  1785. const xmlChar *delegates[MAX_DELEGATE];
  1786. int nbList = 0, i;
  1787. /*
  1788. * Assume the entries have been sorted by decreasing substring
  1789. * matches when the list was produced.
  1790. */
  1791. cur = catal;
  1792. while (cur != NULL) {
  1793. if (((cur->type == XML_CATA_DELEGATE_SYSTEM) ||
  1794. (cur->type == XML_CATA_DELEGATE_URI)) &&
  1795. (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))) {
  1796. for (i = 0;i < nbList;i++)
  1797. if (xmlStrEqual(cur->URL, delegates[i]))
  1798. break;
  1799. if (i < nbList) {
  1800. cur = cur->next;
  1801. continue;
  1802. }
  1803. if (nbList < MAX_DELEGATE)
  1804. delegates[nbList++] = cur->URL;
  1805. if (cur->children == NULL) {
  1806. xmlFetchXMLCatalogFile(cur);
  1807. }
  1808. if (cur->children != NULL) {
  1809. if (xmlDebugCatalogs)
  1810. xmlGenericError(xmlGenericErrorContext,
  1811. "Trying URI delegate %s\n", cur->URL);
  1812. ret = xmlCatalogListXMLResolveURI(
  1813. cur->children, URI);
  1814. if (ret != NULL)
  1815. return(ret);
  1816. }
  1817. }
  1818. cur = cur->next;
  1819. }
  1820. /*
  1821. * Apply the cut algorithm explained in 4/
  1822. */
  1823. return(XML_CATAL_BREAK);
  1824. }
  1825. if (haveNext) {
  1826. cur = catal;
  1827. while (cur != NULL) {
  1828. if (cur->type == XML_CATA_NEXT_CATALOG) {
  1829. if (cur->children == NULL) {
  1830. xmlFetchXMLCatalogFile(cur);
  1831. }
  1832. if (cur->children != NULL) {
  1833. ret = xmlCatalogListXMLResolveURI(cur->children, URI);
  1834. if (ret != NULL)
  1835. return(ret);
  1836. }
  1837. }
  1838. cur = cur->next;
  1839. }
  1840. }
  1841. return(NULL);
  1842. }
  1843. /**
  1844. * xmlCatalogListXMLResolve:
  1845. * @catal: a catalog list
  1846. * @pubID: the public ID string
  1847. * @sysID: the system ID string
  1848. *
  1849. * Do a complete resolution lookup of an External Identifier for a
  1850. * list of catalogs
  1851. *
  1852. * Implements (or tries to) 7.1. External Identifier Resolution
  1853. * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
  1854. *
  1855. * Returns the URI of the resource or NULL if not found
  1856. */
  1857. static xmlChar *
  1858. xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
  1859. const xmlChar *sysID) {
  1860. xmlChar *ret = NULL;
  1861. xmlChar *urnID = NULL;
  1862. xmlChar *normid;
  1863. if (catal == NULL)
  1864. return(NULL);
  1865. if ((pubID == NULL) && (sysID == NULL))
  1866. return(NULL);
  1867. normid = xmlCatalogNormalizePublic(pubID);
  1868. if (normid != NULL)
  1869. pubID = (*normid != 0 ? normid : NULL);
  1870. if (!xmlStrncmp(pubID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
  1871. urnID = xmlCatalogUnWrapURN(pubID);
  1872. if (xmlDebugCatalogs) {
  1873. if (urnID == NULL)
  1874. xmlGenericError(xmlGenericErrorContext,
  1875. "Public URN ID %s expanded to NULL\n", pubID);
  1876. else
  1877. xmlGenericError(xmlGenericErrorContext,
  1878. "Public URN ID expanded to %s\n", urnID);
  1879. }
  1880. ret = xmlCatalogListXMLResolve(catal, urnID, sysID);
  1881. if (urnID != NULL)
  1882. xmlFree(urnID);
  1883. if (normid != NULL)
  1884. xmlFree(normid);
  1885. return(ret);
  1886. }
  1887. if (!xmlStrncmp(sysID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
  1888. urnID = xmlCatalogUnWrapURN(sysID);
  1889. if (xmlDebugCatalogs) {
  1890. if (urnID == NULL)
  1891. xmlGenericError(xmlGenericErrorContext,
  1892. "System URN ID %s expanded to NULL\n", sysID);
  1893. else
  1894. xmlGenericError(xmlGenericErrorContext,
  1895. "System URN ID expanded to %s\n", urnID);
  1896. }
  1897. if (pubID == NULL)
  1898. ret = xmlCatalogListXMLResolve(catal, urnID, NULL);
  1899. else if (xmlStrEqual(pubID, urnID))
  1900. ret = xmlCatalogListXMLResolve(catal, pubID, NULL);
  1901. else {
  1902. ret = xmlCatalogListXMLResolve(catal, pubID, urnID);
  1903. }
  1904. if (urnID != NULL)
  1905. xmlFree(urnID);
  1906. if (normid != NULL)
  1907. xmlFree(normid);
  1908. return(ret);
  1909. }
  1910. while (catal != NULL) {
  1911. if (catal->type == XML_CATA_CATALOG) {
  1912. if (catal->children == NULL) {
  1913. xmlFetchXMLCatalogFile(catal);
  1914. }
  1915. if (catal->children != NULL) {
  1916. ret = xmlCatalogXMLResolve(catal->children, pubID, sysID);
  1917. if (ret != NULL) {
  1918. break;
  1919. } else if (catal->children->depth > MAX_CATAL_DEPTH) {
  1920. ret = NULL;
  1921. break;
  1922. }
  1923. }
  1924. }
  1925. catal = catal->next;
  1926. }
  1927. if (normid != NULL)
  1928. xmlFree(normid);
  1929. return(ret);
  1930. }
  1931. /**
  1932. * xmlCatalogListXMLResolveURI:
  1933. * @catal: a catalog list
  1934. * @URI: the URI
  1935. *
  1936. * Do a complete resolution lookup of an URI for a list of catalogs
  1937. *
  1938. * Implements (or tries to) 7.2. URI Resolution
  1939. * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
  1940. *
  1941. * Returns the URI of the resource or NULL if not found
  1942. */
  1943. static xmlChar *
  1944. xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
  1945. xmlChar *ret = NULL;
  1946. xmlChar *urnID = NULL;
  1947. if (catal == NULL)
  1948. return(NULL);
  1949. if (URI == NULL)
  1950. return(NULL);
  1951. if (!xmlStrncmp(URI, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
  1952. urnID = xmlCatalogUnWrapURN(URI);
  1953. if (xmlDebugCatalogs) {
  1954. if (urnID == NULL)
  1955. xmlGenericError(xmlGenericErrorContext,
  1956. "URN ID %s expanded to NULL\n", URI);
  1957. else
  1958. xmlGenericError(xmlGenericErrorContext,
  1959. "URN ID expanded to %s\n", urnID);
  1960. }
  1961. ret = xmlCatalogListXMLResolve(catal, urnID, NULL);
  1962. if (urnID != NULL)
  1963. xmlFree(urnID);
  1964. return(ret);
  1965. }
  1966. while (catal != NULL) {
  1967. if (catal->type == XML_CATA_CATALOG) {
  1968. if (catal->children == NULL) {
  1969. xmlFetchXMLCatalogFile(catal);
  1970. }
  1971. if (catal->children != NULL) {
  1972. ret = xmlCatalogXMLResolveURI(catal->children, URI);
  1973. if (ret != NULL)
  1974. return(ret);
  1975. }
  1976. }
  1977. catal = catal->next;
  1978. }
  1979. return(ret);
  1980. }
  1981. /************************************************************************
  1982. * *
  1983. * The SGML Catalog parser *
  1984. * *
  1985. ************************************************************************/
  1986. #define RAW *cur
  1987. #define NEXT cur++;
  1988. #define SKIP(x) cur += x;
  1989. #define SKIP_BLANKS while (IS_BLANK_CH(*cur)) NEXT;
  1990. /**
  1991. * xmlParseSGMLCatalogComment:
  1992. * @cur: the current character
  1993. *
  1994. * Skip a comment in an SGML catalog
  1995. *
  1996. * Returns new current character
  1997. */
  1998. static const xmlChar *
  1999. xmlParseSGMLCatalogComment(const xmlChar *cur) {
  2000. if ((cur[0] != '-') || (cur[1] != '-'))
  2001. return(cur);
  2002. SKIP(2);
  2003. while ((cur[0] != 0) && ((cur[0] != '-') || ((cur[1] != '-'))))
  2004. NEXT;
  2005. if (cur[0] == 0) {
  2006. return(NULL);
  2007. }
  2008. return(cur + 2);
  2009. }
  2010. /**
  2011. * xmlParseSGMLCatalogPubid:
  2012. * @cur: the current character
  2013. * @id: the return location
  2014. *
  2015. * Parse an SGML catalog ID
  2016. *
  2017. * Returns new current character and store the value in @id
  2018. */
  2019. static const xmlChar *
  2020. xmlParseSGMLCatalogPubid(const xmlChar *cur, xmlChar **id) {
  2021. xmlChar *buf = NULL, *tmp;
  2022. int len = 0;
  2023. int size = 50;
  2024. xmlChar stop;
  2025. *id = NULL;
  2026. if (RAW == '"') {
  2027. NEXT;
  2028. stop = '"';
  2029. } else if (RAW == '\'') {
  2030. NEXT;
  2031. stop = '\'';
  2032. } else {
  2033. stop = ' ';
  2034. }
  2035. buf = (xmlChar *) xmlMallocAtomic(size);
  2036. if (buf == NULL) {
  2037. xmlCatalogErrMemory("allocating public ID");
  2038. return(NULL);
  2039. }
  2040. while (IS_PUBIDCHAR_CH(*cur) || (*cur == '?')) {
  2041. if ((*cur == stop) && (stop != ' '))
  2042. break;
  2043. if ((stop == ' ') && (IS_BLANK_CH(*cur)))
  2044. break;
  2045. if (len + 1 >= size) {
  2046. size *= 2;
  2047. tmp = (xmlChar *) xmlRealloc(buf, size);
  2048. if (tmp == NULL) {
  2049. xmlCatalogErrMemory("allocating public ID");
  2050. xmlFree(buf);
  2051. return(NULL);
  2052. }
  2053. buf = tmp;
  2054. }
  2055. buf[len++] = *cur;
  2056. NEXT;
  2057. }
  2058. buf[len] = 0;
  2059. if (stop == ' ') {
  2060. if (!IS_BLANK_CH(*cur)) {
  2061. xmlFree(buf);
  2062. return(NULL);
  2063. }
  2064. } else {
  2065. if (*cur != stop) {
  2066. xmlFree(buf);
  2067. return(NULL);
  2068. }
  2069. NEXT;
  2070. }
  2071. *id = buf;
  2072. return(cur);
  2073. }
  2074. /**
  2075. * xmlParseSGMLCatalogName:
  2076. * @cur: the current character
  2077. * @name: the return location
  2078. *
  2079. * Parse an SGML catalog name
  2080. *
  2081. * Returns new current character and store the value in @name
  2082. */
  2083. static const xmlChar *
  2084. xmlParseSGMLCatalogName(const xmlChar *cur, xmlChar **name) {
  2085. xmlChar buf[XML_MAX_NAMELEN + 5];
  2086. int len = 0;
  2087. int c;
  2088. *name = NULL;
  2089. /*
  2090. * Handler for more complex cases
  2091. */
  2092. c = *cur;
  2093. if ((!IS_LETTER(c) && (c != '_') && (c != ':'))) {
  2094. return(NULL);
  2095. }
  2096. while (((IS_LETTER(c)) || (IS_DIGIT(c)) ||
  2097. (c == '.') || (c == '-') ||
  2098. (c == '_') || (c == ':'))) {
  2099. buf[len++] = c;
  2100. cur++;
  2101. c = *cur;
  2102. if (len >= XML_MAX_NAMELEN)
  2103. return(NULL);
  2104. }
  2105. *name = xmlStrndup(buf, len);
  2106. return(cur);
  2107. }
  2108. /**
  2109. * xmlGetSGMLCatalogEntryType:
  2110. * @name: the entry name
  2111. *
  2112. * Get the Catalog entry type for a given SGML Catalog name
  2113. *
  2114. * Returns Catalog entry type
  2115. */
  2116. static xmlCatalogEntryType
  2117. xmlGetSGMLCatalogEntryType(const xmlChar *name) {
  2118. xmlCatalogEntryType type = XML_CATA_NONE;
  2119. if (xmlStrEqual(name, (const xmlChar *) "SYSTEM"))
  2120. type = SGML_CATA_SYSTEM;
  2121. else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC"))
  2122. type = SGML_CATA_PUBLIC;
  2123. else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE"))
  2124. type = SGML_CATA_DELEGATE;
  2125. else if (xmlStrEqual(name, (const xmlChar *) "ENTITY"))
  2126. type = SGML_CATA_ENTITY;
  2127. else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE"))
  2128. type = SGML_CATA_DOCTYPE;
  2129. else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE"))
  2130. type = SGML_CATA_LINKTYPE;
  2131. else if (xmlStrEqual(name, (const xmlChar *) "NOTATION"))
  2132. type = SGML_CATA_NOTATION;
  2133. else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL"))
  2134. type = SGML_CATA_SGMLDECL;
  2135. else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT"))
  2136. type = SGML_CATA_DOCUMENT;
  2137. else if (xmlStrEqual(name, (const xmlChar *) "CATALOG"))
  2138. type = SGML_CATA_CATALOG;
  2139. else if (xmlStrEqual(name, (const xmlChar *) "BASE"))
  2140. type = SGML_CATA_BASE;
  2141. return(type);
  2142. }
  2143. /**
  2144. * xmlParseSGMLCatalog:
  2145. * @catal: the SGML Catalog
  2146. * @value: the content of the SGML Catalog serialization
  2147. * @file: the filepath for the catalog
  2148. * @super: should this be handled as a Super Catalog in which case
  2149. * parsing is not recursive
  2150. *
  2151. * Parse an SGML catalog content and fill up the @catal hash table with
  2152. * the new entries found.
  2153. *
  2154. * Returns 0 in case of success, -1 in case of error.
  2155. */
  2156. static int
  2157. xmlParseSGMLCatalog(xmlCatalogPtr catal, const xmlChar *value,
  2158. const char *file, int super) {
  2159. const xmlChar *cur = value;
  2160. xmlChar *base = NULL;
  2161. int res;
  2162. if ((cur == NULL) || (file == NULL))
  2163. return(-1);
  2164. base = xmlStrdup((const xmlChar *) file);
  2165. while ((cur != NULL) && (cur[0] != 0)) {
  2166. SKIP_BLANKS;
  2167. if (cur[0] == 0)
  2168. break;
  2169. if ((cur[0] == '-') && (cur[1] == '-')) {
  2170. cur = xmlParseSGMLCatalogComment(cur);
  2171. if (cur == NULL) {
  2172. /* error */
  2173. break;
  2174. }
  2175. } else {
  2176. xmlChar *sysid = NULL;
  2177. xmlChar *name = NULL;
  2178. xmlCatalogEntryType type = XML_CATA_NONE;
  2179. cur = xmlParseSGMLCatalogName(cur, &name);
  2180. if (cur == NULL || name == NULL) {
  2181. /* error */
  2182. break;
  2183. }
  2184. if (!IS_BLANK_CH(*cur)) {
  2185. /* error */
  2186. xmlFree(name);
  2187. break;
  2188. }
  2189. SKIP_BLANKS;
  2190. if (xmlStrEqual(name, (const xmlChar *) "SYSTEM"))
  2191. type = SGML_CATA_SYSTEM;
  2192. else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC"))
  2193. type = SGML_CATA_PUBLIC;
  2194. else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE"))
  2195. type = SGML_CATA_DELEGATE;
  2196. else if (xmlStrEqual(name, (const xmlChar *) "ENTITY"))
  2197. type = SGML_CATA_ENTITY;
  2198. else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE"))
  2199. type = SGML_CATA_DOCTYPE;
  2200. else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE"))
  2201. type = SGML_CATA_LINKTYPE;
  2202. else if (xmlStrEqual(name, (const xmlChar *) "NOTATION"))
  2203. type = SGML_CATA_NOTATION;
  2204. else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL"))
  2205. type = SGML_CATA_SGMLDECL;
  2206. else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT"))
  2207. type = SGML_CATA_DOCUMENT;
  2208. else if (xmlStrEqual(name, (const xmlChar *) "CATALOG"))
  2209. type = SGML_CATA_CATALOG;
  2210. else if (xmlStrEqual(name, (const xmlChar *) "BASE"))
  2211. type = SGML_CATA_BASE;
  2212. else if (xmlStrEqual(name, (const xmlChar *) "OVERRIDE")) {
  2213. xmlFree(name);
  2214. cur = xmlParseSGMLCatalogName(cur, &name);
  2215. if (name == NULL) {
  2216. /* error */
  2217. break;
  2218. }
  2219. xmlFree(name);
  2220. continue;
  2221. }
  2222. xmlFree(name);
  2223. name = NULL;
  2224. switch(type) {
  2225. case SGML_CATA_ENTITY:
  2226. if (*cur == '%')
  2227. type = SGML_CATA_PENTITY;
  2228. /* Falls through. */
  2229. case SGML_CATA_PENTITY:
  2230. case SGML_CATA_DOCTYPE:
  2231. case SGML_CATA_LINKTYPE:
  2232. case SGML_CATA_NOTATION:
  2233. cur = xmlParseSGMLCatalogName(cur, &name);
  2234. if (cur == NULL) {
  2235. /* error */
  2236. break;
  2237. }
  2238. if (!IS_BLANK_CH(*cur)) {
  2239. /* error */
  2240. break;
  2241. }
  2242. SKIP_BLANKS;
  2243. cur = xmlParseSGMLCatalogPubid(cur, &sysid);
  2244. if (cur == NULL) {
  2245. /* error */
  2246. break;
  2247. }
  2248. break;
  2249. case SGML_CATA_PUBLIC:
  2250. case SGML_CATA_SYSTEM:
  2251. case SGML_CATA_DELEGATE:
  2252. cur = xmlParseSGMLCatalogPubid(cur, &name);
  2253. if (cur == NULL) {
  2254. /* error */
  2255. break;
  2256. }
  2257. if (type != SGML_CATA_SYSTEM) {
  2258. xmlChar *normid;
  2259. normid = xmlCatalogNormalizePublic(name);
  2260. if (normid != NULL) {
  2261. if (name != NULL)
  2262. xmlFree(name);
  2263. if (*normid != 0)
  2264. name = normid;
  2265. else {
  2266. xmlFree(normid);
  2267. name = NULL;
  2268. }
  2269. }
  2270. }
  2271. if (!IS_BLANK_CH(*cur)) {
  2272. /* error */
  2273. break;
  2274. }
  2275. SKIP_BLANKS;
  2276. cur = xmlParseSGMLCatalogPubid(cur, &sysid);
  2277. if (cur == NULL) {
  2278. /* error */
  2279. break;
  2280. }
  2281. break;
  2282. case SGML_CATA_BASE:
  2283. case SGML_CATA_CATALOG:
  2284. case SGML_CATA_DOCUMENT:
  2285. case SGML_CATA_SGMLDECL:
  2286. cur = xmlParseSGMLCatalogPubid(cur, &sysid);
  2287. if (cur == NULL) {
  2288. /* error */
  2289. break;
  2290. }
  2291. break;
  2292. default:
  2293. break;
  2294. }
  2295. if (cur == NULL) {
  2296. if (name != NULL)
  2297. xmlFree(name);
  2298. if (sysid != NULL)
  2299. xmlFree(sysid);
  2300. break;
  2301. } else if (type == SGML_CATA_BASE) {
  2302. if (base != NULL)
  2303. xmlFree(base);
  2304. base = xmlStrdup(sysid);
  2305. } else if ((type == SGML_CATA_PUBLIC) ||
  2306. (type == SGML_CATA_SYSTEM)) {
  2307. xmlChar *filename;
  2308. filename = xmlBuildURI(sysid, base);
  2309. if (filename != NULL) {
  2310. xmlCatalogEntryPtr entry;
  2311. entry = xmlNewCatalogEntry(type, name, filename,
  2312. NULL, XML_CATA_PREFER_NONE, NULL);
  2313. res = xmlHashAddEntry(catal->sgml, name, entry);
  2314. if (res < 0) {
  2315. xmlFreeCatalogEntry(entry, NULL);
  2316. }
  2317. xmlFree(filename);
  2318. }
  2319. } else if (type == SGML_CATA_CATALOG) {
  2320. if (super) {
  2321. xmlCatalogEntryPtr entry;
  2322. entry = xmlNewCatalogEntry(type, sysid, NULL, NULL,
  2323. XML_CATA_PREFER_NONE, NULL);
  2324. res = xmlHashAddEntry(catal->sgml, sysid, entry);
  2325. if (res < 0) {
  2326. xmlFreeCatalogEntry(entry, NULL);
  2327. }
  2328. } else {
  2329. xmlChar *filename;
  2330. filename = xmlBuildURI(sysid, base);
  2331. if (filename != NULL) {
  2332. xmlExpandCatalog(catal, (const char *)filename);
  2333. xmlFree(filename);
  2334. }
  2335. }
  2336. }
  2337. /*
  2338. * drop anything else we won't handle it
  2339. */
  2340. if (name != NULL)
  2341. xmlFree(name);
  2342. if (sysid != NULL)
  2343. xmlFree(sysid);
  2344. }
  2345. }
  2346. if (base != NULL)
  2347. xmlFree(base);
  2348. if (cur == NULL)
  2349. return(-1);
  2350. return(0);
  2351. }
  2352. /************************************************************************
  2353. * *
  2354. * SGML Catalog handling *
  2355. * *
  2356. ************************************************************************/
  2357. /**
  2358. * xmlCatalogGetSGMLPublic:
  2359. * @catal: an SGML catalog hash
  2360. * @pubID: the public ID string
  2361. *
  2362. * Try to lookup the catalog local reference associated to a public ID
  2363. *
  2364. * Returns the local resource if found or NULL otherwise.
  2365. */
  2366. static const xmlChar *
  2367. xmlCatalogGetSGMLPublic(xmlHashTablePtr catal, const xmlChar *pubID) {
  2368. xmlCatalogEntryPtr entry;
  2369. xmlChar *normid;
  2370. if (catal == NULL)
  2371. return(NULL);
  2372. normid = xmlCatalogNormalizePublic(pubID);
  2373. if (normid != NULL)
  2374. pubID = (*normid != 0 ? normid : NULL);
  2375. entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, pubID);
  2376. if (entry == NULL) {
  2377. if (normid != NULL)
  2378. xmlFree(normid);
  2379. return(NULL);
  2380. }
  2381. if (entry->type == SGML_CATA_PUBLIC) {
  2382. if (normid != NULL)
  2383. xmlFree(normid);
  2384. return(entry->URL);
  2385. }
  2386. if (normid != NULL)
  2387. xmlFree(normid);
  2388. return(NULL);
  2389. }
  2390. /**
  2391. * xmlCatalogGetSGMLSystem:
  2392. * @catal: an SGML catalog hash
  2393. * @sysID: the system ID string
  2394. *
  2395. * Try to lookup the catalog local reference for a system ID
  2396. *
  2397. * Returns the local resource if found or NULL otherwise.
  2398. */
  2399. static const xmlChar *
  2400. xmlCatalogGetSGMLSystem(xmlHashTablePtr catal, const xmlChar *sysID) {
  2401. xmlCatalogEntryPtr entry;
  2402. if (catal == NULL)
  2403. return(NULL);
  2404. entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, sysID);
  2405. if (entry == NULL)
  2406. return(NULL);
  2407. if (entry->type == SGML_CATA_SYSTEM)
  2408. return(entry->URL);
  2409. return(NULL);
  2410. }
  2411. /**
  2412. * xmlCatalogSGMLResolve:
  2413. * @catal: the SGML catalog
  2414. * @pubID: the public ID string
  2415. * @sysID: the system ID string
  2416. *
  2417. * Do a complete resolution lookup of an External Identifier
  2418. *
  2419. * Returns the URI of the resource or NULL if not found
  2420. */
  2421. static const xmlChar *
  2422. xmlCatalogSGMLResolve(xmlCatalogPtr catal, const xmlChar *pubID,
  2423. const xmlChar *sysID) {
  2424. const xmlChar *ret = NULL;
  2425. if (catal->sgml == NULL)
  2426. return(NULL);
  2427. if (pubID != NULL)
  2428. ret = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
  2429. if (ret != NULL)
  2430. return(ret);
  2431. if (sysID != NULL)
  2432. ret = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
  2433. if (ret != NULL)
  2434. return(ret);
  2435. return(NULL);
  2436. }
  2437. /************************************************************************
  2438. * *
  2439. * Specific Public interfaces *
  2440. * *
  2441. ************************************************************************/
  2442. /**
  2443. * xmlLoadSGMLSuperCatalog:
  2444. * @filename: a file path
  2445. *
  2446. * Load an SGML super catalog. It won't expand CATALOG or DELEGATE
  2447. * references. This is only needed for manipulating SGML Super Catalogs
  2448. * like adding and removing CATALOG or DELEGATE entries.
  2449. *
  2450. * Returns the catalog parsed or NULL in case of error
  2451. */
  2452. xmlCatalogPtr
  2453. xmlLoadSGMLSuperCatalog(const char *filename)
  2454. {
  2455. xmlChar *content;
  2456. xmlCatalogPtr catal;
  2457. int ret;
  2458. content = xmlLoadFileContent(filename);
  2459. if (content == NULL)
  2460. return(NULL);
  2461. catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
  2462. if (catal == NULL) {
  2463. xmlFree(content);
  2464. return(NULL);
  2465. }
  2466. ret = xmlParseSGMLCatalog(catal, content, filename, 1);
  2467. xmlFree(content);
  2468. if (ret < 0) {
  2469. xmlFreeCatalog(catal);
  2470. return(NULL);
  2471. }
  2472. return (catal);
  2473. }
  2474. /**
  2475. * xmlLoadACatalog:
  2476. * @filename: a file path
  2477. *
  2478. * Load the catalog and build the associated data structures.
  2479. * This can be either an XML Catalog or an SGML Catalog
  2480. * It will recurse in SGML CATALOG entries. On the other hand XML
  2481. * Catalogs are not handled recursively.
  2482. *
  2483. * Returns the catalog parsed or NULL in case of error
  2484. */
  2485. xmlCatalogPtr
  2486. xmlLoadACatalog(const char *filename)
  2487. {
  2488. xmlChar *content;
  2489. xmlChar *first;
  2490. xmlCatalogPtr catal;
  2491. int ret;
  2492. content = xmlLoadFileContent(filename);
  2493. if (content == NULL)
  2494. return(NULL);
  2495. first = content;
  2496. while ((*first != 0) && (*first != '-') && (*first != '<') &&
  2497. (!(((*first >= 'A') && (*first <= 'Z')) ||
  2498. ((*first >= 'a') && (*first <= 'z')))))
  2499. first++;
  2500. if (*first != '<') {
  2501. catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
  2502. if (catal == NULL) {
  2503. xmlFree(content);
  2504. return(NULL);
  2505. }
  2506. ret = xmlParseSGMLCatalog(catal, content, filename, 0);
  2507. if (ret < 0) {
  2508. xmlFreeCatalog(catal);
  2509. xmlFree(content);
  2510. return(NULL);
  2511. }
  2512. } else {
  2513. catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
  2514. if (catal == NULL) {
  2515. xmlFree(content);
  2516. return(NULL);
  2517. }
  2518. catal->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
  2519. NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
  2520. }
  2521. xmlFree(content);
  2522. return (catal);
  2523. }
  2524. /**
  2525. * xmlExpandCatalog:
  2526. * @catal: a catalog
  2527. * @filename: a file path
  2528. *
  2529. * Load the catalog and expand the existing catal structure.
  2530. * This can be either an XML Catalog or an SGML Catalog
  2531. *
  2532. * Returns 0 in case of success, -1 in case of error
  2533. */
  2534. static int
  2535. xmlExpandCatalog(xmlCatalogPtr catal, const char *filename)
  2536. {
  2537. int ret;
  2538. if ((catal == NULL) || (filename == NULL))
  2539. return(-1);
  2540. if (catal->type == XML_SGML_CATALOG_TYPE) {
  2541. xmlChar *content;
  2542. content = xmlLoadFileContent(filename);
  2543. if (content == NULL)
  2544. return(-1);
  2545. ret = xmlParseSGMLCatalog(catal, content, filename, 0);
  2546. if (ret < 0) {
  2547. xmlFree(content);
  2548. return(-1);
  2549. }
  2550. xmlFree(content);
  2551. } else {
  2552. xmlCatalogEntryPtr tmp, cur;
  2553. tmp = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
  2554. NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
  2555. cur = catal->xml;
  2556. if (cur == NULL) {
  2557. catal->xml = tmp;
  2558. } else {
  2559. while (cur->next != NULL) cur = cur->next;
  2560. cur->next = tmp;
  2561. }
  2562. }
  2563. return (0);
  2564. }
  2565. /**
  2566. * xmlACatalogResolveSystem:
  2567. * @catal: a Catalog
  2568. * @sysID: the system ID string
  2569. *
  2570. * Try to lookup the catalog resource for a system ID
  2571. *
  2572. * Returns the resource if found or NULL otherwise, the value returned
  2573. * must be freed by the caller.
  2574. */
  2575. xmlChar *
  2576. xmlACatalogResolveSystem(xmlCatalogPtr catal, const xmlChar *sysID) {
  2577. xmlChar *ret = NULL;
  2578. if ((sysID == NULL) || (catal == NULL))
  2579. return(NULL);
  2580. if (xmlDebugCatalogs)
  2581. xmlGenericError(xmlGenericErrorContext,
  2582. "Resolve sysID %s\n", sysID);
  2583. if (catal->type == XML_XML_CATALOG_TYPE) {
  2584. ret = xmlCatalogListXMLResolve(catal->xml, NULL, sysID);
  2585. if (ret == XML_CATAL_BREAK)
  2586. ret = NULL;
  2587. } else {
  2588. const xmlChar *sgml;
  2589. sgml = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
  2590. if (sgml != NULL)
  2591. ret = xmlStrdup(sgml);
  2592. }
  2593. return(ret);
  2594. }
  2595. /**
  2596. * xmlACatalogResolvePublic:
  2597. * @catal: a Catalog
  2598. * @pubID: the public ID string
  2599. *
  2600. * Try to lookup the catalog local reference associated to a public ID in that catalog
  2601. *
  2602. * Returns the local resource if found or NULL otherwise, the value returned
  2603. * must be freed by the caller.
  2604. */
  2605. xmlChar *
  2606. xmlACatalogResolvePublic(xmlCatalogPtr catal, const xmlChar *pubID) {
  2607. xmlChar *ret = NULL;
  2608. if ((pubID == NULL) || (catal == NULL))
  2609. return(NULL);
  2610. if (xmlDebugCatalogs)
  2611. xmlGenericError(xmlGenericErrorContext,
  2612. "Resolve pubID %s\n", pubID);
  2613. if (catal->type == XML_XML_CATALOG_TYPE) {
  2614. ret = xmlCatalogListXMLResolve(catal->xml, pubID, NULL);
  2615. if (ret == XML_CATAL_BREAK)
  2616. ret = NULL;
  2617. } else {
  2618. const xmlChar *sgml;
  2619. sgml = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
  2620. if (sgml != NULL)
  2621. ret = xmlStrdup(sgml);
  2622. }
  2623. return(ret);
  2624. }
  2625. /**
  2626. * xmlACatalogResolve:
  2627. * @catal: a Catalog
  2628. * @pubID: the public ID string
  2629. * @sysID: the system ID string
  2630. *
  2631. * Do a complete resolution lookup of an External Identifier
  2632. *
  2633. * Returns the URI of the resource or NULL if not found, it must be freed
  2634. * by the caller.
  2635. */
  2636. xmlChar *
  2637. xmlACatalogResolve(xmlCatalogPtr catal, const xmlChar * pubID,
  2638. const xmlChar * sysID)
  2639. {
  2640. xmlChar *ret = NULL;
  2641. if (((pubID == NULL) && (sysID == NULL)) || (catal == NULL))
  2642. return (NULL);
  2643. if (xmlDebugCatalogs) {
  2644. if ((pubID != NULL) && (sysID != NULL)) {
  2645. xmlGenericError(xmlGenericErrorContext,
  2646. "Resolve: pubID %s sysID %s\n", pubID, sysID);
  2647. } else if (pubID != NULL) {
  2648. xmlGenericError(xmlGenericErrorContext,
  2649. "Resolve: pubID %s\n", pubID);
  2650. } else {
  2651. xmlGenericError(xmlGenericErrorContext,
  2652. "Resolve: sysID %s\n", sysID);
  2653. }
  2654. }
  2655. if (catal->type == XML_XML_CATALOG_TYPE) {
  2656. ret = xmlCatalogListXMLResolve(catal->xml, pubID, sysID);
  2657. if (ret == XML_CATAL_BREAK)
  2658. ret = NULL;
  2659. } else {
  2660. const xmlChar *sgml;
  2661. sgml = xmlCatalogSGMLResolve(catal, pubID, sysID);
  2662. if (sgml != NULL)
  2663. ret = xmlStrdup(sgml);
  2664. }
  2665. return (ret);
  2666. }
  2667. /**
  2668. * xmlACatalogResolveURI:
  2669. * @catal: a Catalog
  2670. * @URI: the URI
  2671. *
  2672. * Do a complete resolution lookup of an URI
  2673. *
  2674. * Returns the URI of the resource or NULL if not found, it must be freed
  2675. * by the caller.
  2676. */
  2677. xmlChar *
  2678. xmlACatalogResolveURI(xmlCatalogPtr catal, const xmlChar *URI) {
  2679. xmlChar *ret = NULL;
  2680. if ((URI == NULL) || (catal == NULL))
  2681. return(NULL);
  2682. if (xmlDebugCatalogs)
  2683. xmlGenericError(xmlGenericErrorContext,
  2684. "Resolve URI %s\n", URI);
  2685. if (catal->type == XML_XML_CATALOG_TYPE) {
  2686. ret = xmlCatalogListXMLResolveURI(catal->xml, URI);
  2687. if (ret == XML_CATAL_BREAK)
  2688. ret = NULL;
  2689. } else {
  2690. const xmlChar *sgml;
  2691. sgml = xmlCatalogSGMLResolve(catal, NULL, URI);
  2692. if (sgml != NULL)
  2693. ret = xmlStrdup(sgml);
  2694. }
  2695. return(ret);
  2696. }
  2697. #ifdef LIBXML_OUTPUT_ENABLED
  2698. /**
  2699. * xmlACatalogDump:
  2700. * @catal: a Catalog
  2701. * @out: the file.
  2702. *
  2703. * Dump the given catalog to the given file.
  2704. */
  2705. void
  2706. xmlACatalogDump(xmlCatalogPtr catal, FILE *out) {
  2707. if ((out == NULL) || (catal == NULL))
  2708. return;
  2709. if (catal->type == XML_XML_CATALOG_TYPE) {
  2710. xmlDumpXMLCatalog(out, catal->xml);
  2711. } else {
  2712. xmlHashScan(catal->sgml, xmlCatalogDumpEntry, out);
  2713. }
  2714. }
  2715. #endif /* LIBXML_OUTPUT_ENABLED */
  2716. /**
  2717. * xmlACatalogAdd:
  2718. * @catal: a Catalog
  2719. * @type: the type of record to add to the catalog
  2720. * @orig: the system, public or prefix to match
  2721. * @replace: the replacement value for the match
  2722. *
  2723. * Add an entry in the catalog, it may overwrite existing but
  2724. * different entries.
  2725. *
  2726. * Returns 0 if successful, -1 otherwise
  2727. */
  2728. int
  2729. xmlACatalogAdd(xmlCatalogPtr catal, const xmlChar * type,
  2730. const xmlChar * orig, const xmlChar * replace)
  2731. {
  2732. int res = -1;
  2733. if (catal == NULL)
  2734. return(-1);
  2735. if (catal->type == XML_XML_CATALOG_TYPE) {
  2736. res = xmlAddXMLCatalog(catal->xml, type, orig, replace);
  2737. } else {
  2738. xmlCatalogEntryType cattype;
  2739. cattype = xmlGetSGMLCatalogEntryType(type);
  2740. if (cattype != XML_CATA_NONE) {
  2741. xmlCatalogEntryPtr entry;
  2742. entry = xmlNewCatalogEntry(cattype, orig, replace, NULL,
  2743. XML_CATA_PREFER_NONE, NULL);
  2744. if (catal->sgml == NULL)
  2745. catal->sgml = xmlHashCreate(10);
  2746. res = xmlHashAddEntry(catal->sgml, orig, entry);
  2747. if (res < 0)
  2748. xmlFreeCatalogEntry(entry, NULL);
  2749. }
  2750. }
  2751. return (res);
  2752. }
  2753. /**
  2754. * xmlACatalogRemove:
  2755. * @catal: a Catalog
  2756. * @value: the value to remove
  2757. *
  2758. * Remove an entry from the catalog
  2759. *
  2760. * Returns the number of entries removed if successful, -1 otherwise
  2761. */
  2762. int
  2763. xmlACatalogRemove(xmlCatalogPtr catal, const xmlChar *value) {
  2764. int res = -1;
  2765. if ((catal == NULL) || (value == NULL))
  2766. return(-1);
  2767. if (catal->type == XML_XML_CATALOG_TYPE) {
  2768. res = xmlDelXMLCatalog(catal->xml, value);
  2769. } else {
  2770. res = xmlHashRemoveEntry(catal->sgml, value, xmlFreeCatalogEntry);
  2771. if (res == 0)
  2772. res = 1;
  2773. }
  2774. return(res);
  2775. }
  2776. /**
  2777. * xmlNewCatalog:
  2778. * @sgml: should this create an SGML catalog
  2779. *
  2780. * create a new Catalog.
  2781. *
  2782. * Returns the xmlCatalogPtr or NULL in case of error
  2783. */
  2784. xmlCatalogPtr
  2785. xmlNewCatalog(int sgml) {
  2786. xmlCatalogPtr catal = NULL;
  2787. if (sgml) {
  2788. catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE,
  2789. xmlCatalogDefaultPrefer);
  2790. if ((catal != NULL) && (catal->sgml == NULL))
  2791. catal->sgml = xmlHashCreate(10);
  2792. } else
  2793. catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
  2794. xmlCatalogDefaultPrefer);
  2795. return(catal);
  2796. }
  2797. /**
  2798. * xmlCatalogIsEmpty:
  2799. * @catal: should this create an SGML catalog
  2800. *
  2801. * Check is a catalog is empty
  2802. *
  2803. * Returns 1 if the catalog is empty, 0 if not, amd -1 in case of error.
  2804. */
  2805. int
  2806. xmlCatalogIsEmpty(xmlCatalogPtr catal) {
  2807. if (catal == NULL)
  2808. return(-1);
  2809. if (catal->type == XML_XML_CATALOG_TYPE) {
  2810. if (catal->xml == NULL)
  2811. return(1);
  2812. if ((catal->xml->type != XML_CATA_CATALOG) &&
  2813. (catal->xml->type != XML_CATA_BROKEN_CATALOG))
  2814. return(-1);
  2815. if (catal->xml->children == NULL)
  2816. return(1);
  2817. return(0);
  2818. } else {
  2819. int res;
  2820. if (catal->sgml == NULL)
  2821. return(1);
  2822. res = xmlHashSize(catal->sgml);
  2823. if (res == 0)
  2824. return(1);
  2825. if (res < 0)
  2826. return(-1);
  2827. }
  2828. return(0);
  2829. }
  2830. /************************************************************************
  2831. * *
  2832. * Public interfaces manipulating the global shared default catalog *
  2833. * *
  2834. ************************************************************************/
  2835. /**
  2836. * xmlInitializeCatalogData:
  2837. *
  2838. * Do the catalog initialization only of global data, doesn't try to load
  2839. * any catalog actually.
  2840. * this function is not thread safe, catalog initialization should
  2841. * preferably be done once at startup
  2842. */
  2843. static void
  2844. xmlInitializeCatalogData(void) {
  2845. if (xmlCatalogInitialized != 0)
  2846. return;
  2847. if (getenv("XML_DEBUG_CATALOG"))
  2848. xmlDebugCatalogs = 1;
  2849. xmlCatalogMutex = xmlNewRMutex();
  2850. xmlCatalogInitialized = 1;
  2851. }
  2852. /**
  2853. * xmlInitializeCatalog:
  2854. *
  2855. * Do the catalog initialization.
  2856. * this function is not thread safe, catalog initialization should
  2857. * preferably be done once at startup
  2858. */
  2859. void
  2860. xmlInitializeCatalog(void) {
  2861. if (xmlCatalogInitialized != 0)
  2862. return;
  2863. xmlInitializeCatalogData();
  2864. xmlRMutexLock(xmlCatalogMutex);
  2865. if (getenv("XML_DEBUG_CATALOG"))
  2866. xmlDebugCatalogs = 1;
  2867. if (xmlDefaultCatalog == NULL) {
  2868. const char *catalogs;
  2869. char *path;
  2870. const char *cur, *paths;
  2871. xmlCatalogPtr catal;
  2872. xmlCatalogEntryPtr *nextent;
  2873. catalogs = (const char *) getenv("XML_CATALOG_FILES");
  2874. if (catalogs == NULL)
  2875. #if defined(_WIN32) && defined(_MSC_VER)
  2876. {
  2877. void* hmodule;
  2878. hmodule = GetModuleHandleA("libxml2.dll");
  2879. if (hmodule == NULL)
  2880. hmodule = GetModuleHandleA(NULL);
  2881. if (hmodule != NULL) {
  2882. char buf[256];
  2883. unsigned long len = GetModuleFileNameA(hmodule, buf, 255);
  2884. if (len != 0) {
  2885. char* p = &(buf[len]);
  2886. while (*p != '\\' && p > buf)
  2887. p--;
  2888. if (p != buf) {
  2889. xmlChar* uri;
  2890. strncpy(p, "\\..\\etc\\catalog", 255 - (p - buf));
  2891. uri = xmlCanonicPath((const xmlChar*)buf);
  2892. if (uri != NULL) {
  2893. strncpy(XML_XML_DEFAULT_CATALOG, (char* )uri, 255);
  2894. xmlFree(uri);
  2895. }
  2896. }
  2897. }
  2898. }
  2899. catalogs = XML_XML_DEFAULT_CATALOG;
  2900. }
  2901. #else
  2902. catalogs = XML_XML_DEFAULT_CATALOG;
  2903. #endif
  2904. catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
  2905. xmlCatalogDefaultPrefer);
  2906. if (catal != NULL) {
  2907. /* the XML_CATALOG_FILES envvar is allowed to contain a
  2908. space-separated list of entries. */
  2909. cur = catalogs;
  2910. nextent = &catal->xml;
  2911. while (*cur != '\0') {
  2912. while (xmlIsBlank_ch(*cur))
  2913. cur++;
  2914. if (*cur != 0) {
  2915. paths = cur;
  2916. while ((*cur != 0) && (!xmlIsBlank_ch(*cur)))
  2917. cur++;
  2918. path = (char *) xmlStrndup((const xmlChar *)paths, cur - paths);
  2919. if (path != NULL) {
  2920. *nextent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
  2921. NULL, BAD_CAST path, xmlCatalogDefaultPrefer, NULL);
  2922. if (*nextent != NULL)
  2923. nextent = &((*nextent)->next);
  2924. xmlFree(path);
  2925. }
  2926. }
  2927. }
  2928. xmlDefaultCatalog = catal;
  2929. }
  2930. }
  2931. xmlRMutexUnlock(xmlCatalogMutex);
  2932. }
  2933. /**
  2934. * xmlLoadCatalog:
  2935. * @filename: a file path
  2936. *
  2937. * Load the catalog and makes its definitions effective for the default
  2938. * external entity loader. It will recurse in SGML CATALOG entries.
  2939. * this function is not thread safe, catalog initialization should
  2940. * preferably be done once at startup
  2941. *
  2942. * Returns 0 in case of success -1 in case of error
  2943. */
  2944. int
  2945. xmlLoadCatalog(const char *filename)
  2946. {
  2947. int ret;
  2948. xmlCatalogPtr catal;
  2949. if (!xmlCatalogInitialized)
  2950. xmlInitializeCatalogData();
  2951. xmlRMutexLock(xmlCatalogMutex);
  2952. if (xmlDefaultCatalog == NULL) {
  2953. catal = xmlLoadACatalog(filename);
  2954. if (catal == NULL) {
  2955. xmlRMutexUnlock(xmlCatalogMutex);
  2956. return(-1);
  2957. }
  2958. xmlDefaultCatalog = catal;
  2959. xmlRMutexUnlock(xmlCatalogMutex);
  2960. return(0);
  2961. }
  2962. ret = xmlExpandCatalog(xmlDefaultCatalog, filename);
  2963. xmlRMutexUnlock(xmlCatalogMutex);
  2964. return(ret);
  2965. }
  2966. /**
  2967. * xmlLoadCatalogs:
  2968. * @pathss: a list of directories separated by a colon or a space.
  2969. *
  2970. * Load the catalogs and makes their definitions effective for the default
  2971. * external entity loader.
  2972. * this function is not thread safe, catalog initialization should
  2973. * preferably be done once at startup
  2974. */
  2975. void
  2976. xmlLoadCatalogs(const char *pathss) {
  2977. const char *cur;
  2978. const char *paths;
  2979. xmlChar *path;
  2980. #ifdef _WIN32
  2981. int i, iLen;
  2982. #endif
  2983. if (pathss == NULL)
  2984. return;
  2985. cur = pathss;
  2986. while (*cur != 0) {
  2987. while (xmlIsBlank_ch(*cur)) cur++;
  2988. if (*cur != 0) {
  2989. paths = cur;
  2990. while ((*cur != 0) && (*cur != PATH_SEPARATOR) && (!xmlIsBlank_ch(*cur)))
  2991. cur++;
  2992. path = xmlStrndup((const xmlChar *)paths, cur - paths);
  2993. if (path != NULL) {
  2994. #ifdef _WIN32
  2995. iLen = strlen((const char*)path);
  2996. for(i = 0; i < iLen; i++) {
  2997. if(path[i] == '\\') {
  2998. path[i] = '/';
  2999. }
  3000. }
  3001. #endif
  3002. xmlLoadCatalog((const char *) path);
  3003. xmlFree(path);
  3004. }
  3005. }
  3006. while (*cur == PATH_SEPARATOR)
  3007. cur++;
  3008. }
  3009. }
  3010. /**
  3011. * xmlCatalogCleanup:
  3012. *
  3013. * Free up all the memory associated with catalogs
  3014. */
  3015. void
  3016. xmlCatalogCleanup(void) {
  3017. if (xmlCatalogInitialized == 0)
  3018. return;
  3019. xmlRMutexLock(xmlCatalogMutex);
  3020. if (xmlDebugCatalogs)
  3021. xmlGenericError(xmlGenericErrorContext,
  3022. "Catalogs cleanup\n");
  3023. if (xmlCatalogXMLFiles != NULL)
  3024. xmlHashFree(xmlCatalogXMLFiles, xmlFreeCatalogHashEntryList);
  3025. xmlCatalogXMLFiles = NULL;
  3026. if (xmlDefaultCatalog != NULL)
  3027. xmlFreeCatalog(xmlDefaultCatalog);
  3028. xmlDefaultCatalog = NULL;
  3029. xmlDebugCatalogs = 0;
  3030. xmlCatalogInitialized = 0;
  3031. xmlRMutexUnlock(xmlCatalogMutex);
  3032. xmlFreeRMutex(xmlCatalogMutex);
  3033. }
  3034. /**
  3035. * xmlCatalogResolveSystem:
  3036. * @sysID: the system ID string
  3037. *
  3038. * Try to lookup the catalog resource for a system ID
  3039. *
  3040. * Returns the resource if found or NULL otherwise, the value returned
  3041. * must be freed by the caller.
  3042. */
  3043. xmlChar *
  3044. xmlCatalogResolveSystem(const xmlChar *sysID) {
  3045. xmlChar *ret;
  3046. if (!xmlCatalogInitialized)
  3047. xmlInitializeCatalog();
  3048. ret = xmlACatalogResolveSystem(xmlDefaultCatalog, sysID);
  3049. return(ret);
  3050. }
  3051. /**
  3052. * xmlCatalogResolvePublic:
  3053. * @pubID: the public ID string
  3054. *
  3055. * Try to lookup the catalog reference associated to a public ID
  3056. *
  3057. * Returns the resource if found or NULL otherwise, the value returned
  3058. * must be freed by the caller.
  3059. */
  3060. xmlChar *
  3061. xmlCatalogResolvePublic(const xmlChar *pubID) {
  3062. xmlChar *ret;
  3063. if (!xmlCatalogInitialized)
  3064. xmlInitializeCatalog();
  3065. ret = xmlACatalogResolvePublic(xmlDefaultCatalog, pubID);
  3066. return(ret);
  3067. }
  3068. /**
  3069. * xmlCatalogResolve:
  3070. * @pubID: the public ID string
  3071. * @sysID: the system ID string
  3072. *
  3073. * Do a complete resolution lookup of an External Identifier
  3074. *
  3075. * Returns the URI of the resource or NULL if not found, it must be freed
  3076. * by the caller.
  3077. */
  3078. xmlChar *
  3079. xmlCatalogResolve(const xmlChar *pubID, const xmlChar *sysID) {
  3080. xmlChar *ret;
  3081. if (!xmlCatalogInitialized)
  3082. xmlInitializeCatalog();
  3083. ret = xmlACatalogResolve(xmlDefaultCatalog, pubID, sysID);
  3084. return(ret);
  3085. }
  3086. /**
  3087. * xmlCatalogResolveURI:
  3088. * @URI: the URI
  3089. *
  3090. * Do a complete resolution lookup of an URI
  3091. *
  3092. * Returns the URI of the resource or NULL if not found, it must be freed
  3093. * by the caller.
  3094. */
  3095. xmlChar *
  3096. xmlCatalogResolveURI(const xmlChar *URI) {
  3097. xmlChar *ret;
  3098. if (!xmlCatalogInitialized)
  3099. xmlInitializeCatalog();
  3100. ret = xmlACatalogResolveURI(xmlDefaultCatalog, URI);
  3101. return(ret);
  3102. }
  3103. #ifdef LIBXML_OUTPUT_ENABLED
  3104. /**
  3105. * xmlCatalogDump:
  3106. * @out: the file.
  3107. *
  3108. * Dump all the global catalog content to the given file.
  3109. */
  3110. void
  3111. xmlCatalogDump(FILE *out) {
  3112. if (out == NULL)
  3113. return;
  3114. if (!xmlCatalogInitialized)
  3115. xmlInitializeCatalog();
  3116. xmlACatalogDump(xmlDefaultCatalog, out);
  3117. }
  3118. #endif /* LIBXML_OUTPUT_ENABLED */
  3119. /**
  3120. * xmlCatalogAdd:
  3121. * @type: the type of record to add to the catalog
  3122. * @orig: the system, public or prefix to match
  3123. * @replace: the replacement value for the match
  3124. *
  3125. * Add an entry in the catalog, it may overwrite existing but
  3126. * different entries.
  3127. * If called before any other catalog routine, allows to override the
  3128. * default shared catalog put in place by xmlInitializeCatalog();
  3129. *
  3130. * Returns 0 if successful, -1 otherwise
  3131. */
  3132. int
  3133. xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace) {
  3134. int res = -1;
  3135. if (!xmlCatalogInitialized)
  3136. xmlInitializeCatalogData();
  3137. xmlRMutexLock(xmlCatalogMutex);
  3138. /*
  3139. * Specific case where one want to override the default catalog
  3140. * put in place by xmlInitializeCatalog();
  3141. */
  3142. if ((xmlDefaultCatalog == NULL) &&
  3143. (xmlStrEqual(type, BAD_CAST "catalog"))) {
  3144. xmlDefaultCatalog = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
  3145. xmlCatalogDefaultPrefer);
  3146. if (xmlDefaultCatalog != NULL) {
  3147. xmlDefaultCatalog->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
  3148. orig, NULL, xmlCatalogDefaultPrefer, NULL);
  3149. }
  3150. xmlRMutexUnlock(xmlCatalogMutex);
  3151. return(0);
  3152. }
  3153. res = xmlACatalogAdd(xmlDefaultCatalog, type, orig, replace);
  3154. xmlRMutexUnlock(xmlCatalogMutex);
  3155. return(res);
  3156. }
  3157. /**
  3158. * xmlCatalogRemove:
  3159. * @value: the value to remove
  3160. *
  3161. * Remove an entry from the catalog
  3162. *
  3163. * Returns the number of entries removed if successful, -1 otherwise
  3164. */
  3165. int
  3166. xmlCatalogRemove(const xmlChar *value) {
  3167. int res;
  3168. if (!xmlCatalogInitialized)
  3169. xmlInitializeCatalog();
  3170. xmlRMutexLock(xmlCatalogMutex);
  3171. res = xmlACatalogRemove(xmlDefaultCatalog, value);
  3172. xmlRMutexUnlock(xmlCatalogMutex);
  3173. return(res);
  3174. }
  3175. /**
  3176. * xmlCatalogConvert:
  3177. *
  3178. * Convert all the SGML catalog entries as XML ones
  3179. *
  3180. * Returns the number of entries converted if successful, -1 otherwise
  3181. */
  3182. int
  3183. xmlCatalogConvert(void) {
  3184. int res = -1;
  3185. if (!xmlCatalogInitialized)
  3186. xmlInitializeCatalog();
  3187. xmlRMutexLock(xmlCatalogMutex);
  3188. res = xmlConvertSGMLCatalog(xmlDefaultCatalog);
  3189. xmlRMutexUnlock(xmlCatalogMutex);
  3190. return(res);
  3191. }
  3192. /************************************************************************
  3193. * *
  3194. * Public interface manipulating the common preferences *
  3195. * *
  3196. ************************************************************************/
  3197. /**
  3198. * xmlCatalogGetDefaults:
  3199. *
  3200. * Used to get the user preference w.r.t. to what catalogs should
  3201. * be accepted
  3202. *
  3203. * Returns the current xmlCatalogAllow value
  3204. */
  3205. xmlCatalogAllow
  3206. xmlCatalogGetDefaults(void) {
  3207. return(xmlCatalogDefaultAllow);
  3208. }
  3209. /**
  3210. * xmlCatalogSetDefaults:
  3211. * @allow: what catalogs should be accepted
  3212. *
  3213. * Used to set the user preference w.r.t. to what catalogs should
  3214. * be accepted
  3215. */
  3216. void
  3217. xmlCatalogSetDefaults(xmlCatalogAllow allow) {
  3218. if (xmlDebugCatalogs) {
  3219. switch (allow) {
  3220. case XML_CATA_ALLOW_NONE:
  3221. xmlGenericError(xmlGenericErrorContext,
  3222. "Disabling catalog usage\n");
  3223. break;
  3224. case XML_CATA_ALLOW_GLOBAL:
  3225. xmlGenericError(xmlGenericErrorContext,
  3226. "Allowing only global catalogs\n");
  3227. break;
  3228. case XML_CATA_ALLOW_DOCUMENT:
  3229. xmlGenericError(xmlGenericErrorContext,
  3230. "Allowing only catalogs from the document\n");
  3231. break;
  3232. case XML_CATA_ALLOW_ALL:
  3233. xmlGenericError(xmlGenericErrorContext,
  3234. "Allowing all catalogs\n");
  3235. break;
  3236. }
  3237. }
  3238. xmlCatalogDefaultAllow = allow;
  3239. }
  3240. /**
  3241. * xmlCatalogSetDefaultPrefer:
  3242. * @prefer: the default preference for delegation
  3243. *
  3244. * Allows to set the preference between public and system for deletion
  3245. * in XML Catalog resolution. C.f. section 4.1.1 of the spec
  3246. * Values accepted are XML_CATA_PREFER_PUBLIC or XML_CATA_PREFER_SYSTEM
  3247. *
  3248. * Returns the previous value of the default preference for delegation
  3249. */
  3250. xmlCatalogPrefer
  3251. xmlCatalogSetDefaultPrefer(xmlCatalogPrefer prefer) {
  3252. xmlCatalogPrefer ret = xmlCatalogDefaultPrefer;
  3253. if (prefer == XML_CATA_PREFER_NONE)
  3254. return(ret);
  3255. if (xmlDebugCatalogs) {
  3256. switch (prefer) {
  3257. case XML_CATA_PREFER_PUBLIC:
  3258. xmlGenericError(xmlGenericErrorContext,
  3259. "Setting catalog preference to PUBLIC\n");
  3260. break;
  3261. case XML_CATA_PREFER_SYSTEM:
  3262. xmlGenericError(xmlGenericErrorContext,
  3263. "Setting catalog preference to SYSTEM\n");
  3264. break;
  3265. default:
  3266. return(ret);
  3267. }
  3268. }
  3269. xmlCatalogDefaultPrefer = prefer;
  3270. return(ret);
  3271. }
  3272. /**
  3273. * xmlCatalogSetDebug:
  3274. * @level: the debug level of catalogs required
  3275. *
  3276. * Used to set the debug level for catalog operation, 0 disable
  3277. * debugging, 1 enable it
  3278. *
  3279. * Returns the previous value of the catalog debugging level
  3280. */
  3281. int
  3282. xmlCatalogSetDebug(int level) {
  3283. int ret = xmlDebugCatalogs;
  3284. if (level <= 0)
  3285. xmlDebugCatalogs = 0;
  3286. else
  3287. xmlDebugCatalogs = level;
  3288. return(ret);
  3289. }
  3290. /************************************************************************
  3291. * *
  3292. * Minimal interfaces used for per-document catalogs by the parser *
  3293. * *
  3294. ************************************************************************/
  3295. /**
  3296. * xmlCatalogFreeLocal:
  3297. * @catalogs: a document's list of catalogs
  3298. *
  3299. * Free up the memory associated to the catalog list
  3300. */
  3301. void
  3302. xmlCatalogFreeLocal(void *catalogs) {
  3303. xmlCatalogEntryPtr catal;
  3304. if (!xmlCatalogInitialized)
  3305. xmlInitializeCatalog();
  3306. catal = (xmlCatalogEntryPtr) catalogs;
  3307. if (catal != NULL)
  3308. xmlFreeCatalogEntryList(catal);
  3309. }
  3310. /**
  3311. * xmlCatalogAddLocal:
  3312. * @catalogs: a document's list of catalogs
  3313. * @URL: the URL to a new local catalog
  3314. *
  3315. * Add the new entry to the catalog list
  3316. *
  3317. * Returns the updated list
  3318. */
  3319. void *
  3320. xmlCatalogAddLocal(void *catalogs, const xmlChar *URL) {
  3321. xmlCatalogEntryPtr catal, add;
  3322. if (!xmlCatalogInitialized)
  3323. xmlInitializeCatalog();
  3324. if (URL == NULL)
  3325. return(catalogs);
  3326. if (xmlDebugCatalogs)
  3327. xmlGenericError(xmlGenericErrorContext,
  3328. "Adding document catalog %s\n", URL);
  3329. add = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, URL, NULL,
  3330. xmlCatalogDefaultPrefer, NULL);
  3331. if (add == NULL)
  3332. return(catalogs);
  3333. catal = (xmlCatalogEntryPtr) catalogs;
  3334. if (catal == NULL)
  3335. return((void *) add);
  3336. while (catal->next != NULL)
  3337. catal = catal->next;
  3338. catal->next = add;
  3339. return(catalogs);
  3340. }
  3341. /**
  3342. * xmlCatalogLocalResolve:
  3343. * @catalogs: a document's list of catalogs
  3344. * @pubID: the public ID string
  3345. * @sysID: the system ID string
  3346. *
  3347. * Do a complete resolution lookup of an External Identifier using a
  3348. * document's private catalog list
  3349. *
  3350. * Returns the URI of the resource or NULL if not found, it must be freed
  3351. * by the caller.
  3352. */
  3353. xmlChar *
  3354. xmlCatalogLocalResolve(void *catalogs, const xmlChar *pubID,
  3355. const xmlChar *sysID) {
  3356. xmlCatalogEntryPtr catal;
  3357. xmlChar *ret;
  3358. if (!xmlCatalogInitialized)
  3359. xmlInitializeCatalog();
  3360. if ((pubID == NULL) && (sysID == NULL))
  3361. return(NULL);
  3362. if (xmlDebugCatalogs) {
  3363. if ((pubID != NULL) && (sysID != NULL)) {
  3364. xmlGenericError(xmlGenericErrorContext,
  3365. "Local Resolve: pubID %s sysID %s\n", pubID, sysID);
  3366. } else if (pubID != NULL) {
  3367. xmlGenericError(xmlGenericErrorContext,
  3368. "Local Resolve: pubID %s\n", pubID);
  3369. } else {
  3370. xmlGenericError(xmlGenericErrorContext,
  3371. "Local Resolve: sysID %s\n", sysID);
  3372. }
  3373. }
  3374. catal = (xmlCatalogEntryPtr) catalogs;
  3375. if (catal == NULL)
  3376. return(NULL);
  3377. ret = xmlCatalogListXMLResolve(catal, pubID, sysID);
  3378. if ((ret != NULL) && (ret != XML_CATAL_BREAK))
  3379. return(ret);
  3380. return(NULL);
  3381. }
  3382. /**
  3383. * xmlCatalogLocalResolveURI:
  3384. * @catalogs: a document's list of catalogs
  3385. * @URI: the URI
  3386. *
  3387. * Do a complete resolution lookup of an URI using a
  3388. * document's private catalog list
  3389. *
  3390. * Returns the URI of the resource or NULL if not found, it must be freed
  3391. * by the caller.
  3392. */
  3393. xmlChar *
  3394. xmlCatalogLocalResolveURI(void *catalogs, const xmlChar *URI) {
  3395. xmlCatalogEntryPtr catal;
  3396. xmlChar *ret;
  3397. if (!xmlCatalogInitialized)
  3398. xmlInitializeCatalog();
  3399. if (URI == NULL)
  3400. return(NULL);
  3401. if (xmlDebugCatalogs)
  3402. xmlGenericError(xmlGenericErrorContext,
  3403. "Resolve URI %s\n", URI);
  3404. catal = (xmlCatalogEntryPtr) catalogs;
  3405. if (catal == NULL)
  3406. return(NULL);
  3407. ret = xmlCatalogListXMLResolveURI(catal, URI);
  3408. if ((ret != NULL) && (ret != XML_CATAL_BREAK))
  3409. return(ret);
  3410. return(NULL);
  3411. }
  3412. /************************************************************************
  3413. * *
  3414. * Deprecated interfaces *
  3415. * *
  3416. ************************************************************************/
  3417. /**
  3418. * xmlCatalogGetSystem:
  3419. * @sysID: the system ID string
  3420. *
  3421. * Try to lookup the catalog reference associated to a system ID
  3422. * DEPRECATED, use xmlCatalogResolveSystem()
  3423. *
  3424. * Returns the resource if found or NULL otherwise.
  3425. */
  3426. const xmlChar *
  3427. xmlCatalogGetSystem(const xmlChar *sysID) {
  3428. xmlChar *ret;
  3429. static xmlChar result[1000];
  3430. static int msg = 0;
  3431. if (!xmlCatalogInitialized)
  3432. xmlInitializeCatalog();
  3433. if (msg == 0) {
  3434. xmlGenericError(xmlGenericErrorContext,
  3435. "Use of deprecated xmlCatalogGetSystem() call\n");
  3436. msg++;
  3437. }
  3438. if (sysID == NULL)
  3439. return(NULL);
  3440. /*
  3441. * Check first the XML catalogs
  3442. */
  3443. if (xmlDefaultCatalog != NULL) {
  3444. ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, NULL, sysID);
  3445. if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
  3446. snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
  3447. result[sizeof(result) - 1] = 0;
  3448. return(result);
  3449. }
  3450. }
  3451. if (xmlDefaultCatalog != NULL)
  3452. return(xmlCatalogGetSGMLSystem(xmlDefaultCatalog->sgml, sysID));
  3453. return(NULL);
  3454. }
  3455. /**
  3456. * xmlCatalogGetPublic:
  3457. * @pubID: the public ID string
  3458. *
  3459. * Try to lookup the catalog reference associated to a public ID
  3460. * DEPRECATED, use xmlCatalogResolvePublic()
  3461. *
  3462. * Returns the resource if found or NULL otherwise.
  3463. */
  3464. const xmlChar *
  3465. xmlCatalogGetPublic(const xmlChar *pubID) {
  3466. xmlChar *ret;
  3467. static xmlChar result[1000];
  3468. static int msg = 0;
  3469. if (!xmlCatalogInitialized)
  3470. xmlInitializeCatalog();
  3471. if (msg == 0) {
  3472. xmlGenericError(xmlGenericErrorContext,
  3473. "Use of deprecated xmlCatalogGetPublic() call\n");
  3474. msg++;
  3475. }
  3476. if (pubID == NULL)
  3477. return(NULL);
  3478. /*
  3479. * Check first the XML catalogs
  3480. */
  3481. if (xmlDefaultCatalog != NULL) {
  3482. ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, pubID, NULL);
  3483. if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
  3484. snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
  3485. result[sizeof(result) - 1] = 0;
  3486. return(result);
  3487. }
  3488. }
  3489. if (xmlDefaultCatalog != NULL)
  3490. return(xmlCatalogGetSGMLPublic(xmlDefaultCatalog->sgml, pubID));
  3491. return(NULL);
  3492. }
  3493. #endif /* LIBXML_CATALOG_ENABLED */