xmlIO.c 103 KB

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