xmlIO.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990
  1. /*
  2. * xmlIO.c : implementation of the I/O interfaces used by the parser
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * daniel@veillard.com
  7. *
  8. * 14 Nov 2000 ht - for VMS, truncated name of long functions to under 32 char
  9. */
  10. #define IN_LIBXML
  11. #include "libxml.h"
  12. #include <string.h>
  13. #ifdef HAVE_ERRNO_H
  14. #include <errno.h>
  15. #endif
  16. #ifdef HAVE_SYS_TYPES_H
  17. #include <sys/types.h>
  18. #endif
  19. #ifdef HAVE_SYS_STAT_H
  20. #include <sys/stat.h>
  21. #endif
  22. #ifdef HAVE_FCNTL_H
  23. #include <fcntl.h>
  24. #endif
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #ifdef HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #endif
  31. #ifdef HAVE_ZLIB_H
  32. #include <zlib.h>
  33. #endif
  34. #if defined(WIN32) || defined(_WIN32)
  35. #include <windows.h>
  36. #endif
  37. #if defined(_WIN32_WCE)
  38. #include <winnls.h> /* for CP_UTF8 */
  39. #endif
  40. /* Figure a portable way to know if a file is a directory. */
  41. #ifndef HAVE_STAT
  42. # ifdef HAVE__STAT
  43. /* MS C library seems to define stat and _stat. The definition
  44. is identical. Still, mapping them to each other causes a warning. */
  45. # ifndef _MSC_VER
  46. # define stat(x,y) _stat(x,y)
  47. # endif
  48. # define HAVE_STAT
  49. # endif
  50. #else
  51. # ifdef HAVE__STAT
  52. # if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  53. # define stat _stat
  54. # endif
  55. # endif
  56. #endif
  57. #ifdef HAVE_STAT
  58. # ifndef S_ISDIR
  59. # ifdef _S_ISDIR
  60. # define S_ISDIR(x) _S_ISDIR(x)
  61. # else
  62. # ifdef S_IFDIR
  63. # ifndef S_IFMT
  64. # ifdef _S_IFMT
  65. # define S_IFMT _S_IFMT
  66. # endif
  67. # endif
  68. # ifdef S_IFMT
  69. # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  70. # endif
  71. # endif
  72. # endif
  73. # endif
  74. #endif
  75. #include <libxml/xmlmemory.h>
  76. #include <libxml/parser.h>
  77. #include <libxml/parserInternals.h>
  78. #include <libxml/xmlIO.h>
  79. #include <libxml/uri.h>
  80. #include <libxml/nanohttp.h>
  81. #include <libxml/nanoftp.h>
  82. #include <libxml/xmlerror.h>
  83. #ifdef LIBXML_CATALOG_ENABLED
  84. #include <libxml/catalog.h>
  85. #endif
  86. #include <libxml/globals.h>
  87. /* #define VERBOSE_FAILURE */
  88. /* #define DEBUG_EXTERNAL_ENTITIES */
  89. /* #define DEBUG_INPUT */
  90. #ifdef DEBUG_INPUT
  91. #define MINLEN 40
  92. #else
  93. #define MINLEN 4000
  94. #endif
  95. /*
  96. * Input I/O callback sets
  97. */
  98. typedef struct _xmlInputCallback {
  99. xmlInputMatchCallback matchcallback;
  100. xmlInputOpenCallback opencallback;
  101. xmlInputReadCallback readcallback;
  102. xmlInputCloseCallback closecallback;
  103. } xmlInputCallback;
  104. #define MAX_INPUT_CALLBACK 15
  105. static xmlInputCallback xmlInputCallbackTable[MAX_INPUT_CALLBACK];
  106. static int xmlInputCallbackNr = 0;
  107. static int xmlInputCallbackInitialized = 0;
  108. #ifdef LIBXML_OUTPUT_ENABLED
  109. /*
  110. * Output I/O callback sets
  111. */
  112. typedef struct _xmlOutputCallback {
  113. xmlOutputMatchCallback matchcallback;
  114. xmlOutputOpenCallback opencallback;
  115. xmlOutputWriteCallback writecallback;
  116. xmlOutputCloseCallback closecallback;
  117. } xmlOutputCallback;
  118. #define MAX_OUTPUT_CALLBACK 15
  119. static xmlOutputCallback xmlOutputCallbackTable[MAX_OUTPUT_CALLBACK];
  120. static int xmlOutputCallbackNr = 0;
  121. static int xmlOutputCallbackInitialized = 0;
  122. xmlOutputBufferPtr
  123. xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder);
  124. #endif /* LIBXML_OUTPUT_ENABLED */
  125. /************************************************************************
  126. * *
  127. * Tree memory error handler *
  128. * *
  129. ************************************************************************/
  130. static const char *IOerr[] = {
  131. "Unknown IO error", /* UNKNOWN */
  132. "Permission denied", /* EACCES */
  133. "Resource temporarily unavailable",/* EAGAIN */
  134. "Bad file descriptor", /* EBADF */
  135. "Bad message", /* EBADMSG */
  136. "Resource busy", /* EBUSY */
  137. "Operation canceled", /* ECANCELED */
  138. "No child processes", /* ECHILD */
  139. "Resource deadlock avoided",/* EDEADLK */
  140. "Domain error", /* EDOM */
  141. "File exists", /* EEXIST */
  142. "Bad address", /* EFAULT */
  143. "File too large", /* EFBIG */
  144. "Operation in progress", /* EINPROGRESS */
  145. "Interrupted function call",/* EINTR */
  146. "Invalid argument", /* EINVAL */
  147. "Input/output error", /* EIO */
  148. "Is a directory", /* EISDIR */
  149. "Too many open files", /* EMFILE */
  150. "Too many links", /* EMLINK */
  151. "Inappropriate message buffer length",/* EMSGSIZE */
  152. "Filename too long", /* ENAMETOOLONG */
  153. "Too many open files in system",/* ENFILE */
  154. "No such device", /* ENODEV */
  155. "No such file or directory",/* ENOENT */
  156. "Exec format error", /* ENOEXEC */
  157. "No locks available", /* ENOLCK */
  158. "Not enough space", /* ENOMEM */
  159. "No space left on device", /* ENOSPC */
  160. "Function not implemented", /* ENOSYS */
  161. "Not a directory", /* ENOTDIR */
  162. "Directory not empty", /* ENOTEMPTY */
  163. "Not supported", /* ENOTSUP */
  164. "Inappropriate I/O control operation",/* ENOTTY */
  165. "No such device or address",/* ENXIO */
  166. "Operation not permitted", /* EPERM */
  167. "Broken pipe", /* EPIPE */
  168. "Result too large", /* ERANGE */
  169. "Read-only file system", /* EROFS */
  170. "Invalid seek", /* ESPIPE */
  171. "No such process", /* ESRCH */
  172. "Operation timed out", /* ETIMEDOUT */
  173. "Improper link", /* EXDEV */
  174. "Attempt to load network entity %s", /* XML_IO_NETWORK_ATTEMPT */
  175. "encoder error", /* XML_IO_ENCODER */
  176. "flush error",
  177. "write error",
  178. "no input",
  179. "buffer full",
  180. "loading error",
  181. "not a socket", /* ENOTSOCK */
  182. "already connected", /* EISCONN */
  183. "connection refused", /* ECONNREFUSED */
  184. "unreachable network", /* ENETUNREACH */
  185. "adddress in use", /* EADDRINUSE */
  186. "already in use", /* EALREADY */
  187. "unknown address familly", /* EAFNOSUPPORT */
  188. };
  189. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  190. /**
  191. * __xmlIOWin32UTF8ToWChar:
  192. * @u8String: uft-8 string
  193. *
  194. * Convert a string from utf-8 to wchar (WINDOWS ONLY!)
  195. */
  196. static wchar_t *
  197. __xmlIOWin32UTF8ToWChar(const char *u8String)
  198. {
  199. wchar_t *wString = NULL;
  200. if (u8String) {
  201. int wLen =
  202. MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, u8String,
  203. -1, NULL, 0);
  204. if (wLen) {
  205. wString = xmlMalloc(wLen * sizeof(wchar_t));
  206. if (wString) {
  207. if (MultiByteToWideChar
  208. (CP_UTF8, 0, u8String, -1, wString, wLen) == 0) {
  209. xmlFree(wString);
  210. wString = NULL;
  211. }
  212. }
  213. }
  214. }
  215. return wString;
  216. }
  217. #endif
  218. /**
  219. * xmlIOErrMemory:
  220. * @extra: extra informations
  221. *
  222. * Handle an out of memory condition
  223. */
  224. static void
  225. xmlIOErrMemory(const char *extra)
  226. {
  227. __xmlSimpleError(XML_FROM_IO, XML_ERR_NO_MEMORY, NULL, NULL, extra);
  228. }
  229. /**
  230. * __xmlIOErr:
  231. * @code: the error number
  232. * @
  233. * @extra: extra informations
  234. *
  235. * Handle an I/O error
  236. */
  237. void
  238. __xmlIOErr(int domain, int code, const char *extra)
  239. {
  240. unsigned int idx;
  241. if (code == 0) {
  242. #ifdef HAVE_ERRNO_H
  243. if (errno == 0) code = 0;
  244. #ifdef EACCES
  245. else if (errno == EACCES) code = XML_IO_EACCES;
  246. #endif
  247. #ifdef EAGAIN
  248. else if (errno == EAGAIN) code = XML_IO_EAGAIN;
  249. #endif
  250. #ifdef EBADF
  251. else if (errno == EBADF) code = XML_IO_EBADF;
  252. #endif
  253. #ifdef EBADMSG
  254. else if (errno == EBADMSG) code = XML_IO_EBADMSG;
  255. #endif
  256. #ifdef EBUSY
  257. else if (errno == EBUSY) code = XML_IO_EBUSY;
  258. #endif
  259. #ifdef ECANCELED
  260. else if (errno == ECANCELED) code = XML_IO_ECANCELED;
  261. #endif
  262. #ifdef ECHILD
  263. else if (errno == ECHILD) code = XML_IO_ECHILD;
  264. #endif
  265. #ifdef EDEADLK
  266. else if (errno == EDEADLK) code = XML_IO_EDEADLK;
  267. #endif
  268. #ifdef EDOM
  269. else if (errno == EDOM) code = XML_IO_EDOM;
  270. #endif
  271. #ifdef EEXIST
  272. else if (errno == EEXIST) code = XML_IO_EEXIST;
  273. #endif
  274. #ifdef EFAULT
  275. else if (errno == EFAULT) code = XML_IO_EFAULT;
  276. #endif
  277. #ifdef EFBIG
  278. else if (errno == EFBIG) code = XML_IO_EFBIG;
  279. #endif
  280. #ifdef EINPROGRESS
  281. else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS;
  282. #endif
  283. #ifdef EINTR
  284. else if (errno == EINTR) code = XML_IO_EINTR;
  285. #endif
  286. #ifdef EINVAL
  287. else if (errno == EINVAL) code = XML_IO_EINVAL;
  288. #endif
  289. #ifdef EIO
  290. else if (errno == EIO) code = XML_IO_EIO;
  291. #endif
  292. #ifdef EISDIR
  293. else if (errno == EISDIR) code = XML_IO_EISDIR;
  294. #endif
  295. #ifdef EMFILE
  296. else if (errno == EMFILE) code = XML_IO_EMFILE;
  297. #endif
  298. #ifdef EMLINK
  299. else if (errno == EMLINK) code = XML_IO_EMLINK;
  300. #endif
  301. #ifdef EMSGSIZE
  302. else if (errno == EMSGSIZE) code = XML_IO_EMSGSIZE;
  303. #endif
  304. #ifdef ENAMETOOLONG
  305. else if (errno == ENAMETOOLONG) code = XML_IO_ENAMETOOLONG;
  306. #endif
  307. #ifdef ENFILE
  308. else if (errno == ENFILE) code = XML_IO_ENFILE;
  309. #endif
  310. #ifdef ENODEV
  311. else if (errno == ENODEV) code = XML_IO_ENODEV;
  312. #endif
  313. #ifdef ENOENT
  314. else if (errno == ENOENT) code = XML_IO_ENOENT;
  315. #endif
  316. #ifdef ENOEXEC
  317. else if (errno == ENOEXEC) code = XML_IO_ENOEXEC;
  318. #endif
  319. #ifdef ENOLCK
  320. else if (errno == ENOLCK) code = XML_IO_ENOLCK;
  321. #endif
  322. #ifdef ENOMEM
  323. else if (errno == ENOMEM) code = XML_IO_ENOMEM;
  324. #endif
  325. #ifdef ENOSPC
  326. else if (errno == ENOSPC) code = XML_IO_ENOSPC;
  327. #endif
  328. #ifdef ENOSYS
  329. else if (errno == ENOSYS) code = XML_IO_ENOSYS;
  330. #endif
  331. #ifdef ENOTDIR
  332. else if (errno == ENOTDIR) code = XML_IO_ENOTDIR;
  333. #endif
  334. #ifdef ENOTEMPTY
  335. else if (errno == ENOTEMPTY) code = XML_IO_ENOTEMPTY;
  336. #endif
  337. #ifdef ENOTSUP
  338. else if (errno == ENOTSUP) code = XML_IO_ENOTSUP;
  339. #endif
  340. #ifdef ENOTTY
  341. else if (errno == ENOTTY) code = XML_IO_ENOTTY;
  342. #endif
  343. #ifdef ENXIO
  344. else if (errno == ENXIO) code = XML_IO_ENXIO;
  345. #endif
  346. #ifdef EPERM
  347. else if (errno == EPERM) code = XML_IO_EPERM;
  348. #endif
  349. #ifdef EPIPE
  350. else if (errno == EPIPE) code = XML_IO_EPIPE;
  351. #endif
  352. #ifdef ERANGE
  353. else if (errno == ERANGE) code = XML_IO_ERANGE;
  354. #endif
  355. #ifdef EROFS
  356. else if (errno == EROFS) code = XML_IO_EROFS;
  357. #endif
  358. #ifdef ESPIPE
  359. else if (errno == ESPIPE) code = XML_IO_ESPIPE;
  360. #endif
  361. #ifdef ESRCH
  362. else if (errno == ESRCH) code = XML_IO_ESRCH;
  363. #endif
  364. #ifdef ETIMEDOUT
  365. else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT;
  366. #endif
  367. #ifdef EXDEV
  368. else if (errno == EXDEV) code = XML_IO_EXDEV;
  369. #endif
  370. #ifdef ENOTSOCK
  371. else if (errno == ENOTSOCK) code = XML_IO_ENOTSOCK;
  372. #endif
  373. #ifdef EISCONN
  374. else if (errno == EISCONN) code = XML_IO_EISCONN;
  375. #endif
  376. #ifdef ECONNREFUSED
  377. else if (errno == ECONNREFUSED) code = XML_IO_ECONNREFUSED;
  378. #endif
  379. #ifdef ETIMEDOUT
  380. else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT;
  381. #endif
  382. #ifdef ENETUNREACH
  383. else if (errno == ENETUNREACH) code = XML_IO_ENETUNREACH;
  384. #endif
  385. #ifdef EADDRINUSE
  386. else if (errno == EADDRINUSE) code = XML_IO_EADDRINUSE;
  387. #endif
  388. #ifdef EINPROGRESS
  389. else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS;
  390. #endif
  391. #ifdef EALREADY
  392. else if (errno == EALREADY) code = XML_IO_EALREADY;
  393. #endif
  394. #ifdef EAFNOSUPPORT
  395. else if (errno == EAFNOSUPPORT) code = XML_IO_EAFNOSUPPORT;
  396. #endif
  397. else code = XML_IO_UNKNOWN;
  398. #endif /* HAVE_ERRNO_H */
  399. }
  400. idx = 0;
  401. if (code >= XML_IO_UNKNOWN) idx = code - XML_IO_UNKNOWN;
  402. if (idx >= (sizeof(IOerr) / sizeof(IOerr[0]))) idx = 0;
  403. __xmlSimpleError(domain, code, NULL, IOerr[idx], extra);
  404. }
  405. /**
  406. * xmlIOErr:
  407. * @code: the error number
  408. * @extra: extra informations
  409. *
  410. * Handle an I/O error
  411. */
  412. static void
  413. xmlIOErr(int code, const char *extra)
  414. {
  415. __xmlIOErr(XML_FROM_IO, code, extra);
  416. }
  417. /**
  418. * __xmlLoaderErr:
  419. * @ctx: the parser context
  420. * @extra: extra informations
  421. *
  422. * Handle a resource access error
  423. */
  424. void
  425. __xmlLoaderErr(void *ctx, const char *msg, const char *filename)
  426. {
  427. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  428. xmlStructuredErrorFunc schannel = NULL;
  429. xmlGenericErrorFunc channel = NULL;
  430. void *data = NULL;
  431. xmlErrorLevel level = XML_ERR_ERROR;
  432. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  433. (ctxt->instate == XML_PARSER_EOF))
  434. return;
  435. if ((ctxt != NULL) && (ctxt->sax != NULL)) {
  436. if (ctxt->validate) {
  437. channel = ctxt->sax->error;
  438. level = XML_ERR_ERROR;
  439. } else {
  440. channel = ctxt->sax->warning;
  441. level = XML_ERR_WARNING;
  442. }
  443. if (ctxt->sax->initialized == XML_SAX2_MAGIC)
  444. schannel = ctxt->sax->serror;
  445. data = ctxt->userData;
  446. }
  447. __xmlRaiseError(schannel, channel, data, ctxt, NULL, XML_FROM_IO,
  448. XML_IO_LOAD_ERROR, level, NULL, 0,
  449. filename, NULL, NULL, 0, 0,
  450. msg, filename);
  451. }
  452. /************************************************************************
  453. * *
  454. * Tree memory error handler *
  455. * *
  456. ************************************************************************/
  457. /**
  458. * xmlNormalizeWindowsPath:
  459. * @path: the input file path
  460. *
  461. * This function is obsolete. Please see xmlURIFromPath in uri.c for
  462. * a better solution.
  463. *
  464. * Returns a canonicalized version of the path
  465. */
  466. xmlChar *
  467. xmlNormalizeWindowsPath(const xmlChar *path)
  468. {
  469. return xmlCanonicPath(path);
  470. }
  471. /**
  472. * xmlCleanupInputCallbacks:
  473. *
  474. * clears the entire input callback table. this includes the
  475. * compiled-in I/O.
  476. */
  477. void
  478. xmlCleanupInputCallbacks(void)
  479. {
  480. int i;
  481. if (!xmlInputCallbackInitialized)
  482. return;
  483. for (i = xmlInputCallbackNr - 1; i >= 0; i--) {
  484. xmlInputCallbackTable[i].matchcallback = NULL;
  485. xmlInputCallbackTable[i].opencallback = NULL;
  486. xmlInputCallbackTable[i].readcallback = NULL;
  487. xmlInputCallbackTable[i].closecallback = NULL;
  488. }
  489. xmlInputCallbackNr = 0;
  490. xmlInputCallbackInitialized = 0;
  491. }
  492. /**
  493. * xmlPopInputCallbacks:
  494. *
  495. * Clear the top input callback from the input stack. this includes the
  496. * compiled-in I/O.
  497. *
  498. * Returns the number of input callback registered or -1 in case of error.
  499. */
  500. int
  501. xmlPopInputCallbacks(void)
  502. {
  503. if (!xmlInputCallbackInitialized)
  504. return(-1);
  505. if (xmlInputCallbackNr <= 0)
  506. return(-1);
  507. xmlInputCallbackNr--;
  508. xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = NULL;
  509. xmlInputCallbackTable[xmlInputCallbackNr].opencallback = NULL;
  510. xmlInputCallbackTable[xmlInputCallbackNr].readcallback = NULL;
  511. xmlInputCallbackTable[xmlInputCallbackNr].closecallback = NULL;
  512. return(xmlInputCallbackNr);
  513. }
  514. #ifdef LIBXML_OUTPUT_ENABLED
  515. /**
  516. * xmlCleanupOutputCallbacks:
  517. *
  518. * clears the entire output callback table. this includes the
  519. * compiled-in I/O callbacks.
  520. */
  521. void
  522. xmlCleanupOutputCallbacks(void)
  523. {
  524. int i;
  525. if (!xmlOutputCallbackInitialized)
  526. return;
  527. for (i = xmlOutputCallbackNr - 1; i >= 0; i--) {
  528. xmlOutputCallbackTable[i].matchcallback = NULL;
  529. xmlOutputCallbackTable[i].opencallback = NULL;
  530. xmlOutputCallbackTable[i].writecallback = NULL;
  531. xmlOutputCallbackTable[i].closecallback = NULL;
  532. }
  533. xmlOutputCallbackNr = 0;
  534. xmlOutputCallbackInitialized = 0;
  535. }
  536. #endif /* LIBXML_OUTPUT_ENABLED */
  537. /************************************************************************
  538. * *
  539. * Standard I/O for file accesses *
  540. * *
  541. ************************************************************************/
  542. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  543. /**
  544. * xmlWrapOpenUtf8:
  545. * @path: the path in utf-8 encoding
  546. * @mode: type of access (0 - read, 1 - write)
  547. *
  548. * function opens the file specified by @path
  549. *
  550. */
  551. static FILE*
  552. xmlWrapOpenUtf8(const char *path,int mode)
  553. {
  554. FILE *fd = NULL;
  555. wchar_t *wPath;
  556. wPath = __xmlIOWin32UTF8ToWChar(path);
  557. if(wPath)
  558. {
  559. fd = _wfopen(wPath, mode ? L"wb" : L"rb");
  560. xmlFree(wPath);
  561. }
  562. /* maybe path in native encoding */
  563. if(fd == NULL)
  564. fd = fopen(path, mode ? "wb" : "rb");
  565. return fd;
  566. }
  567. #ifdef HAVE_ZLIB_H
  568. static gzFile
  569. xmlWrapGzOpenUtf8(const char *path, const char *mode)
  570. {
  571. gzFile fd;
  572. wchar_t *wPath;
  573. fd = gzopen (path, mode);
  574. if (fd)
  575. return fd;
  576. wPath = __xmlIOWin32UTF8ToWChar(path);
  577. if(wPath)
  578. {
  579. int d, m = (strstr(mode, "r") ? O_RDONLY : O_RDWR);
  580. #ifdef _O_BINARY
  581. m |= (strstr(mode, "b") ? _O_BINARY : 0);
  582. #endif
  583. d = _wopen(wPath, m);
  584. if (d >= 0)
  585. fd = gzdopen(d, mode);
  586. xmlFree(wPath);
  587. }
  588. return fd;
  589. }
  590. #endif
  591. /**
  592. * xmlWrapStatUtf8:
  593. * @path: the path in utf-8 encoding
  594. * @info: structure that stores results
  595. *
  596. * function obtains information about the file or directory
  597. *
  598. */
  599. static int
  600. xmlWrapStatUtf8(const char *path,struct stat *info)
  601. {
  602. #ifdef HAVE_STAT
  603. int retval = -1;
  604. wchar_t *wPath;
  605. wPath = __xmlIOWin32UTF8ToWChar(path);
  606. if (wPath)
  607. {
  608. retval = _wstat(wPath,info);
  609. xmlFree(wPath);
  610. }
  611. /* maybe path in native encoding */
  612. if(retval < 0)
  613. retval = stat(path,info);
  614. return retval;
  615. #else
  616. return -1;
  617. #endif
  618. }
  619. /**
  620. * xmlWrapOpenNative:
  621. * @path: the path
  622. * @mode: type of access (0 - read, 1 - write)
  623. *
  624. * function opens the file specified by @path
  625. *
  626. */
  627. static FILE*
  628. xmlWrapOpenNative(const char *path,int mode)
  629. {
  630. return fopen(path,mode ? "wb" : "rb");
  631. }
  632. /**
  633. * xmlWrapStatNative:
  634. * @path: the path
  635. * @info: structure that stores results
  636. *
  637. * function obtains information about the file or directory
  638. *
  639. */
  640. static int
  641. xmlWrapStatNative(const char *path,struct stat *info)
  642. {
  643. #ifdef HAVE_STAT
  644. return stat(path,info);
  645. #else
  646. return -1;
  647. #endif
  648. }
  649. typedef int (* xmlWrapStatFunc) (const char *f, struct stat *s);
  650. static xmlWrapStatFunc xmlWrapStat = xmlWrapStatNative;
  651. typedef FILE* (* xmlWrapOpenFunc)(const char *f,int mode);
  652. static xmlWrapOpenFunc xmlWrapOpen = xmlWrapOpenNative;
  653. #ifdef HAVE_ZLIB_H
  654. typedef gzFile (* xmlWrapGzOpenFunc) (const char *f, const char *mode);
  655. static xmlWrapGzOpenFunc xmlWrapGzOpen = gzopen;
  656. #endif
  657. /**
  658. * xmlInitPlatformSpecificIo:
  659. *
  660. * Initialize platform specific features.
  661. */
  662. static void
  663. xmlInitPlatformSpecificIo(void)
  664. {
  665. static int xmlPlatformIoInitialized = 0;
  666. OSVERSIONINFO osvi;
  667. if(xmlPlatformIoInitialized)
  668. return;
  669. osvi.dwOSVersionInfoSize = sizeof(osvi);
  670. if(GetVersionEx(&osvi) && (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)) {
  671. xmlWrapStat = xmlWrapStatUtf8;
  672. xmlWrapOpen = xmlWrapOpenUtf8;
  673. #ifdef HAVE_ZLIB_H
  674. xmlWrapGzOpen = xmlWrapGzOpenUtf8;
  675. #endif
  676. } else {
  677. xmlWrapStat = xmlWrapStatNative;
  678. xmlWrapOpen = xmlWrapOpenNative;
  679. #ifdef HAVE_ZLIB_H
  680. xmlWrapGzOpen = gzopen;
  681. #endif
  682. }
  683. xmlPlatformIoInitialized = 1;
  684. return;
  685. }
  686. #endif
  687. /**
  688. * xmlCheckFilename:
  689. * @path: the path to check
  690. *
  691. * function checks to see if @path is a valid source
  692. * (file, socket...) for XML.
  693. *
  694. * if stat is not available on the target machine,
  695. * returns 1. if stat fails, returns 0 (if calling
  696. * stat on the filename fails, it can't be right).
  697. * if stat succeeds and the file is a directory,
  698. * returns 2. otherwise returns 1.
  699. */
  700. int
  701. xmlCheckFilename (const char *path)
  702. {
  703. #ifdef HAVE_STAT
  704. struct stat stat_buffer;
  705. #endif
  706. if (path == NULL)
  707. return(0);
  708. #ifdef HAVE_STAT
  709. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  710. if (xmlWrapStat(path, &stat_buffer) == -1)
  711. return 0;
  712. #else
  713. if (stat(path, &stat_buffer) == -1)
  714. return 0;
  715. #endif
  716. #ifdef S_ISDIR
  717. if (S_ISDIR(stat_buffer.st_mode))
  718. return 2;
  719. #endif
  720. #endif /* HAVE_STAT */
  721. return 1;
  722. }
  723. static int
  724. xmlNop(void) {
  725. return(0);
  726. }
  727. /**
  728. * xmlFdRead:
  729. * @context: the I/O context
  730. * @buffer: where to drop data
  731. * @len: number of bytes to read
  732. *
  733. * Read @len bytes to @buffer from the I/O channel.
  734. *
  735. * Returns the number of bytes written
  736. */
  737. static int
  738. xmlFdRead (void * context, char * buffer, int len) {
  739. int ret;
  740. ret = read((int) (long) context, &buffer[0], len);
  741. if (ret < 0) xmlIOErr(0, "read()");
  742. return(ret);
  743. }
  744. #ifdef LIBXML_OUTPUT_ENABLED
  745. /**
  746. * xmlFdWrite:
  747. * @context: the I/O context
  748. * @buffer: where to get data
  749. * @len: number of bytes to write
  750. *
  751. * Write @len bytes from @buffer to the I/O channel.
  752. *
  753. * Returns the number of bytes written
  754. */
  755. static int
  756. xmlFdWrite (void * context, const char * buffer, int len) {
  757. int ret = 0;
  758. if (len > 0) {
  759. ret = write((int) (long) context, &buffer[0], len);
  760. if (ret < 0) xmlIOErr(0, "write()");
  761. }
  762. return(ret);
  763. }
  764. #endif /* LIBXML_OUTPUT_ENABLED */
  765. /**
  766. * xmlFdClose:
  767. * @context: the I/O context
  768. *
  769. * Close an I/O channel
  770. *
  771. * Returns 0 in case of success and error code otherwise
  772. */
  773. static int
  774. xmlFdClose (void * context) {
  775. int ret;
  776. ret = close((int) (long) context);
  777. if (ret < 0) xmlIOErr(0, "close()");
  778. return(ret);
  779. }
  780. /**
  781. * xmlFileMatch:
  782. * @filename: the URI for matching
  783. *
  784. * input from FILE *
  785. *
  786. * Returns 1 if matches, 0 otherwise
  787. */
  788. int
  789. xmlFileMatch (const char *filename ATTRIBUTE_UNUSED) {
  790. return(1);
  791. }
  792. /**
  793. * xmlFileOpen_real:
  794. * @filename: the URI for matching
  795. *
  796. * input from FILE *, supports compressed input
  797. * if @filename is " " then the standard input is used
  798. *
  799. * Returns an I/O context or NULL in case of error
  800. */
  801. static void *
  802. xmlFileOpen_real (const char *filename) {
  803. const char *path = NULL;
  804. FILE *fd;
  805. if (filename == NULL)
  806. return(NULL);
  807. if (!strcmp(filename, "-")) {
  808. fd = stdin;
  809. return((void *) fd);
  810. }
  811. if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17)) {
  812. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  813. path = &filename[17];
  814. #else
  815. path = &filename[16];
  816. #endif
  817. } else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
  818. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  819. path = &filename[8];
  820. #else
  821. path = &filename[7];
  822. #endif
  823. } else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:/", 6)) {
  824. /* lots of generators seems to lazy to read RFC 1738 */
  825. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  826. path = &filename[6];
  827. #else
  828. path = &filename[5];
  829. #endif
  830. } else
  831. path = filename;
  832. if (path == NULL)
  833. return(NULL);
  834. if (!xmlCheckFilename(path))
  835. return(NULL);
  836. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  837. fd = xmlWrapOpen(path, 0);
  838. #else
  839. fd = fopen(path, "r");
  840. #endif /* WIN32 */
  841. if (fd == NULL) xmlIOErr(0, path);
  842. return((void *) fd);
  843. }
  844. /**
  845. * xmlFileOpen:
  846. * @filename: the URI for matching
  847. *
  848. * Wrapper around xmlFileOpen_real that try it with an unescaped
  849. * version of @filename, if this fails fallback to @filename
  850. *
  851. * Returns a handler or NULL in case or failure
  852. */
  853. void *
  854. xmlFileOpen (const char *filename) {
  855. char *unescaped;
  856. void *retval;
  857. retval = xmlFileOpen_real(filename);
  858. if (retval == NULL) {
  859. unescaped = xmlURIUnescapeString(filename, 0, NULL);
  860. if (unescaped != NULL) {
  861. retval = xmlFileOpen_real(unescaped);
  862. xmlFree(unescaped);
  863. }
  864. }
  865. return retval;
  866. }
  867. #ifdef LIBXML_OUTPUT_ENABLED
  868. /**
  869. * xmlFileOpenW:
  870. * @filename: the URI for matching
  871. *
  872. * output to from FILE *,
  873. * if @filename is "-" then the standard output is used
  874. *
  875. * Returns an I/O context or NULL in case of error
  876. */
  877. static void *
  878. xmlFileOpenW (const char *filename) {
  879. const char *path = NULL;
  880. FILE *fd;
  881. if (!strcmp(filename, "-")) {
  882. fd = stdout;
  883. return((void *) fd);
  884. }
  885. if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
  886. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  887. path = &filename[17];
  888. #else
  889. path = &filename[16];
  890. #endif
  891. else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
  892. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  893. path = &filename[8];
  894. #else
  895. path = &filename[7];
  896. #endif
  897. } else
  898. path = filename;
  899. if (path == NULL)
  900. return(NULL);
  901. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  902. fd = xmlWrapOpen(path, 1);
  903. #else
  904. fd = fopen(path, "wb");
  905. #endif /* WIN32 */
  906. if (fd == NULL) xmlIOErr(0, path);
  907. return((void *) fd);
  908. }
  909. #endif /* LIBXML_OUTPUT_ENABLED */
  910. /**
  911. * xmlFileRead:
  912. * @context: the I/O context
  913. * @buffer: where to drop data
  914. * @len: number of bytes to write
  915. *
  916. * Read @len bytes to @buffer from the I/O channel.
  917. *
  918. * Returns the number of bytes written or < 0 in case of failure
  919. */
  920. int
  921. xmlFileRead (void * context, char * buffer, int len) {
  922. int ret;
  923. if ((context == NULL) || (buffer == NULL))
  924. return(-1);
  925. ret = fread(&buffer[0], 1, len, (FILE *) context);
  926. if (ret < 0) xmlIOErr(0, "fread()");
  927. return(ret);
  928. }
  929. #ifdef LIBXML_OUTPUT_ENABLED
  930. /**
  931. * xmlFileWrite:
  932. * @context: the I/O context
  933. * @buffer: where to drop data
  934. * @len: number of bytes to write
  935. *
  936. * Write @len bytes from @buffer to the I/O channel.
  937. *
  938. * Returns the number of bytes written
  939. */
  940. static int
  941. xmlFileWrite (void * context, const char * buffer, int len) {
  942. int items;
  943. if ((context == NULL) || (buffer == NULL))
  944. return(-1);
  945. items = fwrite(&buffer[0], len, 1, (FILE *) context);
  946. if ((items == 0) && (ferror((FILE *) context))) {
  947. xmlIOErr(0, "fwrite()");
  948. return(-1);
  949. }
  950. return(items * len);
  951. }
  952. #endif /* LIBXML_OUTPUT_ENABLED */
  953. /**
  954. * xmlFileClose:
  955. * @context: the I/O context
  956. *
  957. * Close an I/O channel
  958. *
  959. * Returns 0 or -1 in case of error
  960. */
  961. int
  962. xmlFileClose (void * context) {
  963. FILE *fil;
  964. int ret;
  965. if (context == NULL)
  966. return(-1);
  967. fil = (FILE *) context;
  968. if ((fil == stdout) || (fil == stderr)) {
  969. ret = fflush(fil);
  970. if (ret < 0)
  971. xmlIOErr(0, "fflush()");
  972. return(0);
  973. }
  974. if (fil == stdin)
  975. return(0);
  976. ret = ( fclose((FILE *) context) == EOF ) ? -1 : 0;
  977. if (ret < 0)
  978. xmlIOErr(0, "fclose()");
  979. return(ret);
  980. }
  981. /**
  982. * xmlFileFlush:
  983. * @context: the I/O context
  984. *
  985. * Flush an I/O channel
  986. */
  987. static int
  988. xmlFileFlush (void * context) {
  989. int ret;
  990. if (context == NULL)
  991. return(-1);
  992. ret = ( fflush((FILE *) context) == EOF ) ? -1 : 0;
  993. if (ret < 0)
  994. xmlIOErr(0, "fflush()");
  995. return(ret);
  996. }
  997. #ifdef LIBXML_OUTPUT_ENABLED
  998. /**
  999. * xmlBufferWrite:
  1000. * @context: the xmlBuffer
  1001. * @buffer: the data to write
  1002. * @len: number of bytes to write
  1003. *
  1004. * Write @len bytes from @buffer to the xml buffer
  1005. *
  1006. * Returns the number of bytes written
  1007. */
  1008. static int
  1009. xmlBufferWrite (void * context, const char * buffer, int len) {
  1010. int ret;
  1011. ret = xmlBufferAdd((xmlBufferPtr) context, (const xmlChar *) buffer, len);
  1012. if (ret != 0)
  1013. return(-1);
  1014. return(len);
  1015. }
  1016. #endif
  1017. #ifdef HAVE_ZLIB_H
  1018. /************************************************************************
  1019. * *
  1020. * I/O for compressed file accesses *
  1021. * *
  1022. ************************************************************************/
  1023. /**
  1024. * xmlGzfileMatch:
  1025. * @filename: the URI for matching
  1026. *
  1027. * input from compressed file test
  1028. *
  1029. * Returns 1 if matches, 0 otherwise
  1030. */
  1031. static int
  1032. xmlGzfileMatch (const char *filename ATTRIBUTE_UNUSED) {
  1033. return(1);
  1034. }
  1035. /**
  1036. * xmlGzfileOpen_real:
  1037. * @filename: the URI for matching
  1038. *
  1039. * input from compressed file open
  1040. * if @filename is " " then the standard input is used
  1041. *
  1042. * Returns an I/O context or NULL in case of error
  1043. */
  1044. static void *
  1045. xmlGzfileOpen_real (const char *filename) {
  1046. const char *path = NULL;
  1047. gzFile fd;
  1048. if (!strcmp(filename, "-")) {
  1049. fd = gzdopen(dup(0), "rb");
  1050. return((void *) fd);
  1051. }
  1052. if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
  1053. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  1054. path = &filename[17];
  1055. #else
  1056. path = &filename[16];
  1057. #endif
  1058. else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
  1059. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  1060. path = &filename[8];
  1061. #else
  1062. path = &filename[7];
  1063. #endif
  1064. } else
  1065. path = filename;
  1066. if (path == NULL)
  1067. return(NULL);
  1068. if (!xmlCheckFilename(path))
  1069. return(NULL);
  1070. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  1071. fd = xmlWrapGzOpen(path, "rb");
  1072. #else
  1073. fd = gzopen(path, "rb");
  1074. #endif
  1075. return((void *) fd);
  1076. }
  1077. /**
  1078. * xmlGzfileOpen:
  1079. * @filename: the URI for matching
  1080. *
  1081. * Wrapper around xmlGzfileOpen if the open fais, it will
  1082. * try to unescape @filename
  1083. */
  1084. static void *
  1085. xmlGzfileOpen (const char *filename) {
  1086. char *unescaped;
  1087. void *retval;
  1088. retval = xmlGzfileOpen_real(filename);
  1089. if (retval == NULL) {
  1090. unescaped = xmlURIUnescapeString(filename, 0, NULL);
  1091. if (unescaped != NULL) {
  1092. retval = xmlGzfileOpen_real(unescaped);
  1093. }
  1094. xmlFree(unescaped);
  1095. }
  1096. return retval;
  1097. }
  1098. #ifdef LIBXML_OUTPUT_ENABLED
  1099. /**
  1100. * xmlGzfileOpenW:
  1101. * @filename: the URI for matching
  1102. * @compression: the compression factor (0 - 9 included)
  1103. *
  1104. * input from compressed file open
  1105. * if @filename is " " then the standard input is used
  1106. *
  1107. * Returns an I/O context or NULL in case of error
  1108. */
  1109. static void *
  1110. xmlGzfileOpenW (const char *filename, int compression) {
  1111. const char *path = NULL;
  1112. char mode[15];
  1113. gzFile fd;
  1114. snprintf(mode, sizeof(mode), "wb%d", compression);
  1115. if (!strcmp(filename, "-")) {
  1116. fd = gzdopen(dup(1), mode);
  1117. return((void *) fd);
  1118. }
  1119. if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
  1120. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  1121. path = &filename[17];
  1122. #else
  1123. path = &filename[16];
  1124. #endif
  1125. else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
  1126. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  1127. path = &filename[8];
  1128. #else
  1129. path = &filename[7];
  1130. #endif
  1131. } else
  1132. path = filename;
  1133. if (path == NULL)
  1134. return(NULL);
  1135. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  1136. fd = xmlWrapGzOpen(path, mode);
  1137. #else
  1138. fd = gzopen(path, mode);
  1139. #endif
  1140. return((void *) fd);
  1141. }
  1142. #endif /* LIBXML_OUTPUT_ENABLED */
  1143. /**
  1144. * xmlGzfileRead:
  1145. * @context: the I/O context
  1146. * @buffer: where to drop data
  1147. * @len: number of bytes to write
  1148. *
  1149. * Read @len bytes to @buffer from the compressed I/O channel.
  1150. *
  1151. * Returns the number of bytes written
  1152. */
  1153. static int
  1154. xmlGzfileRead (void * context, char * buffer, int len) {
  1155. int ret;
  1156. ret = gzread((gzFile) context, &buffer[0], len);
  1157. if (ret < 0) xmlIOErr(0, "gzread()");
  1158. return(ret);
  1159. }
  1160. #ifdef LIBXML_OUTPUT_ENABLED
  1161. /**
  1162. * xmlGzfileWrite:
  1163. * @context: the I/O context
  1164. * @buffer: where to drop data
  1165. * @len: number of bytes to write
  1166. *
  1167. * Write @len bytes from @buffer to the compressed I/O channel.
  1168. *
  1169. * Returns the number of bytes written
  1170. */
  1171. static int
  1172. xmlGzfileWrite (void * context, const char * buffer, int len) {
  1173. int ret;
  1174. ret = gzwrite((gzFile) context, (char *) &buffer[0], len);
  1175. if (ret < 0) xmlIOErr(0, "gzwrite()");
  1176. return(ret);
  1177. }
  1178. #endif /* LIBXML_OUTPUT_ENABLED */
  1179. /**
  1180. * xmlGzfileClose:
  1181. * @context: the I/O context
  1182. *
  1183. * Close a compressed I/O channel
  1184. */
  1185. static int
  1186. xmlGzfileClose (void * context) {
  1187. int ret;
  1188. ret = (gzclose((gzFile) context) == Z_OK ) ? 0 : -1;
  1189. if (ret < 0) xmlIOErr(0, "gzclose()");
  1190. return(ret);
  1191. }
  1192. #endif /* HAVE_ZLIB_H */
  1193. #ifdef LIBXML_HTTP_ENABLED
  1194. /************************************************************************
  1195. * *
  1196. * I/O for HTTP file accesses *
  1197. * *
  1198. ************************************************************************/
  1199. #ifdef LIBXML_OUTPUT_ENABLED
  1200. typedef struct xmlIOHTTPWriteCtxt_
  1201. {
  1202. int compression;
  1203. char * uri;
  1204. void * doc_buff;
  1205. } xmlIOHTTPWriteCtxt, *xmlIOHTTPWriteCtxtPtr;
  1206. #ifdef HAVE_ZLIB_H
  1207. #define DFLT_WBITS ( -15 )
  1208. #define DFLT_MEM_LVL ( 8 )
  1209. #define GZ_MAGIC1 ( 0x1f )
  1210. #define GZ_MAGIC2 ( 0x8b )
  1211. #define LXML_ZLIB_OS_CODE ( 0x03 )
  1212. #define INIT_HTTP_BUFF_SIZE ( 32768 )
  1213. #define DFLT_ZLIB_RATIO ( 5 )
  1214. /*
  1215. ** Data structure and functions to work with sending compressed data
  1216. ** via HTTP.
  1217. */
  1218. typedef struct xmlZMemBuff_
  1219. {
  1220. unsigned long size;
  1221. unsigned long crc;
  1222. unsigned char * zbuff;
  1223. z_stream zctrl;
  1224. } xmlZMemBuff, *xmlZMemBuffPtr;
  1225. /**
  1226. * append_reverse_ulong
  1227. * @buff: Compressed memory buffer
  1228. * @data: Unsigned long to append
  1229. *
  1230. * Append a unsigned long in reverse byte order to the end of the
  1231. * memory buffer.
  1232. */
  1233. static void
  1234. append_reverse_ulong( xmlZMemBuff * buff, unsigned long data ) {
  1235. int idx;
  1236. if ( buff == NULL )
  1237. return;
  1238. /*
  1239. ** This is plagiarized from putLong in gzio.c (zlib source) where
  1240. ** the number "4" is hardcoded. If zlib is ever patched to
  1241. ** support 64 bit file sizes, this code would need to be patched
  1242. ** as well.
  1243. */
  1244. for ( idx = 0; idx < 4; idx++ ) {
  1245. *buff->zctrl.next_out = ( data & 0xff );
  1246. data >>= 8;
  1247. buff->zctrl.next_out++;
  1248. }
  1249. return;
  1250. }
  1251. /**
  1252. *
  1253. * xmlFreeZMemBuff
  1254. * @buff: The memory buffer context to clear
  1255. *
  1256. * Release all the resources associated with the compressed memory buffer.
  1257. */
  1258. static void
  1259. xmlFreeZMemBuff( xmlZMemBuffPtr buff ) {
  1260. #ifdef DEBUG_HTTP
  1261. int z_err;
  1262. #endif
  1263. if ( buff == NULL )
  1264. return;
  1265. xmlFree( buff->zbuff );
  1266. #ifdef DEBUG_HTTP
  1267. z_err = deflateEnd( &buff->zctrl );
  1268. if ( z_err != Z_OK )
  1269. xmlGenericError( xmlGenericErrorContext,
  1270. "xmlFreeZMemBuff: Error releasing zlib context: %d\n",
  1271. z_err );
  1272. #else
  1273. deflateEnd( &buff->zctrl );
  1274. #endif
  1275. xmlFree( buff );
  1276. return;
  1277. }
  1278. /**
  1279. * xmlCreateZMemBuff
  1280. *@compression: Compression value to use
  1281. *
  1282. * Create a memory buffer to hold the compressed XML document. The
  1283. * compressed document in memory will end up being identical to what
  1284. * would be created if gzopen/gzwrite/gzclose were being used to
  1285. * write the document to disk. The code for the header/trailer data to
  1286. * the compression is plagiarized from the zlib source files.
  1287. */
  1288. static void *
  1289. xmlCreateZMemBuff( int compression ) {
  1290. int z_err;
  1291. int hdr_lgth;
  1292. xmlZMemBuffPtr buff = NULL;
  1293. if ( ( compression < 1 ) || ( compression > 9 ) )
  1294. return ( NULL );
  1295. /* Create the control and data areas */
  1296. buff = xmlMalloc( sizeof( xmlZMemBuff ) );
  1297. if ( buff == NULL ) {
  1298. xmlIOErrMemory("creating buffer context");
  1299. return ( NULL );
  1300. }
  1301. (void)memset( buff, 0, sizeof( xmlZMemBuff ) );
  1302. buff->size = INIT_HTTP_BUFF_SIZE;
  1303. buff->zbuff = xmlMalloc( buff->size );
  1304. if ( buff->zbuff == NULL ) {
  1305. xmlFreeZMemBuff( buff );
  1306. xmlIOErrMemory("creating buffer");
  1307. return ( NULL );
  1308. }
  1309. z_err = deflateInit2( &buff->zctrl, compression, Z_DEFLATED,
  1310. DFLT_WBITS, DFLT_MEM_LVL, Z_DEFAULT_STRATEGY );
  1311. if ( z_err != Z_OK ) {
  1312. xmlChar msg[500];
  1313. xmlFreeZMemBuff( buff );
  1314. buff = NULL;
  1315. xmlStrPrintf(msg, 500,
  1316. (const xmlChar *) "xmlCreateZMemBuff: %s %d\n",
  1317. "Error initializing compression context. ZLIB error:",
  1318. z_err );
  1319. xmlIOErr(XML_IO_WRITE, (const char *) msg);
  1320. return ( NULL );
  1321. }
  1322. /* Set the header data. The CRC will be needed for the trailer */
  1323. buff->crc = crc32( 0L, NULL, 0 );
  1324. hdr_lgth = snprintf( (char *)buff->zbuff, buff->size,
  1325. "%c%c%c%c%c%c%c%c%c%c",
  1326. GZ_MAGIC1, GZ_MAGIC2, Z_DEFLATED,
  1327. 0, 0, 0, 0, 0, 0, LXML_ZLIB_OS_CODE );
  1328. buff->zctrl.next_out = buff->zbuff + hdr_lgth;
  1329. buff->zctrl.avail_out = buff->size - hdr_lgth;
  1330. return ( buff );
  1331. }
  1332. /**
  1333. * xmlZMemBuffExtend
  1334. * @buff: Buffer used to compress and consolidate data.
  1335. * @ext_amt: Number of bytes to extend the buffer.
  1336. *
  1337. * Extend the internal buffer used to store the compressed data by the
  1338. * specified amount.
  1339. *
  1340. * Returns 0 on success or -1 on failure to extend the buffer. On failure
  1341. * the original buffer still exists at the original size.
  1342. */
  1343. static int
  1344. xmlZMemBuffExtend( xmlZMemBuffPtr buff, size_t ext_amt ) {
  1345. int rc = -1;
  1346. size_t new_size;
  1347. size_t cur_used;
  1348. unsigned char * tmp_ptr = NULL;
  1349. if ( buff == NULL )
  1350. return ( -1 );
  1351. else if ( ext_amt == 0 )
  1352. return ( 0 );
  1353. cur_used = buff->zctrl.next_out - buff->zbuff;
  1354. new_size = buff->size + ext_amt;
  1355. #ifdef DEBUG_HTTP
  1356. if ( cur_used > new_size )
  1357. xmlGenericError( xmlGenericErrorContext,
  1358. "xmlZMemBuffExtend: %s\n%s %d bytes.\n",
  1359. "Buffer overwrite detected during compressed memory",
  1360. "buffer extension. Overflowed by",
  1361. (cur_used - new_size ) );
  1362. #endif
  1363. tmp_ptr = xmlRealloc( buff->zbuff, new_size );
  1364. if ( tmp_ptr != NULL ) {
  1365. rc = 0;
  1366. buff->size = new_size;
  1367. buff->zbuff = tmp_ptr;
  1368. buff->zctrl.next_out = tmp_ptr + cur_used;
  1369. buff->zctrl.avail_out = new_size - cur_used;
  1370. }
  1371. else {
  1372. xmlChar msg[500];
  1373. xmlStrPrintf(msg, 500,
  1374. (const xmlChar *) "xmlZMemBuffExtend: %s %lu bytes.\n",
  1375. "Allocation failure extending output buffer to",
  1376. new_size );
  1377. xmlIOErr(XML_IO_WRITE, (const char *) msg);
  1378. }
  1379. return ( rc );
  1380. }
  1381. /**
  1382. * xmlZMemBuffAppend
  1383. * @buff: Buffer used to compress and consolidate data
  1384. * @src: Uncompressed source content to append to buffer
  1385. * @len: Length of source data to append to buffer
  1386. *
  1387. * Compress and append data to the internal buffer. The data buffer
  1388. * will be expanded if needed to store the additional data.
  1389. *
  1390. * Returns the number of bytes appended to the buffer or -1 on error.
  1391. */
  1392. static int
  1393. xmlZMemBuffAppend( xmlZMemBuffPtr buff, const char * src, int len ) {
  1394. int z_err;
  1395. size_t min_accept;
  1396. if ( ( buff == NULL ) || ( src == NULL ) )
  1397. return ( -1 );
  1398. buff->zctrl.avail_in = len;
  1399. buff->zctrl.next_in = (unsigned char *)src;
  1400. while ( buff->zctrl.avail_in > 0 ) {
  1401. /*
  1402. ** Extend the buffer prior to deflate call if a reasonable amount
  1403. ** of output buffer space is not available.
  1404. */
  1405. min_accept = buff->zctrl.avail_in / DFLT_ZLIB_RATIO;
  1406. if ( buff->zctrl.avail_out <= min_accept ) {
  1407. if ( xmlZMemBuffExtend( buff, buff->size ) == -1 )
  1408. return ( -1 );
  1409. }
  1410. z_err = deflate( &buff->zctrl, Z_NO_FLUSH );
  1411. if ( z_err != Z_OK ) {
  1412. xmlChar msg[500];
  1413. xmlStrPrintf(msg, 500,
  1414. (const xmlChar *) "xmlZMemBuffAppend: %s %d %s - %d",
  1415. "Compression error while appending",
  1416. len, "bytes to buffer. ZLIB error", z_err );
  1417. xmlIOErr(XML_IO_WRITE, (const char *) msg);
  1418. return ( -1 );
  1419. }
  1420. }
  1421. buff->crc = crc32( buff->crc, (unsigned char *)src, len );
  1422. return ( len );
  1423. }
  1424. /**
  1425. * xmlZMemBuffGetContent
  1426. * @buff: Compressed memory content buffer
  1427. * @data_ref: Pointer reference to point to compressed content
  1428. *
  1429. * Flushes the compression buffers, appends gzip file trailers and
  1430. * returns the compressed content and length of the compressed data.
  1431. * NOTE: The gzip trailer code here is plagiarized from zlib source.
  1432. *
  1433. * Returns the length of the compressed data or -1 on error.
  1434. */
  1435. static int
  1436. xmlZMemBuffGetContent( xmlZMemBuffPtr buff, char ** data_ref ) {
  1437. int zlgth = -1;
  1438. int z_err;
  1439. if ( ( buff == NULL ) || ( data_ref == NULL ) )
  1440. return ( -1 );
  1441. /* Need to loop until compression output buffers are flushed */
  1442. do
  1443. {
  1444. z_err = deflate( &buff->zctrl, Z_FINISH );
  1445. if ( z_err == Z_OK ) {
  1446. /* In this case Z_OK means more buffer space needed */
  1447. if ( xmlZMemBuffExtend( buff, buff->size ) == -1 )
  1448. return ( -1 );
  1449. }
  1450. }
  1451. while ( z_err == Z_OK );
  1452. /* If the compression state is not Z_STREAM_END, some error occurred */
  1453. if ( z_err == Z_STREAM_END ) {
  1454. /* Need to append the gzip data trailer */
  1455. if ( buff->zctrl.avail_out < ( 2 * sizeof( unsigned long ) ) ) {
  1456. if ( xmlZMemBuffExtend(buff, (2 * sizeof(unsigned long))) == -1 )
  1457. return ( -1 );
  1458. }
  1459. /*
  1460. ** For whatever reason, the CRC and length data are pushed out
  1461. ** in reverse byte order. So a memcpy can't be used here.
  1462. */
  1463. append_reverse_ulong( buff, buff->crc );
  1464. append_reverse_ulong( buff, buff->zctrl.total_in );
  1465. zlgth = buff->zctrl.next_out - buff->zbuff;
  1466. *data_ref = (char *)buff->zbuff;
  1467. }
  1468. else {
  1469. xmlChar msg[500];
  1470. xmlStrPrintf(msg, 500,
  1471. (const xmlChar *) "xmlZMemBuffGetContent: %s - %d\n",
  1472. "Error flushing zlib buffers. Error code", z_err );
  1473. xmlIOErr(XML_IO_WRITE, (const char *) msg);
  1474. }
  1475. return ( zlgth );
  1476. }
  1477. #endif /* LIBXML_OUTPUT_ENABLED */
  1478. #endif /* HAVE_ZLIB_H */
  1479. #ifdef LIBXML_OUTPUT_ENABLED
  1480. /**
  1481. * xmlFreeHTTPWriteCtxt
  1482. * @ctxt: Context to cleanup
  1483. *
  1484. * Free allocated memory and reclaim system resources.
  1485. *
  1486. * No return value.
  1487. */
  1488. static void
  1489. xmlFreeHTTPWriteCtxt( xmlIOHTTPWriteCtxtPtr ctxt )
  1490. {
  1491. if ( ctxt->uri != NULL )
  1492. xmlFree( ctxt->uri );
  1493. if ( ctxt->doc_buff != NULL ) {
  1494. #ifdef HAVE_ZLIB_H
  1495. if ( ctxt->compression > 0 ) {
  1496. xmlFreeZMemBuff( ctxt->doc_buff );
  1497. }
  1498. else
  1499. #endif
  1500. {
  1501. xmlOutputBufferClose( ctxt->doc_buff );
  1502. }
  1503. }
  1504. xmlFree( ctxt );
  1505. return;
  1506. }
  1507. #endif /* LIBXML_OUTPUT_ENABLED */
  1508. /**
  1509. * xmlIOHTTPMatch:
  1510. * @filename: the URI for matching
  1511. *
  1512. * check if the URI matches an HTTP one
  1513. *
  1514. * Returns 1 if matches, 0 otherwise
  1515. */
  1516. int
  1517. xmlIOHTTPMatch (const char *filename) {
  1518. if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "http://", 7))
  1519. return(1);
  1520. return(0);
  1521. }
  1522. /**
  1523. * xmlIOHTTPOpen:
  1524. * @filename: the URI for matching
  1525. *
  1526. * open an HTTP I/O channel
  1527. *
  1528. * Returns an I/O context or NULL in case of error
  1529. */
  1530. void *
  1531. xmlIOHTTPOpen (const char *filename) {
  1532. return(xmlNanoHTTPOpen(filename, NULL));
  1533. }
  1534. #ifdef LIBXML_OUTPUT_ENABLED
  1535. /**
  1536. * xmlIOHTTPOpenW:
  1537. * @post_uri: The destination URI for the document
  1538. * @compression: The compression desired for the document.
  1539. *
  1540. * Open a temporary buffer to collect the document for a subsequent HTTP POST
  1541. * request. Non-static as is called from the output buffer creation routine.
  1542. *
  1543. * Returns an I/O context or NULL in case of error.
  1544. */
  1545. void *
  1546. xmlIOHTTPOpenW(const char *post_uri, int compression)
  1547. {
  1548. xmlIOHTTPWriteCtxtPtr ctxt = NULL;
  1549. if (post_uri == NULL)
  1550. return (NULL);
  1551. ctxt = xmlMalloc(sizeof(xmlIOHTTPWriteCtxt));
  1552. if (ctxt == NULL) {
  1553. xmlIOErrMemory("creating HTTP output context");
  1554. return (NULL);
  1555. }
  1556. (void) memset(ctxt, 0, sizeof(xmlIOHTTPWriteCtxt));
  1557. ctxt->uri = (char *) xmlStrdup((const xmlChar *)post_uri);
  1558. if (ctxt->uri == NULL) {
  1559. xmlIOErrMemory("copying URI");
  1560. xmlFreeHTTPWriteCtxt(ctxt);
  1561. return (NULL);
  1562. }
  1563. /*
  1564. * ** Since the document length is required for an HTTP post,
  1565. * ** need to put the document into a buffer. A memory buffer
  1566. * ** is being used to avoid pushing the data to disk and back.
  1567. */
  1568. #ifdef HAVE_ZLIB_H
  1569. if ((compression > 0) && (compression <= 9)) {
  1570. ctxt->compression = compression;
  1571. ctxt->doc_buff = xmlCreateZMemBuff(compression);
  1572. } else
  1573. #endif
  1574. {
  1575. /* Any character conversions should have been done before this */
  1576. ctxt->doc_buff = xmlAllocOutputBufferInternal(NULL);
  1577. }
  1578. if (ctxt->doc_buff == NULL) {
  1579. xmlFreeHTTPWriteCtxt(ctxt);
  1580. ctxt = NULL;
  1581. }
  1582. return (ctxt);
  1583. }
  1584. #endif /* LIBXML_OUTPUT_ENABLED */
  1585. #ifdef LIBXML_OUTPUT_ENABLED
  1586. /**
  1587. * xmlIOHTTPDfltOpenW
  1588. * @post_uri: The destination URI for this document.
  1589. *
  1590. * Calls xmlIOHTTPOpenW with no compression to set up for a subsequent
  1591. * HTTP post command. This function should generally not be used as
  1592. * the open callback is short circuited in xmlOutputBufferCreateFile.
  1593. *
  1594. * Returns a pointer to the new IO context.
  1595. */
  1596. static void *
  1597. xmlIOHTTPDfltOpenW( const char * post_uri ) {
  1598. return ( xmlIOHTTPOpenW( post_uri, 0 ) );
  1599. }
  1600. #endif /* LIBXML_OUTPUT_ENABLED */
  1601. /**
  1602. * xmlIOHTTPRead:
  1603. * @context: the I/O context
  1604. * @buffer: where to drop data
  1605. * @len: number of bytes to write
  1606. *
  1607. * Read @len bytes to @buffer from the I/O channel.
  1608. *
  1609. * Returns the number of bytes written
  1610. */
  1611. int
  1612. xmlIOHTTPRead(void * context, char * buffer, int len) {
  1613. if ((buffer == NULL) || (len < 0)) return(-1);
  1614. return(xmlNanoHTTPRead(context, &buffer[0], len));
  1615. }
  1616. #ifdef LIBXML_OUTPUT_ENABLED
  1617. /**
  1618. * xmlIOHTTPWrite
  1619. * @context: previously opened writing context
  1620. * @buffer: data to output to temporary buffer
  1621. * @len: bytes to output
  1622. *
  1623. * Collect data from memory buffer into a temporary file for later
  1624. * processing.
  1625. *
  1626. * Returns number of bytes written.
  1627. */
  1628. static int
  1629. xmlIOHTTPWrite( void * context, const char * buffer, int len ) {
  1630. xmlIOHTTPWriteCtxtPtr ctxt = context;
  1631. if ( ( ctxt == NULL ) || ( ctxt->doc_buff == NULL ) || ( buffer == NULL ) )
  1632. return ( -1 );
  1633. if ( len > 0 ) {
  1634. /* Use gzwrite or fwrite as previously setup in the open call */
  1635. #ifdef HAVE_ZLIB_H
  1636. if ( ctxt->compression > 0 )
  1637. len = xmlZMemBuffAppend( ctxt->doc_buff, buffer, len );
  1638. else
  1639. #endif
  1640. len = xmlOutputBufferWrite( ctxt->doc_buff, len, buffer );
  1641. if ( len < 0 ) {
  1642. xmlChar msg[500];
  1643. xmlStrPrintf(msg, 500,
  1644. (const xmlChar *) "xmlIOHTTPWrite: %s\n%s '%s'.\n",
  1645. "Error appending to internal buffer.",
  1646. "Error sending document to URI",
  1647. ctxt->uri );
  1648. xmlIOErr(XML_IO_WRITE, (const char *) msg);
  1649. }
  1650. }
  1651. return ( len );
  1652. }
  1653. #endif /* LIBXML_OUTPUT_ENABLED */
  1654. /**
  1655. * xmlIOHTTPClose:
  1656. * @context: the I/O context
  1657. *
  1658. * Close an HTTP I/O channel
  1659. *
  1660. * Returns 0
  1661. */
  1662. int
  1663. xmlIOHTTPClose (void * context) {
  1664. xmlNanoHTTPClose(context);
  1665. return 0;
  1666. }
  1667. #ifdef LIBXML_OUTPUT_ENABLED
  1668. /**
  1669. * xmlIOHTTCloseWrite
  1670. * @context: The I/O context
  1671. * @http_mthd: The HTTP method to be used when sending the data
  1672. *
  1673. * Close the transmit HTTP I/O channel and actually send the data.
  1674. */
  1675. static int
  1676. xmlIOHTTPCloseWrite( void * context, const char * http_mthd ) {
  1677. int close_rc = -1;
  1678. int http_rtn = 0;
  1679. int content_lgth = 0;
  1680. xmlIOHTTPWriteCtxtPtr ctxt = context;
  1681. char * http_content = NULL;
  1682. char * content_encoding = NULL;
  1683. char * content_type = (char *) "text/xml";
  1684. void * http_ctxt = NULL;
  1685. if ( ( ctxt == NULL ) || ( http_mthd == NULL ) )
  1686. return ( -1 );
  1687. /* Retrieve the content from the appropriate buffer */
  1688. #ifdef HAVE_ZLIB_H
  1689. if ( ctxt->compression > 0 ) {
  1690. content_lgth = xmlZMemBuffGetContent( ctxt->doc_buff, &http_content );
  1691. content_encoding = (char *) "Content-Encoding: gzip";
  1692. }
  1693. else
  1694. #endif
  1695. {
  1696. /* Pull the data out of the memory output buffer */
  1697. xmlOutputBufferPtr dctxt = ctxt->doc_buff;
  1698. http_content = (char *)dctxt->buffer->content;
  1699. content_lgth = dctxt->buffer->use;
  1700. }
  1701. if ( http_content == NULL ) {
  1702. xmlChar msg[500];
  1703. xmlStrPrintf(msg, 500,
  1704. (const xmlChar *) "xmlIOHTTPCloseWrite: %s '%s' %s '%s'.\n",
  1705. "Error retrieving content.\nUnable to",
  1706. http_mthd, "data to URI", ctxt->uri );
  1707. xmlIOErr(XML_IO_WRITE, (const char *) msg);
  1708. }
  1709. else {
  1710. http_ctxt = xmlNanoHTTPMethod( ctxt->uri, http_mthd, http_content,
  1711. &content_type, content_encoding,
  1712. content_lgth );
  1713. if ( http_ctxt != NULL ) {
  1714. #ifdef DEBUG_HTTP
  1715. /* If testing/debugging - dump reply with request content */
  1716. FILE * tst_file = NULL;
  1717. char buffer[ 4096 ];
  1718. char * dump_name = NULL;
  1719. int avail;
  1720. xmlGenericError( xmlGenericErrorContext,
  1721. "xmlNanoHTTPCloseWrite: HTTP %s to\n%s returned %d.\n",
  1722. http_mthd, ctxt->uri,
  1723. xmlNanoHTTPReturnCode( http_ctxt ) );
  1724. /*
  1725. ** Since either content or reply may be gzipped,
  1726. ** dump them to separate files instead of the
  1727. ** standard error context.
  1728. */
  1729. dump_name = tempnam( NULL, "lxml" );
  1730. if ( dump_name != NULL ) {
  1731. (void)snprintf( buffer, sizeof(buffer), "%s.content", dump_name );
  1732. tst_file = fopen( buffer, "wb" );
  1733. if ( tst_file != NULL ) {
  1734. xmlGenericError( xmlGenericErrorContext,
  1735. "Transmitted content saved in file: %s\n", buffer );
  1736. fwrite( http_content, sizeof( char ),
  1737. content_lgth, tst_file );
  1738. fclose( tst_file );
  1739. }
  1740. (void)snprintf( buffer, sizeof(buffer), "%s.reply", dump_name );
  1741. tst_file = fopen( buffer, "wb" );
  1742. if ( tst_file != NULL ) {
  1743. xmlGenericError( xmlGenericErrorContext,
  1744. "Reply content saved in file: %s\n", buffer );
  1745. while ( (avail = xmlNanoHTTPRead( http_ctxt,
  1746. buffer, sizeof( buffer ) )) > 0 ) {
  1747. fwrite( buffer, sizeof( char ), avail, tst_file );
  1748. }
  1749. fclose( tst_file );
  1750. }
  1751. free( dump_name );
  1752. }
  1753. #endif /* DEBUG_HTTP */
  1754. http_rtn = xmlNanoHTTPReturnCode( http_ctxt );
  1755. if ( ( http_rtn >= 200 ) && ( http_rtn < 300 ) )
  1756. close_rc = 0;
  1757. else {
  1758. xmlChar msg[500];
  1759. xmlStrPrintf(msg, 500,
  1760. (const xmlChar *) "xmlIOHTTPCloseWrite: HTTP '%s' of %d %s\n'%s' %s %d\n",
  1761. http_mthd, content_lgth,
  1762. "bytes to URI", ctxt->uri,
  1763. "failed. HTTP return code:", http_rtn );
  1764. xmlIOErr(XML_IO_WRITE, (const char *) msg);
  1765. }
  1766. xmlNanoHTTPClose( http_ctxt );
  1767. xmlFree( content_type );
  1768. }
  1769. }
  1770. /* Final cleanups */
  1771. xmlFreeHTTPWriteCtxt( ctxt );
  1772. return ( close_rc );
  1773. }
  1774. /**
  1775. * xmlIOHTTPClosePut
  1776. *
  1777. * @context: The I/O context
  1778. *
  1779. * Close the transmit HTTP I/O channel and actually send data using a PUT
  1780. * HTTP method.
  1781. */
  1782. static int
  1783. xmlIOHTTPClosePut( void * ctxt ) {
  1784. return ( xmlIOHTTPCloseWrite( ctxt, "PUT" ) );
  1785. }
  1786. /**
  1787. * xmlIOHTTPClosePost
  1788. *
  1789. * @context: The I/O context
  1790. *
  1791. * Close the transmit HTTP I/O channel and actually send data using a POST
  1792. * HTTP method.
  1793. */
  1794. static int
  1795. xmlIOHTTPClosePost( void * ctxt ) {
  1796. return ( xmlIOHTTPCloseWrite( ctxt, "POST" ) );
  1797. }
  1798. #endif /* LIBXML_OUTPUT_ENABLED */
  1799. #endif /* LIBXML_HTTP_ENABLED */
  1800. #ifdef LIBXML_FTP_ENABLED
  1801. /************************************************************************
  1802. * *
  1803. * I/O for FTP file accesses *
  1804. * *
  1805. ************************************************************************/
  1806. /**
  1807. * xmlIOFTPMatch:
  1808. * @filename: the URI for matching
  1809. *
  1810. * check if the URI matches an FTP one
  1811. *
  1812. * Returns 1 if matches, 0 otherwise
  1813. */
  1814. int
  1815. xmlIOFTPMatch (const char *filename) {
  1816. if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "ftp://", 6))
  1817. return(1);
  1818. return(0);
  1819. }
  1820. /**
  1821. * xmlIOFTPOpen:
  1822. * @filename: the URI for matching
  1823. *
  1824. * open an FTP I/O channel
  1825. *
  1826. * Returns an I/O context or NULL in case of error
  1827. */
  1828. void *
  1829. xmlIOFTPOpen (const char *filename) {
  1830. return(xmlNanoFTPOpen(filename));
  1831. }
  1832. /**
  1833. * xmlIOFTPRead:
  1834. * @context: the I/O context
  1835. * @buffer: where to drop data
  1836. * @len: number of bytes to write
  1837. *
  1838. * Read @len bytes to @buffer from the I/O channel.
  1839. *
  1840. * Returns the number of bytes written
  1841. */
  1842. int
  1843. xmlIOFTPRead(void * context, char * buffer, int len) {
  1844. if ((buffer == NULL) || (len < 0)) return(-1);
  1845. return(xmlNanoFTPRead(context, &buffer[0], len));
  1846. }
  1847. /**
  1848. * xmlIOFTPClose:
  1849. * @context: the I/O context
  1850. *
  1851. * Close an FTP I/O channel
  1852. *
  1853. * Returns 0
  1854. */
  1855. int
  1856. xmlIOFTPClose (void * context) {
  1857. return ( xmlNanoFTPClose(context) );
  1858. }
  1859. #endif /* LIBXML_FTP_ENABLED */
  1860. /**
  1861. * xmlRegisterInputCallbacks:
  1862. * @matchFunc: the xmlInputMatchCallback
  1863. * @openFunc: the xmlInputOpenCallback
  1864. * @readFunc: the xmlInputReadCallback
  1865. * @closeFunc: the xmlInputCloseCallback
  1866. *
  1867. * Register a new set of I/O callback for handling parser input.
  1868. *
  1869. * Returns the registered handler number or -1 in case of error
  1870. */
  1871. int
  1872. xmlRegisterInputCallbacks(xmlInputMatchCallback matchFunc,
  1873. xmlInputOpenCallback openFunc, xmlInputReadCallback readFunc,
  1874. xmlInputCloseCallback closeFunc) {
  1875. if (xmlInputCallbackNr >= MAX_INPUT_CALLBACK) {
  1876. return(-1);
  1877. }
  1878. xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = matchFunc;
  1879. xmlInputCallbackTable[xmlInputCallbackNr].opencallback = openFunc;
  1880. xmlInputCallbackTable[xmlInputCallbackNr].readcallback = readFunc;
  1881. xmlInputCallbackTable[xmlInputCallbackNr].closecallback = closeFunc;
  1882. xmlInputCallbackInitialized = 1;
  1883. return(xmlInputCallbackNr++);
  1884. }
  1885. #ifdef LIBXML_OUTPUT_ENABLED
  1886. /**
  1887. * xmlRegisterOutputCallbacks:
  1888. * @matchFunc: the xmlOutputMatchCallback
  1889. * @openFunc: the xmlOutputOpenCallback
  1890. * @writeFunc: the xmlOutputWriteCallback
  1891. * @closeFunc: the xmlOutputCloseCallback
  1892. *
  1893. * Register a new set of I/O callback for handling output.
  1894. *
  1895. * Returns the registered handler number or -1 in case of error
  1896. */
  1897. int
  1898. xmlRegisterOutputCallbacks(xmlOutputMatchCallback matchFunc,
  1899. xmlOutputOpenCallback openFunc, xmlOutputWriteCallback writeFunc,
  1900. xmlOutputCloseCallback closeFunc) {
  1901. if (xmlOutputCallbackNr >= MAX_OUTPUT_CALLBACK) {
  1902. return(-1);
  1903. }
  1904. xmlOutputCallbackTable[xmlOutputCallbackNr].matchcallback = matchFunc;
  1905. xmlOutputCallbackTable[xmlOutputCallbackNr].opencallback = openFunc;
  1906. xmlOutputCallbackTable[xmlOutputCallbackNr].writecallback = writeFunc;
  1907. xmlOutputCallbackTable[xmlOutputCallbackNr].closecallback = closeFunc;
  1908. xmlOutputCallbackInitialized = 1;
  1909. return(xmlOutputCallbackNr++);
  1910. }
  1911. #endif /* LIBXML_OUTPUT_ENABLED */
  1912. /**
  1913. * xmlRegisterDefaultInputCallbacks:
  1914. *
  1915. * Registers the default compiled-in I/O handlers.
  1916. */
  1917. void
  1918. xmlRegisterDefaultInputCallbacks(void) {
  1919. if (xmlInputCallbackInitialized)
  1920. return;
  1921. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  1922. xmlInitPlatformSpecificIo();
  1923. #endif
  1924. xmlRegisterInputCallbacks(xmlFileMatch, xmlFileOpen,
  1925. xmlFileRead, xmlFileClose);
  1926. #ifdef HAVE_ZLIB_H
  1927. xmlRegisterInputCallbacks(xmlGzfileMatch, xmlGzfileOpen,
  1928. xmlGzfileRead, xmlGzfileClose);
  1929. #endif /* HAVE_ZLIB_H */
  1930. #ifdef LIBXML_HTTP_ENABLED
  1931. xmlRegisterInputCallbacks(xmlIOHTTPMatch, xmlIOHTTPOpen,
  1932. xmlIOHTTPRead, xmlIOHTTPClose);
  1933. #endif /* LIBXML_HTTP_ENABLED */
  1934. #ifdef LIBXML_FTP_ENABLED
  1935. xmlRegisterInputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen,
  1936. xmlIOFTPRead, xmlIOFTPClose);
  1937. #endif /* LIBXML_FTP_ENABLED */
  1938. xmlInputCallbackInitialized = 1;
  1939. }
  1940. #ifdef LIBXML_OUTPUT_ENABLED
  1941. /**
  1942. * xmlRegisterDefaultOutputCallbacks:
  1943. *
  1944. * Registers the default compiled-in I/O handlers.
  1945. */
  1946. void
  1947. xmlRegisterDefaultOutputCallbacks (void) {
  1948. if (xmlOutputCallbackInitialized)
  1949. return;
  1950. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  1951. xmlInitPlatformSpecificIo();
  1952. #endif
  1953. xmlRegisterOutputCallbacks(xmlFileMatch, xmlFileOpenW,
  1954. xmlFileWrite, xmlFileClose);
  1955. #ifdef LIBXML_HTTP_ENABLED
  1956. xmlRegisterOutputCallbacks(xmlIOHTTPMatch, xmlIOHTTPDfltOpenW,
  1957. xmlIOHTTPWrite, xmlIOHTTPClosePut);
  1958. #endif
  1959. /*********************************
  1960. No way a-priori to distinguish between gzipped files from
  1961. uncompressed ones except opening if existing then closing
  1962. and saving with same compression ratio ... a pain.
  1963. #ifdef HAVE_ZLIB_H
  1964. xmlRegisterOutputCallbacks(xmlGzfileMatch, xmlGzfileOpen,
  1965. xmlGzfileWrite, xmlGzfileClose);
  1966. #endif
  1967. Nor FTP PUT ....
  1968. #ifdef LIBXML_FTP_ENABLED
  1969. xmlRegisterOutputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen,
  1970. xmlIOFTPWrite, xmlIOFTPClose);
  1971. #endif
  1972. **********************************/
  1973. xmlOutputCallbackInitialized = 1;
  1974. }
  1975. #ifdef LIBXML_HTTP_ENABLED
  1976. /**
  1977. * xmlRegisterHTTPPostCallbacks:
  1978. *
  1979. * By default, libxml submits HTTP output requests using the "PUT" method.
  1980. * Calling this method changes the HTTP output method to use the "POST"
  1981. * method instead.
  1982. *
  1983. */
  1984. void
  1985. xmlRegisterHTTPPostCallbacks( void ) {
  1986. /* Register defaults if not done previously */
  1987. if ( xmlOutputCallbackInitialized == 0 )
  1988. xmlRegisterDefaultOutputCallbacks( );
  1989. xmlRegisterOutputCallbacks(xmlIOHTTPMatch, xmlIOHTTPDfltOpenW,
  1990. xmlIOHTTPWrite, xmlIOHTTPClosePost);
  1991. return;
  1992. }
  1993. #endif
  1994. #endif /* LIBXML_OUTPUT_ENABLED */
  1995. /**
  1996. * xmlAllocParserInputBuffer:
  1997. * @enc: the charset encoding if known
  1998. *
  1999. * Create a buffered parser input for progressive parsing
  2000. *
  2001. * Returns the new parser input or NULL
  2002. */
  2003. xmlParserInputBufferPtr
  2004. xmlAllocParserInputBuffer(xmlCharEncoding enc) {
  2005. xmlParserInputBufferPtr ret;
  2006. ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer));
  2007. if (ret == NULL) {
  2008. xmlIOErrMemory("creating input buffer");
  2009. return(NULL);
  2010. }
  2011. memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer));
  2012. ret->buffer = xmlBufferCreateSize(2 * xmlDefaultBufferSize);
  2013. if (ret->buffer == NULL) {
  2014. xmlFree(ret);
  2015. return(NULL);
  2016. }
  2017. ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT;
  2018. ret->encoder = xmlGetCharEncodingHandler(enc);
  2019. if (ret->encoder != NULL)
  2020. ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize);
  2021. else
  2022. ret->raw = NULL;
  2023. ret->readcallback = NULL;
  2024. ret->closecallback = NULL;
  2025. ret->context = NULL;
  2026. ret->compressed = -1;
  2027. ret->rawconsumed = 0;
  2028. return(ret);
  2029. }
  2030. #ifdef LIBXML_OUTPUT_ENABLED
  2031. /**
  2032. * xmlAllocOutputBuffer:
  2033. * @encoder: the encoding converter or NULL
  2034. *
  2035. * Create a buffered parser output
  2036. *
  2037. * Returns the new parser output or NULL
  2038. */
  2039. xmlOutputBufferPtr
  2040. xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
  2041. xmlOutputBufferPtr ret;
  2042. ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
  2043. if (ret == NULL) {
  2044. xmlIOErrMemory("creating output buffer");
  2045. return(NULL);
  2046. }
  2047. memset(ret, 0, (size_t) sizeof(xmlOutputBuffer));
  2048. ret->buffer = xmlBufferCreate();
  2049. if (ret->buffer == NULL) {
  2050. xmlFree(ret);
  2051. return(NULL);
  2052. }
  2053. /* try to avoid a performance problem with Windows realloc() */
  2054. if (ret->buffer->alloc == XML_BUFFER_ALLOC_EXACT)
  2055. ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT;
  2056. ret->encoder = encoder;
  2057. if (encoder != NULL) {
  2058. ret->conv = xmlBufferCreateSize(4000);
  2059. if (ret->conv == NULL) {
  2060. xmlFree(ret);
  2061. return(NULL);
  2062. }
  2063. /*
  2064. * This call is designed to initiate the encoder state
  2065. */
  2066. xmlCharEncOutFunc(encoder, ret->conv, NULL);
  2067. } else
  2068. ret->conv = NULL;
  2069. ret->writecallback = NULL;
  2070. ret->closecallback = NULL;
  2071. ret->context = NULL;
  2072. ret->written = 0;
  2073. return(ret);
  2074. }
  2075. /**
  2076. * xmlAllocOutputBufferInternal:
  2077. * @encoder: the encoding converter or NULL
  2078. *
  2079. * Create a buffered parser output
  2080. *
  2081. * Returns the new parser output or NULL
  2082. */
  2083. xmlOutputBufferPtr
  2084. xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder) {
  2085. xmlOutputBufferPtr ret;
  2086. ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
  2087. if (ret == NULL) {
  2088. xmlIOErrMemory("creating output buffer");
  2089. return(NULL);
  2090. }
  2091. memset(ret, 0, (size_t) sizeof(xmlOutputBuffer));
  2092. ret->buffer = xmlBufferCreate();
  2093. if (ret->buffer == NULL) {
  2094. xmlFree(ret);
  2095. return(NULL);
  2096. }
  2097. /*
  2098. * For conversion buffers we use the special IO handling
  2099. * We don't do that from the exported API to avoid confusing
  2100. * user's code.
  2101. */
  2102. ret->buffer->alloc = XML_BUFFER_ALLOC_IO;
  2103. ret->buffer->contentIO = ret->buffer->content;
  2104. ret->encoder = encoder;
  2105. if (encoder != NULL) {
  2106. ret->conv = xmlBufferCreateSize(4000);
  2107. if (ret->conv == NULL) {
  2108. xmlFree(ret);
  2109. return(NULL);
  2110. }
  2111. /*
  2112. * This call is designed to initiate the encoder state
  2113. */
  2114. xmlCharEncOutFunc(encoder, ret->conv, NULL);
  2115. } else
  2116. ret->conv = NULL;
  2117. ret->writecallback = NULL;
  2118. ret->closecallback = NULL;
  2119. ret->context = NULL;
  2120. ret->written = 0;
  2121. return(ret);
  2122. }
  2123. #endif /* LIBXML_OUTPUT_ENABLED */
  2124. /**
  2125. * xmlFreeParserInputBuffer:
  2126. * @in: a buffered parser input
  2127. *
  2128. * Free up the memory used by a buffered parser input
  2129. */
  2130. void
  2131. xmlFreeParserInputBuffer(xmlParserInputBufferPtr in) {
  2132. if (in == NULL) return;
  2133. if (in->raw) {
  2134. xmlBufferFree(in->raw);
  2135. in->raw = NULL;
  2136. }
  2137. if (in->encoder != NULL) {
  2138. xmlCharEncCloseFunc(in->encoder);
  2139. }
  2140. if (in->closecallback != NULL) {
  2141. in->closecallback(in->context);
  2142. }
  2143. if (in->buffer != NULL) {
  2144. xmlBufferFree(in->buffer);
  2145. in->buffer = NULL;
  2146. }
  2147. xmlFree(in);
  2148. }
  2149. #ifdef LIBXML_OUTPUT_ENABLED
  2150. /**
  2151. * xmlOutputBufferClose:
  2152. * @out: a buffered output
  2153. *
  2154. * flushes and close the output I/O channel
  2155. * and free up all the associated resources
  2156. *
  2157. * Returns the number of byte written or -1 in case of error.
  2158. */
  2159. int
  2160. xmlOutputBufferClose(xmlOutputBufferPtr out)
  2161. {
  2162. int written;
  2163. int err_rc = 0;
  2164. if (out == NULL)
  2165. return (-1);
  2166. if (out->writecallback != NULL)
  2167. xmlOutputBufferFlush(out);
  2168. if (out->closecallback != NULL) {
  2169. err_rc = out->closecallback(out->context);
  2170. }
  2171. written = out->written;
  2172. if (out->conv) {
  2173. xmlBufferFree(out->conv);
  2174. out->conv = NULL;
  2175. }
  2176. if (out->encoder != NULL) {
  2177. xmlCharEncCloseFunc(out->encoder);
  2178. }
  2179. if (out->buffer != NULL) {
  2180. xmlBufferFree(out->buffer);
  2181. out->buffer = NULL;
  2182. }
  2183. if (out->error)
  2184. err_rc = -1;
  2185. xmlFree(out);
  2186. return ((err_rc == 0) ? written : err_rc);
  2187. }
  2188. #endif /* LIBXML_OUTPUT_ENABLED */
  2189. xmlParserInputBufferPtr
  2190. __xmlParserInputBufferCreateFilename(const char *URI, xmlCharEncoding enc) {
  2191. xmlParserInputBufferPtr ret;
  2192. int i = 0;
  2193. void *context = NULL;
  2194. if (xmlInputCallbackInitialized == 0)
  2195. xmlRegisterDefaultInputCallbacks();
  2196. if (URI == NULL) return(NULL);
  2197. /*
  2198. * Try to find one of the input accept method accepting that scheme
  2199. * Go in reverse to give precedence to user defined handlers.
  2200. */
  2201. if (context == NULL) {
  2202. for (i = xmlInputCallbackNr - 1;i >= 0;i--) {
  2203. if ((xmlInputCallbackTable[i].matchcallback != NULL) &&
  2204. (xmlInputCallbackTable[i].matchcallback(URI) != 0)) {
  2205. context = xmlInputCallbackTable[i].opencallback(URI);
  2206. if (context != NULL) {
  2207. break;
  2208. }
  2209. }
  2210. }
  2211. }
  2212. if (context == NULL) {
  2213. return(NULL);
  2214. }
  2215. /*
  2216. * Allocate the Input buffer front-end.
  2217. */
  2218. ret = xmlAllocParserInputBuffer(enc);
  2219. if (ret != NULL) {
  2220. ret->context = context;
  2221. ret->readcallback = xmlInputCallbackTable[i].readcallback;
  2222. ret->closecallback = xmlInputCallbackTable[i].closecallback;
  2223. #ifdef HAVE_ZLIB_H
  2224. if ((xmlInputCallbackTable[i].opencallback == xmlGzfileOpen) &&
  2225. (strcmp(URI, "-") != 0)) {
  2226. if (((z_stream *)context)->avail_in > 4) {
  2227. char *cptr, buff4[4];
  2228. cptr = (char *) ((z_stream *)context)->next_in;
  2229. if (gzread(context, buff4, 4) == 4) {
  2230. if (strncmp(buff4, cptr, 4) == 0)
  2231. ret->compressed = 0;
  2232. else
  2233. ret->compressed = 1;
  2234. gzrewind(context);
  2235. }
  2236. }
  2237. }
  2238. #endif
  2239. }
  2240. else
  2241. xmlInputCallbackTable[i].closecallback (context);
  2242. return(ret);
  2243. }
  2244. /**
  2245. * xmlParserInputBufferCreateFilename:
  2246. * @URI: a C string containing the URI or filename
  2247. * @enc: the charset encoding if known
  2248. *
  2249. * Create a buffered parser input for the progressive parsing of a file
  2250. * If filename is "-' then we use stdin as the input.
  2251. * Automatic support for ZLIB/Compress compressed document is provided
  2252. * by default if found at compile-time.
  2253. * Do an encoding check if enc == XML_CHAR_ENCODING_NONE
  2254. *
  2255. * Returns the new parser input or NULL
  2256. */
  2257. xmlParserInputBufferPtr
  2258. xmlParserInputBufferCreateFilename(const char *URI, xmlCharEncoding enc) {
  2259. if ((xmlParserInputBufferCreateFilenameValue)) {
  2260. return xmlParserInputBufferCreateFilenameValue(URI, enc);
  2261. }
  2262. return __xmlParserInputBufferCreateFilename(URI, enc);
  2263. }
  2264. #ifdef LIBXML_OUTPUT_ENABLED
  2265. xmlOutputBufferPtr
  2266. __xmlOutputBufferCreateFilename(const char *URI,
  2267. xmlCharEncodingHandlerPtr encoder,
  2268. int compression ATTRIBUTE_UNUSED) {
  2269. xmlOutputBufferPtr ret;
  2270. xmlURIPtr puri;
  2271. int i = 0;
  2272. void *context = NULL;
  2273. char *unescaped = NULL;
  2274. #ifdef HAVE_ZLIB_H
  2275. int is_file_uri = 1;
  2276. #endif
  2277. if (xmlOutputCallbackInitialized == 0)
  2278. xmlRegisterDefaultOutputCallbacks();
  2279. if (URI == NULL) return(NULL);
  2280. puri = xmlParseURI(URI);
  2281. if (puri != NULL) {
  2282. #ifdef HAVE_ZLIB_H
  2283. if ((puri->scheme != NULL) &&
  2284. (!xmlStrEqual(BAD_CAST puri->scheme, BAD_CAST "file")))
  2285. is_file_uri = 0;
  2286. #endif
  2287. /*
  2288. * try to limit the damages of the URI unescaping code.
  2289. */
  2290. if ((puri->scheme == NULL) ||
  2291. (xmlStrEqual(BAD_CAST puri->scheme, BAD_CAST "file")))
  2292. unescaped = xmlURIUnescapeString(URI, 0, NULL);
  2293. xmlFreeURI(puri);
  2294. }
  2295. /*
  2296. * Try to find one of the output accept method accepting that scheme
  2297. * Go in reverse to give precedence to user defined handlers.
  2298. * try with an unescaped version of the URI
  2299. */
  2300. if (unescaped != NULL) {
  2301. #ifdef HAVE_ZLIB_H
  2302. if ((compression > 0) && (compression <= 9) && (is_file_uri == 1)) {
  2303. context = xmlGzfileOpenW(unescaped, compression);
  2304. if (context != NULL) {
  2305. ret = xmlAllocOutputBufferInternal(encoder);
  2306. if (ret != NULL) {
  2307. ret->context = context;
  2308. ret->writecallback = xmlGzfileWrite;
  2309. ret->closecallback = xmlGzfileClose;
  2310. }
  2311. xmlFree(unescaped);
  2312. return(ret);
  2313. }
  2314. }
  2315. #endif
  2316. for (i = xmlOutputCallbackNr - 1;i >= 0;i--) {
  2317. if ((xmlOutputCallbackTable[i].matchcallback != NULL) &&
  2318. (xmlOutputCallbackTable[i].matchcallback(unescaped) != 0)) {
  2319. #if defined(LIBXML_HTTP_ENABLED) && defined(HAVE_ZLIB_H)
  2320. /* Need to pass compression parameter into HTTP open calls */
  2321. if (xmlOutputCallbackTable[i].matchcallback == xmlIOHTTPMatch)
  2322. context = xmlIOHTTPOpenW(unescaped, compression);
  2323. else
  2324. #endif
  2325. context = xmlOutputCallbackTable[i].opencallback(unescaped);
  2326. if (context != NULL)
  2327. break;
  2328. }
  2329. }
  2330. xmlFree(unescaped);
  2331. }
  2332. /*
  2333. * If this failed try with a non-escaped URI this may be a strange
  2334. * filename
  2335. */
  2336. if (context == NULL) {
  2337. #ifdef HAVE_ZLIB_H
  2338. if ((compression > 0) && (compression <= 9) && (is_file_uri == 1)) {
  2339. context = xmlGzfileOpenW(URI, compression);
  2340. if (context != NULL) {
  2341. ret = xmlAllocOutputBufferInternal(encoder);
  2342. if (ret != NULL) {
  2343. ret->context = context;
  2344. ret->writecallback = xmlGzfileWrite;
  2345. ret->closecallback = xmlGzfileClose;
  2346. }
  2347. return(ret);
  2348. }
  2349. }
  2350. #endif
  2351. for (i = xmlOutputCallbackNr - 1;i >= 0;i--) {
  2352. if ((xmlOutputCallbackTable[i].matchcallback != NULL) &&
  2353. (xmlOutputCallbackTable[i].matchcallback(URI) != 0)) {
  2354. #if defined(LIBXML_HTTP_ENABLED) && defined(HAVE_ZLIB_H)
  2355. /* Need to pass compression parameter into HTTP open calls */
  2356. if (xmlOutputCallbackTable[i].matchcallback == xmlIOHTTPMatch)
  2357. context = xmlIOHTTPOpenW(URI, compression);
  2358. else
  2359. #endif
  2360. context = xmlOutputCallbackTable[i].opencallback(URI);
  2361. if (context != NULL)
  2362. break;
  2363. }
  2364. }
  2365. }
  2366. if (context == NULL) {
  2367. return(NULL);
  2368. }
  2369. /*
  2370. * Allocate the Output buffer front-end.
  2371. */
  2372. ret = xmlAllocOutputBufferInternal(encoder);
  2373. if (ret != NULL) {
  2374. ret->context = context;
  2375. ret->writecallback = xmlOutputCallbackTable[i].writecallback;
  2376. ret->closecallback = xmlOutputCallbackTable[i].closecallback;
  2377. }
  2378. return(ret);
  2379. }
  2380. /**
  2381. * xmlOutputBufferCreateFilename:
  2382. * @URI: a C string containing the URI or filename
  2383. * @encoder: the encoding converter or NULL
  2384. * @compression: the compression ration (0 none, 9 max).
  2385. *
  2386. * Create a buffered output for the progressive saving of a file
  2387. * If filename is "-' then we use stdout as the output.
  2388. * Automatic support for ZLIB/Compress compressed document is provided
  2389. * by default if found at compile-time.
  2390. * TODO: currently if compression is set, the library only support
  2391. * writing to a local file.
  2392. *
  2393. * Returns the new output or NULL
  2394. */
  2395. xmlOutputBufferPtr
  2396. xmlOutputBufferCreateFilename(const char *URI,
  2397. xmlCharEncodingHandlerPtr encoder,
  2398. int compression ATTRIBUTE_UNUSED) {
  2399. if ((xmlOutputBufferCreateFilenameValue)) {
  2400. return xmlOutputBufferCreateFilenameValue(URI, encoder, compression);
  2401. }
  2402. return __xmlOutputBufferCreateFilename(URI, encoder, compression);
  2403. }
  2404. #endif /* LIBXML_OUTPUT_ENABLED */
  2405. /**
  2406. * xmlParserInputBufferCreateFile:
  2407. * @file: a FILE*
  2408. * @enc: the charset encoding if known
  2409. *
  2410. * Create a buffered parser input for the progressive parsing of a FILE *
  2411. * buffered C I/O
  2412. *
  2413. * Returns the new parser input or NULL
  2414. */
  2415. xmlParserInputBufferPtr
  2416. xmlParserInputBufferCreateFile(FILE *file, xmlCharEncoding enc) {
  2417. xmlParserInputBufferPtr ret;
  2418. if (xmlInputCallbackInitialized == 0)
  2419. xmlRegisterDefaultInputCallbacks();
  2420. if (file == NULL) return(NULL);
  2421. ret = xmlAllocParserInputBuffer(enc);
  2422. if (ret != NULL) {
  2423. ret->context = file;
  2424. ret->readcallback = xmlFileRead;
  2425. ret->closecallback = xmlFileFlush;
  2426. }
  2427. return(ret);
  2428. }
  2429. #ifdef LIBXML_OUTPUT_ENABLED
  2430. /**
  2431. * xmlOutputBufferCreateFile:
  2432. * @file: a FILE*
  2433. * @encoder: the encoding converter or NULL
  2434. *
  2435. * Create a buffered output for the progressive saving to a FILE *
  2436. * buffered C I/O
  2437. *
  2438. * Returns the new parser output or NULL
  2439. */
  2440. xmlOutputBufferPtr
  2441. xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) {
  2442. xmlOutputBufferPtr ret;
  2443. if (xmlOutputCallbackInitialized == 0)
  2444. xmlRegisterDefaultOutputCallbacks();
  2445. if (file == NULL) return(NULL);
  2446. ret = xmlAllocOutputBufferInternal(encoder);
  2447. if (ret != NULL) {
  2448. ret->context = file;
  2449. ret->writecallback = xmlFileWrite;
  2450. ret->closecallback = xmlFileFlush;
  2451. }
  2452. return(ret);
  2453. }
  2454. /**
  2455. * xmlOutputBufferCreateBuffer:
  2456. * @buffer: a xmlBufferPtr
  2457. * @encoder: the encoding converter or NULL
  2458. *
  2459. * Create a buffered output for the progressive saving to a xmlBuffer
  2460. *
  2461. * Returns the new parser output or NULL
  2462. */
  2463. xmlOutputBufferPtr
  2464. xmlOutputBufferCreateBuffer(xmlBufferPtr buffer,
  2465. xmlCharEncodingHandlerPtr encoder) {
  2466. xmlOutputBufferPtr ret;
  2467. if (buffer == NULL) return(NULL);
  2468. ret = xmlOutputBufferCreateIO((xmlOutputWriteCallback)
  2469. xmlBufferWrite,
  2470. (xmlOutputCloseCallback)
  2471. NULL, (void *) buffer, encoder);
  2472. return(ret);
  2473. }
  2474. #endif /* LIBXML_OUTPUT_ENABLED */
  2475. /**
  2476. * xmlParserInputBufferCreateFd:
  2477. * @fd: a file descriptor number
  2478. * @enc: the charset encoding if known
  2479. *
  2480. * Create a buffered parser input for the progressive parsing for the input
  2481. * from a file descriptor
  2482. *
  2483. * Returns the new parser input or NULL
  2484. */
  2485. xmlParserInputBufferPtr
  2486. xmlParserInputBufferCreateFd(int fd, xmlCharEncoding enc) {
  2487. xmlParserInputBufferPtr ret;
  2488. if (fd < 0) return(NULL);
  2489. ret = xmlAllocParserInputBuffer(enc);
  2490. if (ret != NULL) {
  2491. ret->context = (void *) (long) fd;
  2492. ret->readcallback = xmlFdRead;
  2493. ret->closecallback = xmlFdClose;
  2494. }
  2495. return(ret);
  2496. }
  2497. /**
  2498. * xmlParserInputBufferCreateMem:
  2499. * @mem: the memory input
  2500. * @size: the length of the memory block
  2501. * @enc: the charset encoding if known
  2502. *
  2503. * Create a buffered parser input for the progressive parsing for the input
  2504. * from a memory area.
  2505. *
  2506. * Returns the new parser input or NULL
  2507. */
  2508. xmlParserInputBufferPtr
  2509. xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) {
  2510. xmlParserInputBufferPtr ret;
  2511. int errcode;
  2512. if (size <= 0) return(NULL);
  2513. if (mem == NULL) return(NULL);
  2514. ret = xmlAllocParserInputBuffer(enc);
  2515. if (ret != NULL) {
  2516. ret->context = (void *) mem;
  2517. ret->readcallback = (xmlInputReadCallback) xmlNop;
  2518. ret->closecallback = NULL;
  2519. errcode = xmlBufferAdd(ret->buffer, (const xmlChar *) mem, size);
  2520. if (errcode != 0) {
  2521. xmlFree(ret);
  2522. return(NULL);
  2523. }
  2524. }
  2525. return(ret);
  2526. }
  2527. /**
  2528. * xmlParserInputBufferCreateStatic:
  2529. * @mem: the memory input
  2530. * @size: the length of the memory block
  2531. * @enc: the charset encoding if known
  2532. *
  2533. * Create a buffered parser input for the progressive parsing for the input
  2534. * from an immutable memory area. This will not copy the memory area to
  2535. * the buffer, but the memory is expected to be available until the end of
  2536. * the parsing, this is useful for example when using mmap'ed file.
  2537. *
  2538. * Returns the new parser input or NULL
  2539. */
  2540. xmlParserInputBufferPtr
  2541. xmlParserInputBufferCreateStatic(const char *mem, int size,
  2542. xmlCharEncoding enc) {
  2543. xmlParserInputBufferPtr ret;
  2544. if (size <= 0) return(NULL);
  2545. if (mem == NULL) return(NULL);
  2546. ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer));
  2547. if (ret == NULL) {
  2548. xmlIOErrMemory("creating input buffer");
  2549. return(NULL);
  2550. }
  2551. memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer));
  2552. ret->buffer = xmlBufferCreateStatic((void *)mem, (size_t) size);
  2553. if (ret->buffer == NULL) {
  2554. xmlFree(ret);
  2555. return(NULL);
  2556. }
  2557. ret->encoder = xmlGetCharEncodingHandler(enc);
  2558. if (ret->encoder != NULL)
  2559. ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize);
  2560. else
  2561. ret->raw = NULL;
  2562. ret->compressed = -1;
  2563. ret->context = (void *) mem;
  2564. ret->readcallback = NULL;
  2565. ret->closecallback = NULL;
  2566. return(ret);
  2567. }
  2568. #ifdef LIBXML_OUTPUT_ENABLED
  2569. /**
  2570. * xmlOutputBufferCreateFd:
  2571. * @fd: a file descriptor number
  2572. * @encoder: the encoding converter or NULL
  2573. *
  2574. * Create a buffered output for the progressive saving
  2575. * to a file descriptor
  2576. *
  2577. * Returns the new parser output or NULL
  2578. */
  2579. xmlOutputBufferPtr
  2580. xmlOutputBufferCreateFd(int fd, xmlCharEncodingHandlerPtr encoder) {
  2581. xmlOutputBufferPtr ret;
  2582. if (fd < 0) return(NULL);
  2583. ret = xmlAllocOutputBufferInternal(encoder);
  2584. if (ret != NULL) {
  2585. ret->context = (void *) (long) fd;
  2586. ret->writecallback = xmlFdWrite;
  2587. ret->closecallback = NULL;
  2588. }
  2589. return(ret);
  2590. }
  2591. #endif /* LIBXML_OUTPUT_ENABLED */
  2592. /**
  2593. * xmlParserInputBufferCreateIO:
  2594. * @ioread: an I/O read function
  2595. * @ioclose: an I/O close function
  2596. * @ioctx: an I/O handler
  2597. * @enc: the charset encoding if known
  2598. *
  2599. * Create a buffered parser input for the progressive parsing for the input
  2600. * from an I/O handler
  2601. *
  2602. * Returns the new parser input or NULL
  2603. */
  2604. xmlParserInputBufferPtr
  2605. xmlParserInputBufferCreateIO(xmlInputReadCallback ioread,
  2606. xmlInputCloseCallback ioclose, void *ioctx, xmlCharEncoding enc) {
  2607. xmlParserInputBufferPtr ret;
  2608. if (ioread == NULL) return(NULL);
  2609. ret = xmlAllocParserInputBuffer(enc);
  2610. if (ret != NULL) {
  2611. ret->context = (void *) ioctx;
  2612. ret->readcallback = ioread;
  2613. ret->closecallback = ioclose;
  2614. }
  2615. return(ret);
  2616. }
  2617. #ifdef LIBXML_OUTPUT_ENABLED
  2618. /**
  2619. * xmlOutputBufferCreateIO:
  2620. * @iowrite: an I/O write function
  2621. * @ioclose: an I/O close function
  2622. * @ioctx: an I/O handler
  2623. * @encoder: the charset encoding if known
  2624. *
  2625. * Create a buffered output for the progressive saving
  2626. * to an I/O handler
  2627. *
  2628. * Returns the new parser output or NULL
  2629. */
  2630. xmlOutputBufferPtr
  2631. xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite,
  2632. xmlOutputCloseCallback ioclose, void *ioctx,
  2633. xmlCharEncodingHandlerPtr encoder) {
  2634. xmlOutputBufferPtr ret;
  2635. if (iowrite == NULL) return(NULL);
  2636. ret = xmlAllocOutputBufferInternal(encoder);
  2637. if (ret != NULL) {
  2638. ret->context = (void *) ioctx;
  2639. ret->writecallback = iowrite;
  2640. ret->closecallback = ioclose;
  2641. }
  2642. return(ret);
  2643. }
  2644. #endif /* LIBXML_OUTPUT_ENABLED */
  2645. /**
  2646. * xmlParserInputBufferCreateFilenameDefault:
  2647. * @func: function pointer to the new ParserInputBufferCreateFilenameFunc
  2648. *
  2649. * Registers a callback for URI input file handling
  2650. *
  2651. * Returns the old value of the registration function
  2652. */
  2653. xmlParserInputBufferCreateFilenameFunc
  2654. xmlParserInputBufferCreateFilenameDefault(xmlParserInputBufferCreateFilenameFunc func)
  2655. {
  2656. xmlParserInputBufferCreateFilenameFunc old = xmlParserInputBufferCreateFilenameValue;
  2657. if (old == NULL) {
  2658. old = __xmlParserInputBufferCreateFilename;
  2659. }
  2660. xmlParserInputBufferCreateFilenameValue = func;
  2661. return(old);
  2662. }
  2663. /**
  2664. * xmlOutputBufferCreateFilenameDefault:
  2665. * @func: function pointer to the new OutputBufferCreateFilenameFunc
  2666. *
  2667. * Registers a callback for URI output file handling
  2668. *
  2669. * Returns the old value of the registration function
  2670. */
  2671. xmlOutputBufferCreateFilenameFunc
  2672. xmlOutputBufferCreateFilenameDefault(xmlOutputBufferCreateFilenameFunc func)
  2673. {
  2674. xmlOutputBufferCreateFilenameFunc old = xmlOutputBufferCreateFilenameValue;
  2675. #ifdef LIBXML_OUTPUT_ENABLED
  2676. if (old == NULL) {
  2677. old = __xmlOutputBufferCreateFilename;
  2678. }
  2679. #endif
  2680. xmlOutputBufferCreateFilenameValue = func;
  2681. return(old);
  2682. }
  2683. /**
  2684. * xmlParserInputBufferPush:
  2685. * @in: a buffered parser input
  2686. * @len: the size in bytes of the array.
  2687. * @buf: an char array
  2688. *
  2689. * Push the content of the arry in the input buffer
  2690. * This routine handle the I18N transcoding to internal UTF-8
  2691. * This is used when operating the parser in progressive (push) mode.
  2692. *
  2693. * Returns the number of chars read and stored in the buffer, or -1
  2694. * in case of error.
  2695. */
  2696. int
  2697. xmlParserInputBufferPush(xmlParserInputBufferPtr in,
  2698. int len, const char *buf) {
  2699. int nbchars = 0;
  2700. int ret;
  2701. if (len < 0) return(0);
  2702. if ((in == NULL) || (in->error)) return(-1);
  2703. if (in->encoder != NULL) {
  2704. unsigned int use;
  2705. /*
  2706. * Store the data in the incoming raw buffer
  2707. */
  2708. if (in->raw == NULL) {
  2709. in->raw = xmlBufferCreate();
  2710. }
  2711. ret = xmlBufferAdd(in->raw, (const xmlChar *) buf, len);
  2712. if (ret != 0)
  2713. return(-1);
  2714. /*
  2715. * convert as much as possible to the parser reading buffer.
  2716. */
  2717. use = in->raw->use;
  2718. nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
  2719. if (nbchars < 0) {
  2720. xmlIOErr(XML_IO_ENCODER, NULL);
  2721. in->error = XML_IO_ENCODER;
  2722. return(-1);
  2723. }
  2724. in->rawconsumed += (use - in->raw->use);
  2725. } else {
  2726. nbchars = len;
  2727. ret = xmlBufferAdd(in->buffer, (xmlChar *) buf, nbchars);
  2728. if (ret != 0)
  2729. return(-1);
  2730. }
  2731. #ifdef DEBUG_INPUT
  2732. xmlGenericError(xmlGenericErrorContext,
  2733. "I/O: pushed %d chars, buffer %d/%d\n",
  2734. nbchars, in->buffer->use, in->buffer->size);
  2735. #endif
  2736. return(nbchars);
  2737. }
  2738. /**
  2739. * endOfInput:
  2740. *
  2741. * When reading from an Input channel indicated end of file or error
  2742. * don't reread from it again.
  2743. */
  2744. static int
  2745. endOfInput (void * context ATTRIBUTE_UNUSED,
  2746. char * buffer ATTRIBUTE_UNUSED,
  2747. int len ATTRIBUTE_UNUSED) {
  2748. return(0);
  2749. }
  2750. /**
  2751. * xmlParserInputBufferGrow:
  2752. * @in: a buffered parser input
  2753. * @len: indicative value of the amount of chars to read
  2754. *
  2755. * Grow up the content of the input buffer, the old data are preserved
  2756. * This routine handle the I18N transcoding to internal UTF-8
  2757. * This routine is used when operating the parser in normal (pull) mode
  2758. *
  2759. * TODO: one should be able to remove one extra copy by copying directly
  2760. * onto in->buffer or in->raw
  2761. *
  2762. * Returns the number of chars read and stored in the buffer, or -1
  2763. * in case of error.
  2764. */
  2765. int
  2766. xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) {
  2767. char *buffer = NULL;
  2768. int res = 0;
  2769. int nbchars = 0;
  2770. int buffree;
  2771. unsigned int needSize;
  2772. if ((in == NULL) || (in->error)) return(-1);
  2773. if ((len <= MINLEN) && (len != 4))
  2774. len = MINLEN;
  2775. buffree = in->buffer->size - in->buffer->use;
  2776. if (buffree <= 0) {
  2777. xmlIOErr(XML_IO_BUFFER_FULL, NULL);
  2778. in->error = XML_IO_BUFFER_FULL;
  2779. return(-1);
  2780. }
  2781. needSize = in->buffer->use + len + 1;
  2782. if (needSize > in->buffer->size){
  2783. if (!xmlBufferResize(in->buffer, needSize)){
  2784. xmlIOErrMemory("growing input buffer");
  2785. in->error = XML_ERR_NO_MEMORY;
  2786. return(-1);
  2787. }
  2788. }
  2789. buffer = (char *)&in->buffer->content[in->buffer->use];
  2790. /*
  2791. * Call the read method for this I/O type.
  2792. */
  2793. if (in->readcallback != NULL) {
  2794. res = in->readcallback(in->context, &buffer[0], len);
  2795. if (res <= 0)
  2796. in->readcallback = endOfInput;
  2797. } else {
  2798. xmlIOErr(XML_IO_NO_INPUT, NULL);
  2799. in->error = XML_IO_NO_INPUT;
  2800. return(-1);
  2801. }
  2802. if (res < 0) {
  2803. return(-1);
  2804. }
  2805. len = res;
  2806. if (in->encoder != NULL) {
  2807. unsigned int use;
  2808. /*
  2809. * Store the data in the incoming raw buffer
  2810. */
  2811. if (in->raw == NULL) {
  2812. in->raw = xmlBufferCreate();
  2813. }
  2814. res = xmlBufferAdd(in->raw, (const xmlChar *) buffer, len);
  2815. if (res != 0)
  2816. return(-1);
  2817. /*
  2818. * convert as much as possible to the parser reading buffer.
  2819. */
  2820. use = in->raw->use;
  2821. nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
  2822. if (nbchars < 0) {
  2823. xmlIOErr(XML_IO_ENCODER, NULL);
  2824. in->error = XML_IO_ENCODER;
  2825. return(-1);
  2826. }
  2827. in->rawconsumed += (use - in->raw->use);
  2828. } else {
  2829. nbchars = len;
  2830. in->buffer->use += nbchars;
  2831. buffer[nbchars] = 0;
  2832. }
  2833. #ifdef DEBUG_INPUT
  2834. xmlGenericError(xmlGenericErrorContext,
  2835. "I/O: read %d chars, buffer %d/%d\n",
  2836. nbchars, in->buffer->use, in->buffer->size);
  2837. #endif
  2838. return(nbchars);
  2839. }
  2840. /**
  2841. * xmlParserInputBufferRead:
  2842. * @in: a buffered parser input
  2843. * @len: indicative value of the amount of chars to read
  2844. *
  2845. * Refresh the content of the input buffer, the old data are considered
  2846. * consumed
  2847. * This routine handle the I18N transcoding to internal UTF-8
  2848. *
  2849. * Returns the number of chars read and stored in the buffer, or -1
  2850. * in case of error.
  2851. */
  2852. int
  2853. xmlParserInputBufferRead(xmlParserInputBufferPtr in, int len) {
  2854. if ((in == NULL) || (in->error)) return(-1);
  2855. if (in->readcallback != NULL)
  2856. return(xmlParserInputBufferGrow(in, len));
  2857. else if ((in->buffer != NULL) &&
  2858. (in->buffer->alloc == XML_BUFFER_ALLOC_IMMUTABLE))
  2859. return(0);
  2860. else
  2861. return(-1);
  2862. }
  2863. #ifdef LIBXML_OUTPUT_ENABLED
  2864. /**
  2865. * xmlOutputBufferWrite:
  2866. * @out: a buffered parser output
  2867. * @len: the size in bytes of the array.
  2868. * @buf: an char array
  2869. *
  2870. * Write the content of the array in the output I/O buffer
  2871. * This routine handle the I18N transcoding from internal UTF-8
  2872. * The buffer is lossless, i.e. will store in case of partial
  2873. * or delayed writes.
  2874. *
  2875. * Returns the number of chars immediately written, or -1
  2876. * in case of error.
  2877. */
  2878. int
  2879. xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) {
  2880. int nbchars = 0; /* number of chars to output to I/O */
  2881. int ret; /* return from function call */
  2882. int written = 0; /* number of char written to I/O so far */
  2883. int chunk; /* number of byte curreent processed from buf */
  2884. if ((out == NULL) || (out->error)) return(-1);
  2885. if (len < 0) return(0);
  2886. if (out->error) return(-1);
  2887. do {
  2888. chunk = len;
  2889. if (chunk > 4 * MINLEN)
  2890. chunk = 4 * MINLEN;
  2891. /*
  2892. * first handle encoding stuff.
  2893. */
  2894. if (out->encoder != NULL) {
  2895. /*
  2896. * Store the data in the incoming raw buffer
  2897. */
  2898. if (out->conv == NULL) {
  2899. out->conv = xmlBufferCreate();
  2900. }
  2901. ret = xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk);
  2902. if (ret != 0)
  2903. return(-1);
  2904. if ((out->buffer->use < MINLEN) && (chunk == len))
  2905. goto done;
  2906. /*
  2907. * convert as much as possible to the parser reading buffer.
  2908. */
  2909. ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
  2910. if ((ret < 0) && (ret != -3)) {
  2911. xmlIOErr(XML_IO_ENCODER, NULL);
  2912. out->error = XML_IO_ENCODER;
  2913. return(-1);
  2914. }
  2915. nbchars = out->conv->use;
  2916. } else {
  2917. ret = xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk);
  2918. if (ret != 0)
  2919. return(-1);
  2920. nbchars = out->buffer->use;
  2921. }
  2922. buf += chunk;
  2923. len -= chunk;
  2924. if ((nbchars < MINLEN) && (len <= 0))
  2925. goto done;
  2926. if (out->writecallback) {
  2927. /*
  2928. * second write the stuff to the I/O channel
  2929. */
  2930. if (out->encoder != NULL) {
  2931. ret = out->writecallback(out->context,
  2932. (const char *)out->conv->content, nbchars);
  2933. if (ret >= 0)
  2934. xmlBufferShrink(out->conv, ret);
  2935. } else {
  2936. ret = out->writecallback(out->context,
  2937. (const char *)out->buffer->content, nbchars);
  2938. if (ret >= 0)
  2939. xmlBufferShrink(out->buffer, ret);
  2940. }
  2941. if (ret < 0) {
  2942. xmlIOErr(XML_IO_WRITE, NULL);
  2943. out->error = XML_IO_WRITE;
  2944. return(ret);
  2945. }
  2946. out->written += ret;
  2947. }
  2948. written += nbchars;
  2949. } while (len > 0);
  2950. done:
  2951. #ifdef DEBUG_INPUT
  2952. xmlGenericError(xmlGenericErrorContext,
  2953. "I/O: wrote %d chars\n", written);
  2954. #endif
  2955. return(written);
  2956. }
  2957. /**
  2958. * xmlEscapeContent:
  2959. * @out: a pointer to an array of bytes to store the result
  2960. * @outlen: the length of @out
  2961. * @in: a pointer to an array of unescaped UTF-8 bytes
  2962. * @inlen: the length of @in
  2963. *
  2964. * Take a block of UTF-8 chars in and escape them.
  2965. * Returns 0 if success, or -1 otherwise
  2966. * The value of @inlen after return is the number of octets consumed
  2967. * if the return value is positive, else unpredictable.
  2968. * The value of @outlen after return is the number of octets consumed.
  2969. */
  2970. static int
  2971. xmlEscapeContent(unsigned char* out, int *outlen,
  2972. const xmlChar* in, int *inlen) {
  2973. unsigned char* outstart = out;
  2974. const unsigned char* base = in;
  2975. unsigned char* outend = out + *outlen;
  2976. const unsigned char* inend;
  2977. inend = in + (*inlen);
  2978. while ((in < inend) && (out < outend)) {
  2979. if (*in == '<') {
  2980. if (outend - out < 4) break;
  2981. *out++ = '&';
  2982. *out++ = 'l';
  2983. *out++ = 't';
  2984. *out++ = ';';
  2985. } else if (*in == '>') {
  2986. if (outend - out < 4) break;
  2987. *out++ = '&';
  2988. *out++ = 'g';
  2989. *out++ = 't';
  2990. *out++ = ';';
  2991. } else if (*in == '&') {
  2992. if (outend - out < 5) break;
  2993. *out++ = '&';
  2994. *out++ = 'a';
  2995. *out++ = 'm';
  2996. *out++ = 'p';
  2997. *out++ = ';';
  2998. } else if (*in == '\r') {
  2999. if (outend - out < 5) break;
  3000. *out++ = '&';
  3001. *out++ = '#';
  3002. *out++ = '1';
  3003. *out++ = '3';
  3004. *out++ = ';';
  3005. } else {
  3006. *out++ = (unsigned char) *in;
  3007. }
  3008. ++in;
  3009. }
  3010. *outlen = out - outstart;
  3011. *inlen = in - base;
  3012. return(0);
  3013. }
  3014. /**
  3015. * xmlOutputBufferWriteEscape:
  3016. * @out: a buffered parser output
  3017. * @str: a zero terminated UTF-8 string
  3018. * @escaping: an optional escaping function (or NULL)
  3019. *
  3020. * Write the content of the string in the output I/O buffer
  3021. * This routine escapes the caracters and then handle the I18N
  3022. * transcoding from internal UTF-8
  3023. * The buffer is lossless, i.e. will store in case of partial
  3024. * or delayed writes.
  3025. *
  3026. * Returns the number of chars immediately written, or -1
  3027. * in case of error.
  3028. */
  3029. int
  3030. xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
  3031. xmlCharEncodingOutputFunc escaping) {
  3032. int nbchars = 0; /* number of chars to output to I/O */
  3033. int ret; /* return from function call */
  3034. int written = 0; /* number of char written to I/O so far */
  3035. int oldwritten=0;/* loop guard */
  3036. int chunk; /* number of byte currently processed from str */
  3037. int len; /* number of bytes in str */
  3038. int cons; /* byte from str consumed */
  3039. if ((out == NULL) || (out->error) || (str == NULL) ||
  3040. (out->buffer == NULL) ||
  3041. (out->buffer->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) return(-1);
  3042. len = strlen((const char *)str);
  3043. if (len < 0) return(0);
  3044. if (out->error) return(-1);
  3045. if (escaping == NULL) escaping = xmlEscapeContent;
  3046. do {
  3047. oldwritten = written;
  3048. /*
  3049. * how many bytes to consume and how many bytes to store.
  3050. */
  3051. cons = len;
  3052. chunk = (out->buffer->size - out->buffer->use) - 1;
  3053. /*
  3054. * make sure we have enough room to save first, if this is
  3055. * not the case force a flush, but make sure we stay in the loop
  3056. */
  3057. if (chunk < 40) {
  3058. if (xmlBufferGrow(out->buffer, out->buffer->size + 100) < 0)
  3059. return(-1);
  3060. oldwritten = -1;
  3061. continue;
  3062. }
  3063. /*
  3064. * first handle encoding stuff.
  3065. */
  3066. if (out->encoder != NULL) {
  3067. /*
  3068. * Store the data in the incoming raw buffer
  3069. */
  3070. if (out->conv == NULL) {
  3071. out->conv = xmlBufferCreate();
  3072. }
  3073. ret = escaping(out->buffer->content + out->buffer->use ,
  3074. &chunk, str, &cons);
  3075. if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */
  3076. return(-1);
  3077. out->buffer->use += chunk;
  3078. out->buffer->content[out->buffer->use] = 0;
  3079. if ((out->buffer->use < MINLEN) && (cons == len))
  3080. goto done;
  3081. /*
  3082. * convert as much as possible to the output buffer.
  3083. */
  3084. ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
  3085. if ((ret < 0) && (ret != -3)) {
  3086. xmlIOErr(XML_IO_ENCODER, NULL);
  3087. out->error = XML_IO_ENCODER;
  3088. return(-1);
  3089. }
  3090. nbchars = out->conv->use;
  3091. } else {
  3092. ret = escaping(out->buffer->content + out->buffer->use ,
  3093. &chunk, str, &cons);
  3094. if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */
  3095. return(-1);
  3096. out->buffer->use += chunk;
  3097. out->buffer->content[out->buffer->use] = 0;
  3098. nbchars = out->buffer->use;
  3099. }
  3100. str += cons;
  3101. len -= cons;
  3102. if ((nbchars < MINLEN) && (len <= 0))
  3103. goto done;
  3104. if (out->writecallback) {
  3105. /*
  3106. * second write the stuff to the I/O channel
  3107. */
  3108. if (out->encoder != NULL) {
  3109. ret = out->writecallback(out->context,
  3110. (const char *)out->conv->content, nbchars);
  3111. if (ret >= 0)
  3112. xmlBufferShrink(out->conv, ret);
  3113. } else {
  3114. ret = out->writecallback(out->context,
  3115. (const char *)out->buffer->content, nbchars);
  3116. if (ret >= 0)
  3117. xmlBufferShrink(out->buffer, ret);
  3118. }
  3119. if (ret < 0) {
  3120. xmlIOErr(XML_IO_WRITE, NULL);
  3121. out->error = XML_IO_WRITE;
  3122. return(ret);
  3123. }
  3124. out->written += ret;
  3125. } else if (out->buffer->size - out->buffer->use < MINLEN) {
  3126. xmlBufferResize(out->buffer, out->buffer->size + MINLEN);
  3127. }
  3128. written += nbchars;
  3129. } while ((len > 0) && (oldwritten != written));
  3130. done:
  3131. #ifdef DEBUG_INPUT
  3132. xmlGenericError(xmlGenericErrorContext,
  3133. "I/O: wrote %d chars\n", written);
  3134. #endif
  3135. return(written);
  3136. }
  3137. /**
  3138. * xmlOutputBufferWriteString:
  3139. * @out: a buffered parser output
  3140. * @str: a zero terminated C string
  3141. *
  3142. * Write the content of the string in the output I/O buffer
  3143. * This routine handle the I18N transcoding from internal UTF-8
  3144. * The buffer is lossless, i.e. will store in case of partial
  3145. * or delayed writes.
  3146. *
  3147. * Returns the number of chars immediately written, or -1
  3148. * in case of error.
  3149. */
  3150. int
  3151. xmlOutputBufferWriteString(xmlOutputBufferPtr out, const char *str) {
  3152. int len;
  3153. if ((out == NULL) || (out->error)) return(-1);
  3154. if (str == NULL)
  3155. return(-1);
  3156. len = strlen(str);
  3157. if (len > 0)
  3158. return(xmlOutputBufferWrite(out, len, str));
  3159. return(len);
  3160. }
  3161. /**
  3162. * xmlOutputBufferFlush:
  3163. * @out: a buffered output
  3164. *
  3165. * flushes the output I/O channel
  3166. *
  3167. * Returns the number of byte written or -1 in case of error.
  3168. */
  3169. int
  3170. xmlOutputBufferFlush(xmlOutputBufferPtr out) {
  3171. int nbchars = 0, ret = 0;
  3172. if ((out == NULL) || (out->error)) return(-1);
  3173. /*
  3174. * first handle encoding stuff.
  3175. */
  3176. if ((out->conv != NULL) && (out->encoder != NULL)) {
  3177. /*
  3178. * convert as much as possible to the parser reading buffer.
  3179. */
  3180. nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
  3181. if (nbchars < 0) {
  3182. xmlIOErr(XML_IO_ENCODER, NULL);
  3183. out->error = XML_IO_ENCODER;
  3184. return(-1);
  3185. }
  3186. }
  3187. /*
  3188. * second flush the stuff to the I/O channel
  3189. */
  3190. if ((out->conv != NULL) && (out->encoder != NULL) &&
  3191. (out->writecallback != NULL)) {
  3192. ret = out->writecallback(out->context,
  3193. (const char *)out->conv->content, out->conv->use);
  3194. if (ret >= 0)
  3195. xmlBufferShrink(out->conv, ret);
  3196. } else if (out->writecallback != NULL) {
  3197. ret = out->writecallback(out->context,
  3198. (const char *)out->buffer->content, out->buffer->use);
  3199. if (ret >= 0)
  3200. xmlBufferShrink(out->buffer, ret);
  3201. }
  3202. if (ret < 0) {
  3203. xmlIOErr(XML_IO_FLUSH, NULL);
  3204. out->error = XML_IO_FLUSH;
  3205. return(ret);
  3206. }
  3207. out->written += ret;
  3208. #ifdef DEBUG_INPUT
  3209. xmlGenericError(xmlGenericErrorContext,
  3210. "I/O: flushed %d chars\n", ret);
  3211. #endif
  3212. return(ret);
  3213. }
  3214. #endif /* LIBXML_OUTPUT_ENABLED */
  3215. /**
  3216. * xmlParserGetDirectory:
  3217. * @filename: the path to a file
  3218. *
  3219. * lookup the directory for that file
  3220. *
  3221. * Returns a new allocated string containing the directory, or NULL.
  3222. */
  3223. char *
  3224. xmlParserGetDirectory(const char *filename) {
  3225. char *ret = NULL;
  3226. char dir[1024];
  3227. char *cur;
  3228. #ifdef _WIN32_WCE /* easy way by now ... wince does not have dirs! */
  3229. return NULL;
  3230. #endif
  3231. if (xmlInputCallbackInitialized == 0)
  3232. xmlRegisterDefaultInputCallbacks();
  3233. if (filename == NULL) return(NULL);
  3234. #if defined(WIN32) && !defined(__CYGWIN__)
  3235. # define IS_XMLPGD_SEP(ch) ((ch=='/')||(ch=='\\'))
  3236. #else
  3237. # define IS_XMLPGD_SEP(ch) (ch=='/')
  3238. #endif
  3239. strncpy(dir, filename, 1023);
  3240. dir[1023] = 0;
  3241. cur = &dir[strlen(dir)];
  3242. while (cur > dir) {
  3243. if (IS_XMLPGD_SEP(*cur)) break;
  3244. cur --;
  3245. }
  3246. if (IS_XMLPGD_SEP(*cur)) {
  3247. if (cur == dir) dir[1] = 0;
  3248. else *cur = 0;
  3249. ret = xmlMemStrdup(dir);
  3250. } else {
  3251. if (getcwd(dir, 1024) != NULL) {
  3252. dir[1023] = 0;
  3253. ret = xmlMemStrdup(dir);
  3254. }
  3255. }
  3256. return(ret);
  3257. #undef IS_XMLPGD_SEP
  3258. }
  3259. /****************************************************************
  3260. * *
  3261. * External entities loading *
  3262. * *
  3263. ****************************************************************/
  3264. /**
  3265. * xmlCheckHTTPInput:
  3266. * @ctxt: an XML parser context
  3267. * @ret: an XML parser input
  3268. *
  3269. * Check an input in case it was created from an HTTP stream, in that
  3270. * case it will handle encoding and update of the base URL in case of
  3271. * redirection. It also checks for HTTP errors in which case the input
  3272. * is cleanly freed up and an appropriate error is raised in context
  3273. *
  3274. * Returns the input or NULL in case of HTTP error.
  3275. */
  3276. xmlParserInputPtr
  3277. xmlCheckHTTPInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr ret) {
  3278. #ifdef LIBXML_HTTP_ENABLED
  3279. if ((ret != NULL) && (ret->buf != NULL) &&
  3280. (ret->buf->readcallback == xmlIOHTTPRead) &&
  3281. (ret->buf->context != NULL)) {
  3282. const char *encoding;
  3283. const char *redir;
  3284. const char *mime;
  3285. int code;
  3286. code = xmlNanoHTTPReturnCode(ret->buf->context);
  3287. if (code >= 400) {
  3288. /* fatal error */
  3289. if (ret->filename != NULL)
  3290. __xmlLoaderErr(ctxt, "failed to load HTTP resource \"%s\"\n",
  3291. (const char *) ret->filename);
  3292. else
  3293. __xmlLoaderErr(ctxt, "failed to load HTTP resource\n", NULL);
  3294. xmlFreeInputStream(ret);
  3295. ret = NULL;
  3296. } else {
  3297. mime = xmlNanoHTTPMimeType(ret->buf->context);
  3298. if ((xmlStrstr(BAD_CAST mime, BAD_CAST "/xml")) ||
  3299. (xmlStrstr(BAD_CAST mime, BAD_CAST "+xml"))) {
  3300. encoding = xmlNanoHTTPEncoding(ret->buf->context);
  3301. if (encoding != NULL) {
  3302. xmlCharEncodingHandlerPtr handler;
  3303. handler = xmlFindCharEncodingHandler(encoding);
  3304. if (handler != NULL) {
  3305. xmlSwitchInputEncoding(ctxt, ret, handler);
  3306. } else {
  3307. __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
  3308. "Unknown encoding %s",
  3309. BAD_CAST encoding, NULL);
  3310. }
  3311. if (ret->encoding == NULL)
  3312. ret->encoding = xmlStrdup(BAD_CAST encoding);
  3313. }
  3314. #if 0
  3315. } else if (xmlStrstr(BAD_CAST mime, BAD_CAST "html")) {
  3316. #endif
  3317. }
  3318. redir = xmlNanoHTTPRedir(ret->buf->context);
  3319. if (redir != NULL) {
  3320. if (ret->filename != NULL)
  3321. xmlFree((xmlChar *) ret->filename);
  3322. if (ret->directory != NULL) {
  3323. xmlFree((xmlChar *) ret->directory);
  3324. ret->directory = NULL;
  3325. }
  3326. ret->filename =
  3327. (char *) xmlStrdup((const xmlChar *) redir);
  3328. }
  3329. }
  3330. }
  3331. #endif
  3332. return(ret);
  3333. }
  3334. static int xmlNoNetExists(const char *URL) {
  3335. const char *path;
  3336. if (URL == NULL)
  3337. return(0);
  3338. if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file://localhost/", 17))
  3339. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  3340. path = &URL[17];
  3341. #else
  3342. path = &URL[16];
  3343. #endif
  3344. else if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file:///", 8)) {
  3345. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  3346. path = &URL[8];
  3347. #else
  3348. path = &URL[7];
  3349. #endif
  3350. } else
  3351. path = URL;
  3352. return xmlCheckFilename(path);
  3353. }
  3354. #ifdef LIBXML_CATALOG_ENABLED
  3355. /**
  3356. * xmlResolveResourceFromCatalog:
  3357. * @URL: the URL for the entity to load
  3358. * @ID: the System ID for the entity to load
  3359. * @ctxt: the context in which the entity is called or NULL
  3360. *
  3361. * Resolves the URL and ID against the appropriate catalog.
  3362. * This function is used by xmlDefaultExternalEntityLoader and
  3363. * xmlNoNetExternalEntityLoader.
  3364. *
  3365. * Returns a new allocated URL, or NULL.
  3366. */
  3367. static xmlChar *
  3368. xmlResolveResourceFromCatalog(const char *URL, const char *ID,
  3369. xmlParserCtxtPtr ctxt) {
  3370. xmlChar *resource = NULL;
  3371. xmlCatalogAllow pref;
  3372. /*
  3373. * If the resource doesn't exists as a file,
  3374. * try to load it from the resource pointed in the catalogs
  3375. */
  3376. pref = xmlCatalogGetDefaults();
  3377. if ((pref != XML_CATA_ALLOW_NONE) && (!xmlNoNetExists(URL))) {
  3378. /*
  3379. * Do a local lookup
  3380. */
  3381. if ((ctxt != NULL) && (ctxt->catalogs != NULL) &&
  3382. ((pref == XML_CATA_ALLOW_ALL) ||
  3383. (pref == XML_CATA_ALLOW_DOCUMENT))) {
  3384. resource = xmlCatalogLocalResolve(ctxt->catalogs,
  3385. (const xmlChar *)ID,
  3386. (const xmlChar *)URL);
  3387. }
  3388. /*
  3389. * Try a global lookup
  3390. */
  3391. if ((resource == NULL) &&
  3392. ((pref == XML_CATA_ALLOW_ALL) ||
  3393. (pref == XML_CATA_ALLOW_GLOBAL))) {
  3394. resource = xmlCatalogResolve((const xmlChar *)ID,
  3395. (const xmlChar *)URL);
  3396. }
  3397. if ((resource == NULL) && (URL != NULL))
  3398. resource = xmlStrdup((const xmlChar *) URL);
  3399. /*
  3400. * TODO: do an URI lookup on the reference
  3401. */
  3402. if ((resource != NULL) && (!xmlNoNetExists((const char *)resource))) {
  3403. xmlChar *tmp = NULL;
  3404. if ((ctxt != NULL) && (ctxt->catalogs != NULL) &&
  3405. ((pref == XML_CATA_ALLOW_ALL) ||
  3406. (pref == XML_CATA_ALLOW_DOCUMENT))) {
  3407. tmp = xmlCatalogLocalResolveURI(ctxt->catalogs, resource);
  3408. }
  3409. if ((tmp == NULL) &&
  3410. ((pref == XML_CATA_ALLOW_ALL) ||
  3411. (pref == XML_CATA_ALLOW_GLOBAL))) {
  3412. tmp = xmlCatalogResolveURI(resource);
  3413. }
  3414. if (tmp != NULL) {
  3415. xmlFree(resource);
  3416. resource = tmp;
  3417. }
  3418. }
  3419. }
  3420. return resource;
  3421. }
  3422. #endif
  3423. /**
  3424. * xmlDefaultExternalEntityLoader:
  3425. * @URL: the URL for the entity to load
  3426. * @ID: the System ID for the entity to load
  3427. * @ctxt: the context in which the entity is called or NULL
  3428. *
  3429. * By default we don't load external entitites, yet.
  3430. *
  3431. * Returns a new allocated xmlParserInputPtr, or NULL.
  3432. */
  3433. static xmlParserInputPtr
  3434. xmlDefaultExternalEntityLoader(const char *URL, const char *ID,
  3435. xmlParserCtxtPtr ctxt)
  3436. {
  3437. xmlParserInputPtr ret = NULL;
  3438. xmlChar *resource = NULL;
  3439. #ifdef DEBUG_EXTERNAL_ENTITIES
  3440. xmlGenericError(xmlGenericErrorContext,
  3441. "xmlDefaultExternalEntityLoader(%s, xxx)\n", URL);
  3442. #endif
  3443. if ((ctxt != NULL) && (ctxt->options & XML_PARSE_NONET)) {
  3444. int options = ctxt->options;
  3445. ctxt->options -= XML_PARSE_NONET;
  3446. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  3447. ctxt->options = options;
  3448. return(ret);
  3449. }
  3450. #ifdef LIBXML_CATALOG_ENABLED
  3451. resource = xmlResolveResourceFromCatalog(URL, ID, ctxt);
  3452. #endif
  3453. if (resource == NULL)
  3454. resource = (xmlChar *) URL;
  3455. if (resource == NULL) {
  3456. if (ID == NULL)
  3457. ID = "NULL";
  3458. __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n", ID);
  3459. return (NULL);
  3460. }
  3461. ret = xmlNewInputFromFile(ctxt, (const char *) resource);
  3462. if ((resource != NULL) && (resource != (xmlChar *) URL))
  3463. xmlFree(resource);
  3464. return (ret);
  3465. }
  3466. static xmlExternalEntityLoader xmlCurrentExternalEntityLoader =
  3467. xmlDefaultExternalEntityLoader;
  3468. /**
  3469. * xmlSetExternalEntityLoader:
  3470. * @f: the new entity resolver function
  3471. *
  3472. * Changes the defaultexternal entity resolver function for the application
  3473. */
  3474. void
  3475. xmlSetExternalEntityLoader(xmlExternalEntityLoader f) {
  3476. xmlCurrentExternalEntityLoader = f;
  3477. }
  3478. /**
  3479. * xmlGetExternalEntityLoader:
  3480. *
  3481. * Get the default external entity resolver function for the application
  3482. *
  3483. * Returns the xmlExternalEntityLoader function pointer
  3484. */
  3485. xmlExternalEntityLoader
  3486. xmlGetExternalEntityLoader(void) {
  3487. return(xmlCurrentExternalEntityLoader);
  3488. }
  3489. /**
  3490. * xmlLoadExternalEntity:
  3491. * @URL: the URL for the entity to load
  3492. * @ID: the Public ID for the entity to load
  3493. * @ctxt: the context in which the entity is called or NULL
  3494. *
  3495. * Load an external entity, note that the use of this function for
  3496. * unparsed entities may generate problems
  3497. *
  3498. * Returns the xmlParserInputPtr or NULL
  3499. */
  3500. xmlParserInputPtr
  3501. xmlLoadExternalEntity(const char *URL, const char *ID,
  3502. xmlParserCtxtPtr ctxt) {
  3503. if ((URL != NULL) && (xmlNoNetExists(URL) == 0)) {
  3504. char *canonicFilename;
  3505. xmlParserInputPtr ret;
  3506. canonicFilename = (char *) xmlCanonicPath((const xmlChar *) URL);
  3507. if (canonicFilename == NULL) {
  3508. xmlIOErrMemory("building canonical path\n");
  3509. return(NULL);
  3510. }
  3511. ret = xmlCurrentExternalEntityLoader(canonicFilename, ID, ctxt);
  3512. xmlFree(canonicFilename);
  3513. return(ret);
  3514. }
  3515. return(xmlCurrentExternalEntityLoader(URL, ID, ctxt));
  3516. }
  3517. /************************************************************************
  3518. * *
  3519. * Disabling Network access *
  3520. * *
  3521. ************************************************************************/
  3522. /**
  3523. * xmlNoNetExternalEntityLoader:
  3524. * @URL: the URL for the entity to load
  3525. * @ID: the System ID for the entity to load
  3526. * @ctxt: the context in which the entity is called or NULL
  3527. *
  3528. * A specific entity loader disabling network accesses, though still
  3529. * allowing local catalog accesses for resolution.
  3530. *
  3531. * Returns a new allocated xmlParserInputPtr, or NULL.
  3532. */
  3533. xmlParserInputPtr
  3534. xmlNoNetExternalEntityLoader(const char *URL, const char *ID,
  3535. xmlParserCtxtPtr ctxt) {
  3536. xmlParserInputPtr input = NULL;
  3537. xmlChar *resource = NULL;
  3538. #ifdef LIBXML_CATALOG_ENABLED
  3539. resource = xmlResolveResourceFromCatalog(URL, ID, ctxt);
  3540. #endif
  3541. if (resource == NULL)
  3542. resource = (xmlChar *) URL;
  3543. if (resource != NULL) {
  3544. if ((!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "ftp://", 6)) ||
  3545. (!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "http://", 7))) {
  3546. xmlIOErr(XML_IO_NETWORK_ATTEMPT, (const char *) resource);
  3547. if (resource != (xmlChar *) URL)
  3548. xmlFree(resource);
  3549. return(NULL);
  3550. }
  3551. }
  3552. input = xmlDefaultExternalEntityLoader((const char *) resource, ID, ctxt);
  3553. if (resource != (xmlChar *) URL)
  3554. xmlFree(resource);
  3555. return(input);
  3556. }
  3557. #define bottom_xmlIO
  3558. #include "elfgcchack.h"