xmlIO.c 102 KB

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