xmlIO.c 104 KB

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