xmlIO.c 100 KB

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