xmlIO.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138
  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. else
  2423. xmlGzfileClose(context);
  2424. return(ret);
  2425. }
  2426. }
  2427. #endif
  2428. for (i = xmlOutputCallbackNr - 1;i >= 0;i--) {
  2429. if ((xmlOutputCallbackTable[i].matchcallback != NULL) &&
  2430. (xmlOutputCallbackTable[i].matchcallback(URI) != 0)) {
  2431. #if defined(LIBXML_HTTP_ENABLED) && defined(LIBXML_ZLIB_ENABLED)
  2432. /* Need to pass compression parameter into HTTP open calls */
  2433. if (xmlOutputCallbackTable[i].matchcallback == xmlIOHTTPMatch)
  2434. context = xmlIOHTTPOpenW(URI, compression);
  2435. else
  2436. #endif
  2437. context = xmlOutputCallbackTable[i].opencallback(URI);
  2438. if (context != NULL)
  2439. break;
  2440. }
  2441. }
  2442. }
  2443. if (context == NULL) {
  2444. return(NULL);
  2445. }
  2446. /*
  2447. * Allocate the Output buffer front-end.
  2448. */
  2449. ret = xmlAllocOutputBufferInternal(encoder);
  2450. if (ret != NULL) {
  2451. ret->context = context;
  2452. ret->writecallback = xmlOutputCallbackTable[i].writecallback;
  2453. ret->closecallback = xmlOutputCallbackTable[i].closecallback;
  2454. }
  2455. return(ret);
  2456. }
  2457. /**
  2458. * xmlOutputBufferCreateFilename:
  2459. * @URI: a C string containing the URI or filename
  2460. * @encoder: the encoding converter or NULL
  2461. * @compression: the compression ration (0 none, 9 max).
  2462. *
  2463. * Create a buffered output for the progressive saving of a file
  2464. * If filename is "-' then we use stdout as the output.
  2465. * Automatic support for ZLIB/Compress compressed document is provided
  2466. * by default if found at compile-time.
  2467. * TODO: currently if compression is set, the library only support
  2468. * writing to a local file.
  2469. *
  2470. * Returns the new output or NULL
  2471. */
  2472. xmlOutputBufferPtr
  2473. xmlOutputBufferCreateFilename(const char *URI,
  2474. xmlCharEncodingHandlerPtr encoder,
  2475. int compression ATTRIBUTE_UNUSED) {
  2476. if ((xmlOutputBufferCreateFilenameValue)) {
  2477. return xmlOutputBufferCreateFilenameValue(URI, encoder, compression);
  2478. }
  2479. return __xmlOutputBufferCreateFilename(URI, encoder, compression);
  2480. }
  2481. #endif /* LIBXML_OUTPUT_ENABLED */
  2482. /**
  2483. * xmlParserInputBufferCreateFile:
  2484. * @file: a FILE*
  2485. * @enc: the charset encoding if known
  2486. *
  2487. * Create a buffered parser input for the progressive parsing of a FILE *
  2488. * buffered C I/O
  2489. *
  2490. * Returns the new parser input or NULL
  2491. */
  2492. xmlParserInputBufferPtr
  2493. xmlParserInputBufferCreateFile(FILE *file, xmlCharEncoding enc) {
  2494. xmlParserInputBufferPtr ret;
  2495. if (xmlInputCallbackInitialized == 0)
  2496. xmlRegisterDefaultInputCallbacks();
  2497. if (file == NULL) return(NULL);
  2498. ret = xmlAllocParserInputBuffer(enc);
  2499. if (ret != NULL) {
  2500. ret->context = file;
  2501. ret->readcallback = xmlFileRead;
  2502. ret->closecallback = xmlFileFlush;
  2503. }
  2504. return(ret);
  2505. }
  2506. #ifdef LIBXML_OUTPUT_ENABLED
  2507. /**
  2508. * xmlOutputBufferCreateFile:
  2509. * @file: a FILE*
  2510. * @encoder: the encoding converter or NULL
  2511. *
  2512. * Create a buffered output for the progressive saving to a FILE *
  2513. * buffered C I/O
  2514. *
  2515. * Returns the new parser output or NULL
  2516. */
  2517. xmlOutputBufferPtr
  2518. xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) {
  2519. xmlOutputBufferPtr ret;
  2520. if (xmlOutputCallbackInitialized == 0)
  2521. xmlRegisterDefaultOutputCallbacks();
  2522. if (file == NULL) return(NULL);
  2523. ret = xmlAllocOutputBufferInternal(encoder);
  2524. if (ret != NULL) {
  2525. ret->context = file;
  2526. ret->writecallback = xmlFileWrite;
  2527. ret->closecallback = xmlFileFlush;
  2528. }
  2529. return(ret);
  2530. }
  2531. /**
  2532. * xmlOutputBufferCreateBuffer:
  2533. * @buffer: a xmlBufferPtr
  2534. * @encoder: the encoding converter or NULL
  2535. *
  2536. * Create a buffered output for the progressive saving to a xmlBuffer
  2537. *
  2538. * Returns the new parser output or NULL
  2539. */
  2540. xmlOutputBufferPtr
  2541. xmlOutputBufferCreateBuffer(xmlBufferPtr buffer,
  2542. xmlCharEncodingHandlerPtr encoder) {
  2543. xmlOutputBufferPtr ret;
  2544. if (buffer == NULL) return(NULL);
  2545. ret = xmlOutputBufferCreateIO(xmlBufferWrite, NULL, (void *) buffer,
  2546. encoder);
  2547. return(ret);
  2548. }
  2549. /**
  2550. * xmlOutputBufferGetContent:
  2551. * @out: an xmlOutputBufferPtr
  2552. *
  2553. * Gives a pointer to the data currently held in the output buffer
  2554. *
  2555. * Returns a pointer to the data or NULL in case of error
  2556. */
  2557. const xmlChar *
  2558. xmlOutputBufferGetContent(xmlOutputBufferPtr out) {
  2559. if ((out == NULL) || (out->buffer == NULL))
  2560. return(NULL);
  2561. return(xmlBufContent(out->buffer));
  2562. }
  2563. /**
  2564. * xmlOutputBufferGetSize:
  2565. * @out: an xmlOutputBufferPtr
  2566. *
  2567. * Gives the length of the data currently held in the output buffer
  2568. *
  2569. * Returns 0 in case or error or no data is held, the size otherwise
  2570. */
  2571. size_t
  2572. xmlOutputBufferGetSize(xmlOutputBufferPtr out) {
  2573. if ((out == NULL) || (out->buffer == NULL))
  2574. return(0);
  2575. return(xmlBufUse(out->buffer));
  2576. }
  2577. #endif /* LIBXML_OUTPUT_ENABLED */
  2578. /**
  2579. * xmlParserInputBufferCreateFd:
  2580. * @fd: a file descriptor number
  2581. * @enc: the charset encoding if known
  2582. *
  2583. * Create a buffered parser input for the progressive parsing for the input
  2584. * from a file descriptor
  2585. *
  2586. * Returns the new parser input or NULL
  2587. */
  2588. xmlParserInputBufferPtr
  2589. xmlParserInputBufferCreateFd(int fd, xmlCharEncoding enc) {
  2590. xmlParserInputBufferPtr ret;
  2591. if (fd < 0) return(NULL);
  2592. ret = xmlAllocParserInputBuffer(enc);
  2593. if (ret != NULL) {
  2594. ret->context = (void *) (ptrdiff_t) fd;
  2595. ret->readcallback = xmlFdRead;
  2596. ret->closecallback = xmlFdClose;
  2597. }
  2598. return(ret);
  2599. }
  2600. /**
  2601. * xmlParserInputBufferCreateMem:
  2602. * @mem: the memory input
  2603. * @size: the length of the memory block
  2604. * @enc: the charset encoding if known
  2605. *
  2606. * Create a buffered parser input for the progressive parsing for the input
  2607. * from a memory area.
  2608. *
  2609. * Returns the new parser input or NULL
  2610. */
  2611. xmlParserInputBufferPtr
  2612. xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) {
  2613. xmlParserInputBufferPtr ret;
  2614. int errcode;
  2615. if (size < 0) return(NULL);
  2616. if (mem == NULL) return(NULL);
  2617. ret = xmlAllocParserInputBuffer(enc);
  2618. if (ret != NULL) {
  2619. ret->context = (void *) mem;
  2620. ret->readcallback = xmlInputReadCallbackNop;
  2621. ret->closecallback = NULL;
  2622. errcode = xmlBufAdd(ret->buffer, (const xmlChar *) mem, size);
  2623. if (errcode != 0) {
  2624. xmlFree(ret);
  2625. return(NULL);
  2626. }
  2627. }
  2628. return(ret);
  2629. }
  2630. /**
  2631. * xmlParserInputBufferCreateStatic:
  2632. * @mem: the memory input
  2633. * @size: the length of the memory block
  2634. * @enc: the charset encoding if known
  2635. *
  2636. * Create a buffered parser input for the progressive parsing for the input
  2637. * from an immutable memory area. This will not copy the memory area to
  2638. * the buffer, but the memory is expected to be available until the end of
  2639. * the parsing, this is useful for example when using mmap'ed file.
  2640. *
  2641. * Returns the new parser input or NULL
  2642. */
  2643. xmlParserInputBufferPtr
  2644. xmlParserInputBufferCreateStatic(const char *mem, int size,
  2645. xmlCharEncoding enc) {
  2646. xmlParserInputBufferPtr ret;
  2647. if (size < 0) return(NULL);
  2648. if (mem == NULL) return(NULL);
  2649. ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer));
  2650. if (ret == NULL) {
  2651. xmlIOErrMemory("creating input buffer");
  2652. return(NULL);
  2653. }
  2654. memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer));
  2655. ret->buffer = xmlBufCreateStatic((void *)mem, (size_t) size);
  2656. if (ret->buffer == NULL) {
  2657. xmlFree(ret);
  2658. return(NULL);
  2659. }
  2660. ret->encoder = xmlGetCharEncodingHandler(enc);
  2661. if (ret->encoder != NULL)
  2662. ret->raw = xmlBufCreateSize(2 * xmlDefaultBufferSize);
  2663. else
  2664. ret->raw = NULL;
  2665. ret->compressed = -1;
  2666. ret->context = (void *) mem;
  2667. ret->readcallback = NULL;
  2668. ret->closecallback = NULL;
  2669. return(ret);
  2670. }
  2671. #ifdef LIBXML_OUTPUT_ENABLED
  2672. /**
  2673. * xmlOutputBufferCreateFd:
  2674. * @fd: a file descriptor number
  2675. * @encoder: the encoding converter or NULL
  2676. *
  2677. * Create a buffered output for the progressive saving
  2678. * to a file descriptor
  2679. *
  2680. * Returns the new parser output or NULL
  2681. */
  2682. xmlOutputBufferPtr
  2683. xmlOutputBufferCreateFd(int fd, xmlCharEncodingHandlerPtr encoder) {
  2684. xmlOutputBufferPtr ret;
  2685. if (fd < 0) return(NULL);
  2686. ret = xmlAllocOutputBufferInternal(encoder);
  2687. if (ret != NULL) {
  2688. ret->context = (void *) (ptrdiff_t) fd;
  2689. ret->writecallback = xmlFdWrite;
  2690. ret->closecallback = NULL;
  2691. }
  2692. return(ret);
  2693. }
  2694. #endif /* LIBXML_OUTPUT_ENABLED */
  2695. /**
  2696. * xmlParserInputBufferCreateIO:
  2697. * @ioread: an I/O read function
  2698. * @ioclose: an I/O close function
  2699. * @ioctx: an I/O handler
  2700. * @enc: the charset encoding if known
  2701. *
  2702. * Create a buffered parser input for the progressive parsing for the input
  2703. * from an I/O handler
  2704. *
  2705. * Returns the new parser input or NULL
  2706. */
  2707. xmlParserInputBufferPtr
  2708. xmlParserInputBufferCreateIO(xmlInputReadCallback ioread,
  2709. xmlInputCloseCallback ioclose, void *ioctx, xmlCharEncoding enc) {
  2710. xmlParserInputBufferPtr ret;
  2711. if (ioread == NULL) return(NULL);
  2712. ret = xmlAllocParserInputBuffer(enc);
  2713. if (ret != NULL) {
  2714. ret->context = (void *) ioctx;
  2715. ret->readcallback = ioread;
  2716. ret->closecallback = ioclose;
  2717. }
  2718. return(ret);
  2719. }
  2720. #ifdef LIBXML_OUTPUT_ENABLED
  2721. /**
  2722. * xmlOutputBufferCreateIO:
  2723. * @iowrite: an I/O write function
  2724. * @ioclose: an I/O close function
  2725. * @ioctx: an I/O handler
  2726. * @encoder: the charset encoding if known
  2727. *
  2728. * Create a buffered output for the progressive saving
  2729. * to an I/O handler
  2730. *
  2731. * Returns the new parser output or NULL
  2732. */
  2733. xmlOutputBufferPtr
  2734. xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite,
  2735. xmlOutputCloseCallback ioclose, void *ioctx,
  2736. xmlCharEncodingHandlerPtr encoder) {
  2737. xmlOutputBufferPtr ret;
  2738. if (iowrite == NULL) return(NULL);
  2739. ret = xmlAllocOutputBufferInternal(encoder);
  2740. if (ret != NULL) {
  2741. ret->context = (void *) ioctx;
  2742. ret->writecallback = iowrite;
  2743. ret->closecallback = ioclose;
  2744. }
  2745. return(ret);
  2746. }
  2747. #endif /* LIBXML_OUTPUT_ENABLED */
  2748. /**
  2749. * xmlParserInputBufferCreateFilenameDefault:
  2750. * @func: function pointer to the new ParserInputBufferCreateFilenameFunc
  2751. *
  2752. * Registers a callback for URI input file handling
  2753. *
  2754. * Returns the old value of the registration function
  2755. */
  2756. xmlParserInputBufferCreateFilenameFunc
  2757. xmlParserInputBufferCreateFilenameDefault(xmlParserInputBufferCreateFilenameFunc func)
  2758. {
  2759. xmlParserInputBufferCreateFilenameFunc old = xmlParserInputBufferCreateFilenameValue;
  2760. if (old == NULL) {
  2761. old = __xmlParserInputBufferCreateFilename;
  2762. }
  2763. xmlParserInputBufferCreateFilenameValue = func;
  2764. return(old);
  2765. }
  2766. /**
  2767. * xmlOutputBufferCreateFilenameDefault:
  2768. * @func: function pointer to the new OutputBufferCreateFilenameFunc
  2769. *
  2770. * Registers a callback for URI output file handling
  2771. *
  2772. * Returns the old value of the registration function
  2773. */
  2774. xmlOutputBufferCreateFilenameFunc
  2775. xmlOutputBufferCreateFilenameDefault(xmlOutputBufferCreateFilenameFunc func)
  2776. {
  2777. xmlOutputBufferCreateFilenameFunc old = xmlOutputBufferCreateFilenameValue;
  2778. #ifdef LIBXML_OUTPUT_ENABLED
  2779. if (old == NULL) {
  2780. old = __xmlOutputBufferCreateFilename;
  2781. }
  2782. #endif
  2783. xmlOutputBufferCreateFilenameValue = func;
  2784. return(old);
  2785. }
  2786. /**
  2787. * xmlParserInputBufferPush:
  2788. * @in: a buffered parser input
  2789. * @len: the size in bytes of the array.
  2790. * @buf: an char array
  2791. *
  2792. * Push the content of the arry in the input buffer
  2793. * This routine handle the I18N transcoding to internal UTF-8
  2794. * This is used when operating the parser in progressive (push) mode.
  2795. *
  2796. * Returns the number of chars read and stored in the buffer, or -1
  2797. * in case of error.
  2798. */
  2799. int
  2800. xmlParserInputBufferPush(xmlParserInputBufferPtr in,
  2801. int len, const char *buf) {
  2802. int nbchars = 0;
  2803. int ret;
  2804. if (len < 0) return(0);
  2805. if ((in == NULL) || (in->error)) return(-1);
  2806. if (in->encoder != NULL) {
  2807. unsigned int use;
  2808. /*
  2809. * Store the data in the incoming raw buffer
  2810. */
  2811. if (in->raw == NULL) {
  2812. in->raw = xmlBufCreate();
  2813. }
  2814. ret = xmlBufAdd(in->raw, (const xmlChar *) buf, len);
  2815. if (ret != 0)
  2816. return(-1);
  2817. /*
  2818. * convert as much as possible to the parser reading buffer.
  2819. */
  2820. use = xmlBufUse(in->raw);
  2821. nbchars = xmlCharEncInput(in, 1);
  2822. if (nbchars < 0) {
  2823. xmlIOErr(XML_IO_ENCODER, NULL);
  2824. in->error = XML_IO_ENCODER;
  2825. return(-1);
  2826. }
  2827. in->rawconsumed += (use - xmlBufUse(in->raw));
  2828. } else {
  2829. nbchars = len;
  2830. ret = xmlBufAdd(in->buffer, (xmlChar *) buf, nbchars);
  2831. if (ret != 0)
  2832. return(-1);
  2833. }
  2834. #ifdef DEBUG_INPUT
  2835. xmlGenericError(xmlGenericErrorContext,
  2836. "I/O: pushed %d chars, buffer %d/%d\n",
  2837. nbchars, xmlBufUse(in->buffer), xmlBufLength(in->buffer));
  2838. #endif
  2839. return(nbchars);
  2840. }
  2841. /**
  2842. * endOfInput:
  2843. *
  2844. * When reading from an Input channel indicated end of file or error
  2845. * don't reread from it again.
  2846. */
  2847. static int
  2848. endOfInput (void * context ATTRIBUTE_UNUSED,
  2849. char * buffer ATTRIBUTE_UNUSED,
  2850. int len ATTRIBUTE_UNUSED) {
  2851. return(0);
  2852. }
  2853. /**
  2854. * xmlParserInputBufferGrow:
  2855. * @in: a buffered parser input
  2856. * @len: indicative value of the amount of chars to read
  2857. *
  2858. * Grow up the content of the input buffer, the old data are preserved
  2859. * This routine handle the I18N transcoding to internal UTF-8
  2860. * This routine is used when operating the parser in normal (pull) mode
  2861. *
  2862. * TODO: one should be able to remove one extra copy by copying directly
  2863. * onto in->buffer or in->raw
  2864. *
  2865. * Returns the number of chars read and stored in the buffer, or -1
  2866. * in case of error.
  2867. */
  2868. int
  2869. xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) {
  2870. char *buffer = NULL;
  2871. int res = 0;
  2872. int nbchars = 0;
  2873. if ((in == NULL) || (in->error)) return(-1);
  2874. if ((len <= MINLEN) && (len != 4))
  2875. len = MINLEN;
  2876. if (xmlBufAvail(in->buffer) <= 0) {
  2877. xmlIOErr(XML_IO_BUFFER_FULL, NULL);
  2878. in->error = XML_IO_BUFFER_FULL;
  2879. return(-1);
  2880. }
  2881. if (xmlBufGrow(in->buffer, len + 1) < 0) {
  2882. xmlIOErrMemory("growing input buffer");
  2883. in->error = XML_ERR_NO_MEMORY;
  2884. return(-1);
  2885. }
  2886. buffer = (char *)xmlBufEnd(in->buffer);
  2887. /*
  2888. * Call the read method for this I/O type.
  2889. */
  2890. if (in->readcallback != NULL) {
  2891. res = in->readcallback(in->context, &buffer[0], len);
  2892. if (res <= 0)
  2893. in->readcallback = endOfInput;
  2894. } else {
  2895. xmlIOErr(XML_IO_NO_INPUT, NULL);
  2896. in->error = XML_IO_NO_INPUT;
  2897. return(-1);
  2898. }
  2899. if (res < 0) {
  2900. return(-1);
  2901. }
  2902. /*
  2903. * try to establish compressed status of input if not done already
  2904. */
  2905. if (in->compressed == -1) {
  2906. #ifdef LIBXML_LZMA_ENABLED
  2907. if (in->readcallback == xmlXzfileRead)
  2908. in->compressed = __libxml2_xzcompressed(in->context);
  2909. #endif
  2910. }
  2911. len = res;
  2912. if (in->encoder != NULL) {
  2913. unsigned int use;
  2914. /*
  2915. * Store the data in the incoming raw buffer
  2916. */
  2917. if (in->raw == NULL) {
  2918. in->raw = xmlBufCreate();
  2919. }
  2920. res = xmlBufAdd(in->raw, (const xmlChar *) buffer, len);
  2921. if (res != 0)
  2922. return(-1);
  2923. /*
  2924. * convert as much as possible to the parser reading buffer.
  2925. */
  2926. use = xmlBufUse(in->raw);
  2927. nbchars = xmlCharEncInput(in, 1);
  2928. if (nbchars < 0) {
  2929. xmlIOErr(XML_IO_ENCODER, NULL);
  2930. in->error = XML_IO_ENCODER;
  2931. return(-1);
  2932. }
  2933. in->rawconsumed += (use - xmlBufUse(in->raw));
  2934. } else {
  2935. nbchars = len;
  2936. xmlBufAddLen(in->buffer, nbchars);
  2937. }
  2938. #ifdef DEBUG_INPUT
  2939. xmlGenericError(xmlGenericErrorContext,
  2940. "I/O: read %d chars, buffer %d\n",
  2941. nbchars, xmlBufUse(in->buffer));
  2942. #endif
  2943. return(nbchars);
  2944. }
  2945. /**
  2946. * xmlParserInputBufferRead:
  2947. * @in: a buffered parser input
  2948. * @len: indicative value of the amount of chars to read
  2949. *
  2950. * Refresh the content of the input buffer, the old data are considered
  2951. * consumed
  2952. * This routine handle the I18N transcoding to internal UTF-8
  2953. *
  2954. * Returns the number of chars read and stored in the buffer, or -1
  2955. * in case of error.
  2956. */
  2957. int
  2958. xmlParserInputBufferRead(xmlParserInputBufferPtr in, int len) {
  2959. if ((in == NULL) || (in->error)) return(-1);
  2960. if (in->readcallback != NULL)
  2961. return(xmlParserInputBufferGrow(in, len));
  2962. else if (xmlBufGetAllocationScheme(in->buffer) == XML_BUFFER_ALLOC_IMMUTABLE)
  2963. return(0);
  2964. else
  2965. return(-1);
  2966. }
  2967. #ifdef LIBXML_OUTPUT_ENABLED
  2968. /**
  2969. * xmlOutputBufferWrite:
  2970. * @out: a buffered parser output
  2971. * @len: the size in bytes of the array.
  2972. * @buf: an char array
  2973. *
  2974. * Write the content of the array in the output I/O buffer
  2975. * This routine handle the I18N transcoding from internal UTF-8
  2976. * The buffer is lossless, i.e. will store in case of partial
  2977. * or delayed writes.
  2978. *
  2979. * Returns the number of chars immediately written, or -1
  2980. * in case of error.
  2981. */
  2982. int
  2983. xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) {
  2984. int nbchars = 0; /* number of chars to output to I/O */
  2985. int ret; /* return from function call */
  2986. int written = 0; /* number of char written to I/O so far */
  2987. int chunk; /* number of byte current processed from buf */
  2988. if ((out == NULL) || (out->error)) return(-1);
  2989. if (len < 0) return(0);
  2990. if (out->error) return(-1);
  2991. do {
  2992. chunk = len;
  2993. if (chunk > 4 * MINLEN)
  2994. chunk = 4 * MINLEN;
  2995. /*
  2996. * first handle encoding stuff.
  2997. */
  2998. if (out->encoder != NULL) {
  2999. /*
  3000. * Store the data in the incoming raw buffer
  3001. */
  3002. if (out->conv == NULL) {
  3003. out->conv = xmlBufCreate();
  3004. }
  3005. ret = xmlBufAdd(out->buffer, (const xmlChar *) buf, chunk);
  3006. if (ret != 0)
  3007. return(-1);
  3008. if ((xmlBufUse(out->buffer) < MINLEN) && (chunk == len))
  3009. goto done;
  3010. /*
  3011. * convert as much as possible to the parser reading buffer.
  3012. */
  3013. ret = xmlCharEncOutput(out, 0);
  3014. if ((ret < 0) && (ret != -3)) {
  3015. xmlIOErr(XML_IO_ENCODER, NULL);
  3016. out->error = XML_IO_ENCODER;
  3017. return(-1);
  3018. }
  3019. if (out->writecallback)
  3020. nbchars = xmlBufUse(out->conv);
  3021. else
  3022. nbchars = ret >= 0 ? ret : 0;
  3023. } else {
  3024. ret = xmlBufAdd(out->buffer, (const xmlChar *) buf, chunk);
  3025. if (ret != 0)
  3026. return(-1);
  3027. if (out->writecallback)
  3028. nbchars = xmlBufUse(out->buffer);
  3029. else
  3030. nbchars = chunk;
  3031. }
  3032. buf += chunk;
  3033. len -= chunk;
  3034. if (out->writecallback) {
  3035. if ((nbchars < MINLEN) && (len <= 0))
  3036. goto done;
  3037. /*
  3038. * second write the stuff to the I/O channel
  3039. */
  3040. if (out->encoder != NULL) {
  3041. ret = out->writecallback(out->context,
  3042. (const char *)xmlBufContent(out->conv), nbchars);
  3043. if (ret >= 0)
  3044. xmlBufShrink(out->conv, ret);
  3045. } else {
  3046. ret = out->writecallback(out->context,
  3047. (const char *)xmlBufContent(out->buffer), nbchars);
  3048. if (ret >= 0)
  3049. xmlBufShrink(out->buffer, ret);
  3050. }
  3051. if (ret < 0) {
  3052. xmlIOErr(XML_IO_WRITE, NULL);
  3053. out->error = XML_IO_WRITE;
  3054. return(ret);
  3055. }
  3056. if (out->written > INT_MAX - ret)
  3057. out->written = INT_MAX;
  3058. else
  3059. out->written += ret;
  3060. }
  3061. written += nbchars;
  3062. } while (len > 0);
  3063. done:
  3064. #ifdef DEBUG_INPUT
  3065. xmlGenericError(xmlGenericErrorContext,
  3066. "I/O: wrote %d chars\n", written);
  3067. #endif
  3068. return(written);
  3069. }
  3070. /**
  3071. * xmlEscapeContent:
  3072. * @out: a pointer to an array of bytes to store the result
  3073. * @outlen: the length of @out
  3074. * @in: a pointer to an array of unescaped UTF-8 bytes
  3075. * @inlen: the length of @in
  3076. *
  3077. * Take a block of UTF-8 chars in and escape them.
  3078. * Returns 0 if success, or -1 otherwise
  3079. * The value of @inlen after return is the number of octets consumed
  3080. * if the return value is positive, else unpredictable.
  3081. * The value of @outlen after return is the number of octets consumed.
  3082. */
  3083. static int
  3084. xmlEscapeContent(unsigned char* out, int *outlen,
  3085. const xmlChar* in, int *inlen) {
  3086. unsigned char* outstart = out;
  3087. const unsigned char* base = in;
  3088. unsigned char* outend = out + *outlen;
  3089. const unsigned char* inend;
  3090. inend = in + (*inlen);
  3091. while ((in < inend) && (out < outend)) {
  3092. if (*in == '<') {
  3093. if (outend - out < 4) break;
  3094. *out++ = '&';
  3095. *out++ = 'l';
  3096. *out++ = 't';
  3097. *out++ = ';';
  3098. } else if (*in == '>') {
  3099. if (outend - out < 4) break;
  3100. *out++ = '&';
  3101. *out++ = 'g';
  3102. *out++ = 't';
  3103. *out++ = ';';
  3104. } else if (*in == '&') {
  3105. if (outend - out < 5) break;
  3106. *out++ = '&';
  3107. *out++ = 'a';
  3108. *out++ = 'm';
  3109. *out++ = 'p';
  3110. *out++ = ';';
  3111. } else if (*in == '\r') {
  3112. if (outend - out < 5) break;
  3113. *out++ = '&';
  3114. *out++ = '#';
  3115. *out++ = '1';
  3116. *out++ = '3';
  3117. *out++ = ';';
  3118. } else {
  3119. *out++ = (unsigned char) *in;
  3120. }
  3121. ++in;
  3122. }
  3123. *outlen = out - outstart;
  3124. *inlen = in - base;
  3125. return(0);
  3126. }
  3127. /**
  3128. * xmlOutputBufferWriteEscape:
  3129. * @out: a buffered parser output
  3130. * @str: a zero terminated UTF-8 string
  3131. * @escaping: an optional escaping function (or NULL)
  3132. *
  3133. * Write the content of the string in the output I/O buffer
  3134. * This routine escapes the characters and then handle the I18N
  3135. * transcoding from internal UTF-8
  3136. * The buffer is lossless, i.e. will store in case of partial
  3137. * or delayed writes.
  3138. *
  3139. * Returns the number of chars immediately written, or -1
  3140. * in case of error.
  3141. */
  3142. int
  3143. xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
  3144. xmlCharEncodingOutputFunc escaping) {
  3145. int nbchars = 0; /* number of chars to output to I/O */
  3146. int ret; /* return from function call */
  3147. int written = 0; /* number of char written to I/O so far */
  3148. int oldwritten=0;/* loop guard */
  3149. int chunk; /* number of byte currently processed from str */
  3150. int len; /* number of bytes in str */
  3151. int cons; /* byte from str consumed */
  3152. if ((out == NULL) || (out->error) || (str == NULL) ||
  3153. (out->buffer == NULL) ||
  3154. (xmlBufGetAllocationScheme(out->buffer) == XML_BUFFER_ALLOC_IMMUTABLE))
  3155. return(-1);
  3156. len = strlen((const char *)str);
  3157. if (len < 0) return(0);
  3158. if (out->error) return(-1);
  3159. if (escaping == NULL) escaping = xmlEscapeContent;
  3160. do {
  3161. oldwritten = written;
  3162. /*
  3163. * how many bytes to consume and how many bytes to store.
  3164. */
  3165. cons = len;
  3166. chunk = xmlBufAvail(out->buffer) - 1;
  3167. /*
  3168. * make sure we have enough room to save first, if this is
  3169. * not the case force a flush, but make sure we stay in the loop
  3170. */
  3171. if (chunk < 40) {
  3172. if (xmlBufGrow(out->buffer, 100) < 0)
  3173. return(-1);
  3174. oldwritten = -1;
  3175. continue;
  3176. }
  3177. /*
  3178. * first handle encoding stuff.
  3179. */
  3180. if (out->encoder != NULL) {
  3181. /*
  3182. * Store the data in the incoming raw buffer
  3183. */
  3184. if (out->conv == NULL) {
  3185. out->conv = xmlBufCreate();
  3186. }
  3187. ret = escaping(xmlBufEnd(out->buffer) ,
  3188. &chunk, str, &cons);
  3189. if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */
  3190. return(-1);
  3191. xmlBufAddLen(out->buffer, chunk);
  3192. if ((xmlBufUse(out->buffer) < MINLEN) && (cons == len))
  3193. goto done;
  3194. /*
  3195. * convert as much as possible to the output buffer.
  3196. */
  3197. ret = xmlCharEncOutput(out, 0);
  3198. if ((ret < 0) && (ret != -3)) {
  3199. xmlIOErr(XML_IO_ENCODER, NULL);
  3200. out->error = XML_IO_ENCODER;
  3201. return(-1);
  3202. }
  3203. if (out->writecallback)
  3204. nbchars = xmlBufUse(out->conv);
  3205. else
  3206. nbchars = ret >= 0 ? ret : 0;
  3207. } else {
  3208. ret = escaping(xmlBufEnd(out->buffer), &chunk, str, &cons);
  3209. if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */
  3210. return(-1);
  3211. xmlBufAddLen(out->buffer, chunk);
  3212. if (out->writecallback)
  3213. nbchars = xmlBufUse(out->buffer);
  3214. else
  3215. nbchars = chunk;
  3216. }
  3217. str += cons;
  3218. len -= cons;
  3219. if (out->writecallback) {
  3220. if ((nbchars < MINLEN) && (len <= 0))
  3221. goto done;
  3222. /*
  3223. * second write the stuff to the I/O channel
  3224. */
  3225. if (out->encoder != NULL) {
  3226. ret = out->writecallback(out->context,
  3227. (const char *)xmlBufContent(out->conv), nbchars);
  3228. if (ret >= 0)
  3229. xmlBufShrink(out->conv, ret);
  3230. } else {
  3231. ret = out->writecallback(out->context,
  3232. (const char *)xmlBufContent(out->buffer), nbchars);
  3233. if (ret >= 0)
  3234. xmlBufShrink(out->buffer, ret);
  3235. }
  3236. if (ret < 0) {
  3237. xmlIOErr(XML_IO_WRITE, NULL);
  3238. out->error = XML_IO_WRITE;
  3239. return(ret);
  3240. }
  3241. if (out->written > INT_MAX - ret)
  3242. out->written = INT_MAX;
  3243. else
  3244. out->written += ret;
  3245. } else if (xmlBufAvail(out->buffer) < MINLEN) {
  3246. xmlBufGrow(out->buffer, MINLEN);
  3247. }
  3248. written += nbchars;
  3249. } while ((len > 0) && (oldwritten != written));
  3250. done:
  3251. #ifdef DEBUG_INPUT
  3252. xmlGenericError(xmlGenericErrorContext,
  3253. "I/O: wrote %d chars\n", written);
  3254. #endif
  3255. return(written);
  3256. }
  3257. /**
  3258. * xmlOutputBufferWriteString:
  3259. * @out: a buffered parser output
  3260. * @str: a zero terminated C string
  3261. *
  3262. * Write the content of the string in the output I/O buffer
  3263. * This routine handle the I18N transcoding from internal UTF-8
  3264. * The buffer is lossless, i.e. will store in case of partial
  3265. * or delayed writes.
  3266. *
  3267. * Returns the number of chars immediately written, or -1
  3268. * in case of error.
  3269. */
  3270. int
  3271. xmlOutputBufferWriteString(xmlOutputBufferPtr out, const char *str) {
  3272. int len;
  3273. if ((out == NULL) || (out->error)) return(-1);
  3274. if (str == NULL)
  3275. return(-1);
  3276. len = strlen(str);
  3277. if (len > 0)
  3278. return(xmlOutputBufferWrite(out, len, str));
  3279. return(len);
  3280. }
  3281. /**
  3282. * xmlOutputBufferFlush:
  3283. * @out: a buffered output
  3284. *
  3285. * flushes the output I/O channel
  3286. *
  3287. * Returns the number of byte written or -1 in case of error.
  3288. */
  3289. int
  3290. xmlOutputBufferFlush(xmlOutputBufferPtr out) {
  3291. int nbchars = 0, ret = 0;
  3292. if ((out == NULL) || (out->error)) return(-1);
  3293. /*
  3294. * first handle encoding stuff.
  3295. */
  3296. if ((out->conv != NULL) && (out->encoder != NULL)) {
  3297. /*
  3298. * convert as much as possible to the parser output buffer.
  3299. */
  3300. do {
  3301. nbchars = xmlCharEncOutput(out, 0);
  3302. if (nbchars < 0) {
  3303. xmlIOErr(XML_IO_ENCODER, NULL);
  3304. out->error = XML_IO_ENCODER;
  3305. return(-1);
  3306. }
  3307. } while (nbchars);
  3308. }
  3309. /*
  3310. * second flush the stuff to the I/O channel
  3311. */
  3312. if ((out->conv != NULL) && (out->encoder != NULL) &&
  3313. (out->writecallback != NULL)) {
  3314. ret = out->writecallback(out->context,
  3315. (const char *)xmlBufContent(out->conv),
  3316. xmlBufUse(out->conv));
  3317. if (ret >= 0)
  3318. xmlBufShrink(out->conv, ret);
  3319. } else if (out->writecallback != NULL) {
  3320. ret = out->writecallback(out->context,
  3321. (const char *)xmlBufContent(out->buffer),
  3322. xmlBufUse(out->buffer));
  3323. if (ret >= 0)
  3324. xmlBufShrink(out->buffer, ret);
  3325. }
  3326. if (ret < 0) {
  3327. xmlIOErr(XML_IO_FLUSH, NULL);
  3328. out->error = XML_IO_FLUSH;
  3329. return(ret);
  3330. }
  3331. if (out->written > INT_MAX - ret)
  3332. out->written = INT_MAX;
  3333. else
  3334. out->written += ret;
  3335. #ifdef DEBUG_INPUT
  3336. xmlGenericError(xmlGenericErrorContext,
  3337. "I/O: flushed %d chars\n", ret);
  3338. #endif
  3339. return(ret);
  3340. }
  3341. #endif /* LIBXML_OUTPUT_ENABLED */
  3342. /**
  3343. * xmlParserGetDirectory:
  3344. * @filename: the path to a file
  3345. *
  3346. * lookup the directory for that file
  3347. *
  3348. * Returns a new allocated string containing the directory, or NULL.
  3349. */
  3350. char *
  3351. xmlParserGetDirectory(const char *filename) {
  3352. char *ret = NULL;
  3353. char dir[1024];
  3354. char *cur;
  3355. #ifdef _WIN32_WCE /* easy way by now ... wince does not have dirs! */
  3356. return NULL;
  3357. #endif
  3358. if (xmlInputCallbackInitialized == 0)
  3359. xmlRegisterDefaultInputCallbacks();
  3360. if (filename == NULL) return(NULL);
  3361. #if defined(_WIN32) && !defined(__CYGWIN__)
  3362. # define IS_XMLPGD_SEP(ch) ((ch=='/')||(ch=='\\'))
  3363. #else
  3364. # define IS_XMLPGD_SEP(ch) (ch=='/')
  3365. #endif
  3366. strncpy(dir, filename, 1023);
  3367. dir[1023] = 0;
  3368. cur = &dir[strlen(dir)];
  3369. while (cur > dir) {
  3370. if (IS_XMLPGD_SEP(*cur)) break;
  3371. cur --;
  3372. }
  3373. if (IS_XMLPGD_SEP(*cur)) {
  3374. if (cur == dir) dir[1] = 0;
  3375. else *cur = 0;
  3376. ret = xmlMemStrdup(dir);
  3377. } else {
  3378. if (getcwd(dir, 1024) != NULL) {
  3379. dir[1023] = 0;
  3380. ret = xmlMemStrdup(dir);
  3381. }
  3382. }
  3383. return(ret);
  3384. #undef IS_XMLPGD_SEP
  3385. }
  3386. /****************************************************************
  3387. * *
  3388. * External entities loading *
  3389. * *
  3390. ****************************************************************/
  3391. /**
  3392. * xmlCheckHTTPInput:
  3393. * @ctxt: an XML parser context
  3394. * @ret: an XML parser input
  3395. *
  3396. * Check an input in case it was created from an HTTP stream, in that
  3397. * case it will handle encoding and update of the base URL in case of
  3398. * redirection. It also checks for HTTP errors in which case the input
  3399. * is cleanly freed up and an appropriate error is raised in context
  3400. *
  3401. * Returns the input or NULL in case of HTTP error.
  3402. */
  3403. xmlParserInputPtr
  3404. xmlCheckHTTPInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr ret) {
  3405. #ifdef LIBXML_HTTP_ENABLED
  3406. if ((ret != NULL) && (ret->buf != NULL) &&
  3407. (ret->buf->readcallback == xmlIOHTTPRead) &&
  3408. (ret->buf->context != NULL)) {
  3409. const char *encoding;
  3410. const char *redir;
  3411. const char *mime;
  3412. int code;
  3413. code = xmlNanoHTTPReturnCode(ret->buf->context);
  3414. if (code >= 400) {
  3415. /* fatal error */
  3416. if (ret->filename != NULL)
  3417. __xmlLoaderErr(ctxt, "failed to load HTTP resource \"%s\"\n",
  3418. (const char *) ret->filename);
  3419. else
  3420. __xmlLoaderErr(ctxt, "failed to load HTTP resource\n", NULL);
  3421. xmlFreeInputStream(ret);
  3422. ret = NULL;
  3423. } else {
  3424. mime = xmlNanoHTTPMimeType(ret->buf->context);
  3425. if ((xmlStrstr(BAD_CAST mime, BAD_CAST "/xml")) ||
  3426. (xmlStrstr(BAD_CAST mime, BAD_CAST "+xml"))) {
  3427. encoding = xmlNanoHTTPEncoding(ret->buf->context);
  3428. if (encoding != NULL) {
  3429. xmlCharEncodingHandlerPtr handler;
  3430. handler = xmlFindCharEncodingHandler(encoding);
  3431. if (handler != NULL) {
  3432. xmlSwitchInputEncoding(ctxt, ret, handler);
  3433. } else {
  3434. __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
  3435. "Unknown encoding %s",
  3436. BAD_CAST encoding, NULL);
  3437. }
  3438. if (ret->encoding == NULL)
  3439. ret->encoding = xmlStrdup(BAD_CAST encoding);
  3440. }
  3441. #if 0
  3442. } else if (xmlStrstr(BAD_CAST mime, BAD_CAST "html")) {
  3443. #endif
  3444. }
  3445. redir = xmlNanoHTTPRedir(ret->buf->context);
  3446. if (redir != NULL) {
  3447. if (ret->filename != NULL)
  3448. xmlFree((xmlChar *) ret->filename);
  3449. if (ret->directory != NULL) {
  3450. xmlFree((xmlChar *) ret->directory);
  3451. ret->directory = NULL;
  3452. }
  3453. ret->filename =
  3454. (char *) xmlStrdup((const xmlChar *) redir);
  3455. }
  3456. }
  3457. }
  3458. #endif
  3459. return(ret);
  3460. }
  3461. static int xmlNoNetExists(const char *URL) {
  3462. const char *path;
  3463. if (URL == NULL)
  3464. return(0);
  3465. if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file://localhost/", 17))
  3466. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  3467. path = &URL[17];
  3468. #else
  3469. path = &URL[16];
  3470. #endif
  3471. else if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file:///", 8)) {
  3472. #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
  3473. path = &URL[8];
  3474. #else
  3475. path = &URL[7];
  3476. #endif
  3477. } else
  3478. path = URL;
  3479. return xmlCheckFilename(path);
  3480. }
  3481. #ifdef LIBXML_CATALOG_ENABLED
  3482. /**
  3483. * xmlResolveResourceFromCatalog:
  3484. * @URL: the URL for the entity to load
  3485. * @ID: the System ID for the entity to load
  3486. * @ctxt: the context in which the entity is called or NULL
  3487. *
  3488. * Resolves the URL and ID against the appropriate catalog.
  3489. * This function is used by xmlDefaultExternalEntityLoader and
  3490. * xmlNoNetExternalEntityLoader.
  3491. *
  3492. * Returns a new allocated URL, or NULL.
  3493. */
  3494. static xmlChar *
  3495. xmlResolveResourceFromCatalog(const char *URL, const char *ID,
  3496. xmlParserCtxtPtr ctxt) {
  3497. xmlChar *resource = NULL;
  3498. xmlCatalogAllow pref;
  3499. /*
  3500. * If the resource doesn't exists as a file,
  3501. * try to load it from the resource pointed in the catalogs
  3502. */
  3503. pref = xmlCatalogGetDefaults();
  3504. if ((pref != XML_CATA_ALLOW_NONE) && (!xmlNoNetExists(URL))) {
  3505. /*
  3506. * Do a local lookup
  3507. */
  3508. if ((ctxt != NULL) && (ctxt->catalogs != NULL) &&
  3509. ((pref == XML_CATA_ALLOW_ALL) ||
  3510. (pref == XML_CATA_ALLOW_DOCUMENT))) {
  3511. resource = xmlCatalogLocalResolve(ctxt->catalogs,
  3512. (const xmlChar *)ID,
  3513. (const xmlChar *)URL);
  3514. }
  3515. /*
  3516. * Try a global lookup
  3517. */
  3518. if ((resource == NULL) &&
  3519. ((pref == XML_CATA_ALLOW_ALL) ||
  3520. (pref == XML_CATA_ALLOW_GLOBAL))) {
  3521. resource = xmlCatalogResolve((const xmlChar *)ID,
  3522. (const xmlChar *)URL);
  3523. }
  3524. if ((resource == NULL) && (URL != NULL))
  3525. resource = xmlStrdup((const xmlChar *) URL);
  3526. /*
  3527. * TODO: do an URI lookup on the reference
  3528. */
  3529. if ((resource != NULL) && (!xmlNoNetExists((const char *)resource))) {
  3530. xmlChar *tmp = NULL;
  3531. if ((ctxt != NULL) && (ctxt->catalogs != NULL) &&
  3532. ((pref == XML_CATA_ALLOW_ALL) ||
  3533. (pref == XML_CATA_ALLOW_DOCUMENT))) {
  3534. tmp = xmlCatalogLocalResolveURI(ctxt->catalogs, resource);
  3535. }
  3536. if ((tmp == NULL) &&
  3537. ((pref == XML_CATA_ALLOW_ALL) ||
  3538. (pref == XML_CATA_ALLOW_GLOBAL))) {
  3539. tmp = xmlCatalogResolveURI(resource);
  3540. }
  3541. if (tmp != NULL) {
  3542. xmlFree(resource);
  3543. resource = tmp;
  3544. }
  3545. }
  3546. }
  3547. return resource;
  3548. }
  3549. #endif
  3550. /**
  3551. * xmlDefaultExternalEntityLoader:
  3552. * @URL: the URL for the entity to load
  3553. * @ID: the System ID for the entity to load
  3554. * @ctxt: the context in which the entity is called or NULL
  3555. *
  3556. * By default we don't load external entities, yet.
  3557. *
  3558. * Returns a new allocated xmlParserInputPtr, or NULL.
  3559. */
  3560. static xmlParserInputPtr
  3561. xmlDefaultExternalEntityLoader(const char *URL, const char *ID,
  3562. xmlParserCtxtPtr ctxt)
  3563. {
  3564. xmlParserInputPtr ret = NULL;
  3565. xmlChar *resource = NULL;
  3566. #ifdef DEBUG_EXTERNAL_ENTITIES
  3567. xmlGenericError(xmlGenericErrorContext,
  3568. "xmlDefaultExternalEntityLoader(%s, xxx)\n", URL);
  3569. #endif
  3570. if ((ctxt != NULL) && (ctxt->options & XML_PARSE_NONET)) {
  3571. int options = ctxt->options;
  3572. ctxt->options -= XML_PARSE_NONET;
  3573. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  3574. ctxt->options = options;
  3575. return(ret);
  3576. }
  3577. #ifdef LIBXML_CATALOG_ENABLED
  3578. resource = xmlResolveResourceFromCatalog(URL, ID, ctxt);
  3579. #endif
  3580. if (resource == NULL)
  3581. resource = (xmlChar *) URL;
  3582. if (resource == NULL) {
  3583. if (ID == NULL)
  3584. ID = "NULL";
  3585. __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n", ID);
  3586. return (NULL);
  3587. }
  3588. ret = xmlNewInputFromFile(ctxt, (const char *) resource);
  3589. if ((resource != NULL) && (resource != (xmlChar *) URL))
  3590. xmlFree(resource);
  3591. return (ret);
  3592. }
  3593. static xmlExternalEntityLoader xmlCurrentExternalEntityLoader =
  3594. xmlDefaultExternalEntityLoader;
  3595. /**
  3596. * xmlSetExternalEntityLoader:
  3597. * @f: the new entity resolver function
  3598. *
  3599. * Changes the defaultexternal entity resolver function for the application
  3600. */
  3601. void
  3602. xmlSetExternalEntityLoader(xmlExternalEntityLoader f) {
  3603. xmlCurrentExternalEntityLoader = f;
  3604. }
  3605. /**
  3606. * xmlGetExternalEntityLoader:
  3607. *
  3608. * Get the default external entity resolver function for the application
  3609. *
  3610. * Returns the xmlExternalEntityLoader function pointer
  3611. */
  3612. xmlExternalEntityLoader
  3613. xmlGetExternalEntityLoader(void) {
  3614. return(xmlCurrentExternalEntityLoader);
  3615. }
  3616. /**
  3617. * xmlLoadExternalEntity:
  3618. * @URL: the URL for the entity to load
  3619. * @ID: the Public ID for the entity to load
  3620. * @ctxt: the context in which the entity is called or NULL
  3621. *
  3622. * Load an external entity, note that the use of this function for
  3623. * unparsed entities may generate problems
  3624. *
  3625. * Returns the xmlParserInputPtr or NULL
  3626. */
  3627. xmlParserInputPtr
  3628. xmlLoadExternalEntity(const char *URL, const char *ID,
  3629. xmlParserCtxtPtr ctxt) {
  3630. if ((URL != NULL) && (xmlNoNetExists(URL) == 0)) {
  3631. char *canonicFilename;
  3632. xmlParserInputPtr ret;
  3633. canonicFilename = (char *) xmlCanonicPath((const xmlChar *) URL);
  3634. if (canonicFilename == NULL) {
  3635. xmlIOErrMemory("building canonical path\n");
  3636. return(NULL);
  3637. }
  3638. ret = xmlCurrentExternalEntityLoader(canonicFilename, ID, ctxt);
  3639. xmlFree(canonicFilename);
  3640. return(ret);
  3641. }
  3642. return(xmlCurrentExternalEntityLoader(URL, ID, ctxt));
  3643. }
  3644. /************************************************************************
  3645. * *
  3646. * Disabling Network access *
  3647. * *
  3648. ************************************************************************/
  3649. /**
  3650. * xmlNoNetExternalEntityLoader:
  3651. * @URL: the URL for the entity to load
  3652. * @ID: the System ID for the entity to load
  3653. * @ctxt: the context in which the entity is called or NULL
  3654. *
  3655. * A specific entity loader disabling network accesses, though still
  3656. * allowing local catalog accesses for resolution.
  3657. *
  3658. * Returns a new allocated xmlParserInputPtr, or NULL.
  3659. */
  3660. xmlParserInputPtr
  3661. xmlNoNetExternalEntityLoader(const char *URL, const char *ID,
  3662. xmlParserCtxtPtr ctxt) {
  3663. xmlParserInputPtr input = NULL;
  3664. xmlChar *resource = NULL;
  3665. #ifdef LIBXML_CATALOG_ENABLED
  3666. resource = xmlResolveResourceFromCatalog(URL, ID, ctxt);
  3667. #endif
  3668. if (resource == NULL)
  3669. resource = (xmlChar *) URL;
  3670. if (resource != NULL) {
  3671. if ((!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "ftp://", 6)) ||
  3672. (!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "http://", 7))) {
  3673. xmlIOErr(XML_IO_NETWORK_ATTEMPT, (const char *) resource);
  3674. if (resource != (xmlChar *) URL)
  3675. xmlFree(resource);
  3676. return(NULL);
  3677. }
  3678. }
  3679. input = xmlDefaultExternalEntityLoader((const char *) resource, ID, ctxt);
  3680. if (resource != (xmlChar *) URL)
  3681. xmlFree(resource);
  3682. return(input);
  3683. }
  3684. #define bottom_xmlIO
  3685. #include "elfgcchack.h"