xmlIO.c 100 KB

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