catalog.c 97 KB

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