xmlreader.c 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033
  1. /*
  2. * xmlreader.c: implements the xmlTextReader streaming node API
  3. *
  4. * NOTE:
  5. * XmlTextReader.Normalization Property won't be supported, since
  6. * it makes the parser non compliant to the XML recommendation
  7. *
  8. * See Copyright for the status of this software.
  9. *
  10. * daniel@veillard.com
  11. */
  12. /*
  13. * TODOs:
  14. * - XML Schemas validation
  15. */
  16. #define IN_LIBXML
  17. #include "libxml.h"
  18. #ifdef LIBXML_READER_ENABLED
  19. #include <string.h> /* for memset() only ! */
  20. #include <stdarg.h>
  21. #ifdef HAVE_CTYPE_H
  22. #include <ctype.h>
  23. #endif
  24. #ifdef HAVE_STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. #include <libxml/xmlmemory.h>
  28. #include <libxml/xmlIO.h>
  29. #include <libxml/xmlreader.h>
  30. #include <libxml/parserInternals.h>
  31. #ifdef LIBXML_SCHEMAS_ENABLED
  32. #include <libxml/relaxng.h>
  33. #include <libxml/xmlschemas.h>
  34. #endif
  35. #include <libxml/uri.h>
  36. #ifdef LIBXML_XINCLUDE_ENABLED
  37. #include <libxml/xinclude.h>
  38. #endif
  39. #ifdef LIBXML_PATTERN_ENABLED
  40. #include <libxml/pattern.h>
  41. #endif
  42. #include "buf.h"
  43. #define MAX_ERR_MSG_SIZE 64000
  44. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  45. /* Keeping free objects can hide memory errors. */
  46. #define MAX_FREE_NODES 1
  47. #else
  48. #define MAX_FREE_NODES 100
  49. #endif
  50. /*
  51. * The following VA_COPY was coded following an example in
  52. * the Samba project. It may not be sufficient for some
  53. * esoteric implementations of va_list but (hopefully) will
  54. * be sufficient for libxml2.
  55. */
  56. #ifndef VA_COPY
  57. #ifdef HAVE_VA_COPY
  58. #define VA_COPY(dest, src) va_copy(dest, src)
  59. #else
  60. #ifdef HAVE___VA_COPY
  61. #define VA_COPY(dest,src) __va_copy(dest, src)
  62. #else
  63. #ifndef VA_LIST_IS_ARRAY
  64. #define VA_COPY(dest,src) (dest) = (src)
  65. #else
  66. #include <string.h>
  67. #define VA_COPY(dest,src) memcpy((char *)(dest),(char *)(src),sizeof(va_list))
  68. #endif
  69. #endif
  70. #endif
  71. #endif
  72. /* #define DEBUG_CALLBACKS */
  73. /* #define DEBUG_READER */
  74. /**
  75. * TODO:
  76. *
  77. * macro to flag unimplemented blocks
  78. */
  79. #define TODO \
  80. xmlGenericError(xmlGenericErrorContext, \
  81. "Unimplemented block at %s:%d\n", \
  82. __FILE__, __LINE__);
  83. #ifdef DEBUG_READER
  84. #define DUMP_READER xmlTextReaderDebug(reader);
  85. #else
  86. #define DUMP_READER
  87. #endif
  88. #define CHUNK_SIZE 512
  89. /************************************************************************
  90. * *
  91. * The parser: maps the Text Reader API on top of the existing *
  92. * parsing routines building a tree *
  93. * *
  94. ************************************************************************/
  95. #define XML_TEXTREADER_INPUT 1
  96. #define XML_TEXTREADER_CTXT 2
  97. typedef enum {
  98. XML_TEXTREADER_NONE = -1,
  99. XML_TEXTREADER_START= 0,
  100. XML_TEXTREADER_ELEMENT= 1,
  101. XML_TEXTREADER_END= 2,
  102. XML_TEXTREADER_EMPTY= 3,
  103. XML_TEXTREADER_BACKTRACK= 4,
  104. XML_TEXTREADER_DONE= 5,
  105. XML_TEXTREADER_ERROR= 6
  106. } xmlTextReaderState;
  107. typedef enum {
  108. XML_TEXTREADER_NOT_VALIDATE = 0,
  109. XML_TEXTREADER_VALIDATE_DTD = 1,
  110. XML_TEXTREADER_VALIDATE_RNG = 2,
  111. XML_TEXTREADER_VALIDATE_XSD = 4
  112. } xmlTextReaderValidate;
  113. struct _xmlTextReader {
  114. int mode; /* the parsing mode */
  115. xmlDocPtr doc; /* when walking an existing doc */
  116. xmlTextReaderValidate validate;/* is there any validation */
  117. int allocs; /* what structure were deallocated */
  118. xmlTextReaderState state;
  119. xmlParserCtxtPtr ctxt; /* the parser context */
  120. xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
  121. xmlParserInputBufferPtr input; /* the input */
  122. startElementSAXFunc startElement;/* initial SAX callbacks */
  123. endElementSAXFunc endElement; /* idem */
  124. startElementNsSAX2Func startElementNs;/* idem */
  125. endElementNsSAX2Func endElementNs; /* idem */
  126. charactersSAXFunc characters;
  127. cdataBlockSAXFunc cdataBlock;
  128. unsigned int base; /* base of the segment in the input */
  129. unsigned int cur; /* current position in the input */
  130. xmlNodePtr node; /* current node */
  131. xmlNodePtr curnode;/* current attribute node */
  132. int depth; /* depth of the current node */
  133. xmlNodePtr faketext;/* fake xmlNs chld */
  134. int preserve;/* preserve the resulting document */
  135. xmlBufPtr buffer; /* used to return const xmlChar * */
  136. xmlDictPtr dict; /* the context dictionary */
  137. /* entity stack when traversing entities content */
  138. xmlNodePtr ent; /* Current Entity Ref Node */
  139. int entNr; /* Depth of the entities stack */
  140. int entMax; /* Max depth of the entities stack */
  141. xmlNodePtr *entTab; /* array of entities */
  142. /* error handling */
  143. xmlTextReaderErrorFunc errorFunc; /* callback function */
  144. void *errorFuncArg; /* callback function user argument */
  145. #ifdef LIBXML_SCHEMAS_ENABLED
  146. /* Handling of RelaxNG validation */
  147. xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */
  148. xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */
  149. int rngPreserveCtxt; /* 1 if the context was provided by the user */
  150. int rngValidErrors;/* The number of errors detected */
  151. xmlNodePtr rngFullNode; /* the node if RNG not progressive */
  152. /* Handling of Schemas validation */
  153. xmlSchemaPtr xsdSchemas; /* The Schemas schemas */
  154. xmlSchemaValidCtxtPtr xsdValidCtxt;/* The Schemas validation context */
  155. int xsdPreserveCtxt; /* 1 if the context was provided by the user */
  156. int xsdValidErrors;/* The number of errors detected */
  157. xmlSchemaSAXPlugPtr xsdPlug; /* the schemas plug in SAX pipeline */
  158. #endif
  159. #ifdef LIBXML_XINCLUDE_ENABLED
  160. /* Handling of XInclude processing */
  161. int xinclude; /* is xinclude asked for */
  162. const xmlChar * xinclude_name; /* the xinclude name from dict */
  163. xmlXIncludeCtxtPtr xincctxt; /* the xinclude context */
  164. int in_xinclude; /* counts for xinclude */
  165. #endif
  166. #ifdef LIBXML_PATTERN_ENABLED
  167. int patternNr; /* number of preserve patterns */
  168. int patternMax; /* max preserve patterns */
  169. xmlPatternPtr *patternTab; /* array of preserve patterns */
  170. #endif
  171. int preserves; /* level of preserves */
  172. int parserFlags; /* the set of options set */
  173. /* Structured error handling */
  174. xmlStructuredErrorFunc sErrorFunc; /* callback function */
  175. };
  176. #define NODE_IS_EMPTY 0x1
  177. #define NODE_IS_PRESERVED 0x2
  178. #define NODE_IS_SPRESERVED 0x4
  179. /**
  180. * CONSTSTR:
  181. *
  182. * Macro used to return an interned string
  183. */
  184. #define CONSTSTR(str) xmlDictLookup(reader->dict, (str), -1)
  185. #define CONSTQSTR(p, str) xmlDictQLookup(reader->dict, (p), (str))
  186. static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
  187. static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
  188. /************************************************************************
  189. * *
  190. * Our own version of the freeing routines as we recycle nodes *
  191. * *
  192. ************************************************************************/
  193. /**
  194. * DICT_FREE:
  195. * @str: a string
  196. *
  197. * Free a string if it is not owned by the "dict" dictionary in the
  198. * current scope
  199. */
  200. #define DICT_FREE(str) \
  201. if ((str) && ((!dict) || \
  202. (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
  203. xmlFree((char *)(str));
  204. static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
  205. static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
  206. /**
  207. * xmlFreeID:
  208. * @not: A id
  209. *
  210. * Deallocate the memory used by an id definition
  211. */
  212. static void
  213. xmlFreeID(xmlIDPtr id) {
  214. xmlDictPtr dict = NULL;
  215. if (id == NULL) return;
  216. if (id->doc != NULL)
  217. dict = id->doc->dict;
  218. if (id->value != NULL)
  219. DICT_FREE(id->value)
  220. if (id->name != NULL)
  221. DICT_FREE(id->name)
  222. xmlFree(id);
  223. }
  224. /**
  225. * xmlTextReaderRemoveID:
  226. * @doc: the document
  227. * @attr: the attribute
  228. *
  229. * Remove the given attribute from the ID table maintained internally.
  230. *
  231. * Returns -1 if the lookup failed and 0 otherwise
  232. */
  233. static int
  234. xmlTextReaderRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
  235. xmlIDTablePtr table;
  236. xmlIDPtr id;
  237. xmlChar *ID;
  238. if (doc == NULL) return(-1);
  239. if (attr == NULL) return(-1);
  240. table = (xmlIDTablePtr) doc->ids;
  241. if (table == NULL)
  242. return(-1);
  243. ID = xmlNodeListGetString(doc, attr->children, 1);
  244. if (ID == NULL)
  245. return(-1);
  246. id = xmlHashLookup(table, ID);
  247. xmlFree(ID);
  248. if (id == NULL || id->attr != attr) {
  249. return(-1);
  250. }
  251. id->name = attr->name;
  252. attr->name = NULL;
  253. id->attr = NULL;
  254. return(0);
  255. }
  256. /**
  257. * xmlTextReaderWalkRemoveRef:
  258. * @data: Contents of current link
  259. * @user: Value supplied by the user
  260. *
  261. * Returns 0 to abort the walk or 1 to continue
  262. */
  263. static int
  264. xmlTextReaderWalkRemoveRef(const void *data, void *user)
  265. {
  266. xmlRefPtr ref = (xmlRefPtr)data;
  267. xmlAttrPtr attr = (xmlAttrPtr)user;
  268. if (ref->attr == attr) { /* Matched: remove and terminate walk */
  269. ref->name = xmlStrdup(attr->name);
  270. ref->attr = NULL;
  271. return 0;
  272. }
  273. return 1;
  274. }
  275. /**
  276. * xmlTextReaderRemoveRef:
  277. * @doc: the document
  278. * @attr: the attribute
  279. *
  280. * Remove the given attribute from the Ref table maintained internally.
  281. *
  282. * Returns -1 if the lookup failed and 0 otherwise
  283. */
  284. static int
  285. xmlTextReaderRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
  286. xmlListPtr ref_list;
  287. xmlRefTablePtr table;
  288. xmlChar *ID;
  289. if (doc == NULL) return(-1);
  290. if (attr == NULL) return(-1);
  291. table = (xmlRefTablePtr) doc->refs;
  292. if (table == NULL)
  293. return(-1);
  294. ID = xmlNodeListGetString(doc, attr->children, 1);
  295. if (ID == NULL)
  296. return(-1);
  297. ref_list = xmlHashLookup(table, ID);
  298. xmlFree(ID);
  299. if(ref_list == NULL)
  300. return (-1);
  301. xmlListWalk(ref_list, xmlTextReaderWalkRemoveRef, attr);
  302. return(0);
  303. }
  304. /**
  305. * xmlTextReaderFreeProp:
  306. * @reader: the xmlTextReaderPtr used
  307. * @cur: the node
  308. *
  309. * Free a node.
  310. */
  311. static void
  312. xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
  313. xmlDictPtr dict;
  314. if ((reader != NULL) && (reader->ctxt != NULL))
  315. dict = reader->ctxt->dict;
  316. else
  317. dict = NULL;
  318. if (cur == NULL) return;
  319. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  320. xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
  321. /* Check for ID removal -> leading to invalid references ! */
  322. if ((cur->parent != NULL) && (cur->parent->doc != NULL)) {
  323. if (xmlIsID(cur->parent->doc, cur->parent, cur))
  324. xmlTextReaderRemoveID(cur->parent->doc, cur);
  325. if (((cur->parent->doc->intSubset != NULL) ||
  326. (cur->parent->doc->extSubset != NULL)) &&
  327. (xmlIsRef(cur->parent->doc, cur->parent, cur)))
  328. xmlTextReaderRemoveRef(cur->parent->doc, cur);
  329. }
  330. if (cur->children != NULL)
  331. xmlTextReaderFreeNodeList(reader, cur->children);
  332. DICT_FREE(cur->name);
  333. if ((reader != NULL) && (reader->ctxt != NULL) &&
  334. (reader->ctxt->freeAttrsNr < MAX_FREE_NODES)) {
  335. cur->next = reader->ctxt->freeAttrs;
  336. reader->ctxt->freeAttrs = cur;
  337. reader->ctxt->freeAttrsNr++;
  338. } else {
  339. xmlFree(cur);
  340. }
  341. }
  342. /**
  343. * xmlTextReaderFreePropList:
  344. * @reader: the xmlTextReaderPtr used
  345. * @cur: the first property in the list
  346. *
  347. * Free a property and all its siblings, all the children are freed too.
  348. */
  349. static void
  350. xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
  351. xmlAttrPtr next;
  352. while (cur != NULL) {
  353. next = cur->next;
  354. xmlTextReaderFreeProp(reader, cur);
  355. cur = next;
  356. }
  357. }
  358. /**
  359. * xmlTextReaderFreeNodeList:
  360. * @reader: the xmlTextReaderPtr used
  361. * @cur: the first node in the list
  362. *
  363. * Free a node and all its siblings, this is a recursive behaviour, all
  364. * the children are freed too.
  365. */
  366. static void
  367. xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
  368. xmlNodePtr next;
  369. xmlNodePtr parent;
  370. xmlDictPtr dict;
  371. size_t depth = 0;
  372. if ((reader != NULL) && (reader->ctxt != NULL))
  373. dict = reader->ctxt->dict;
  374. else
  375. dict = NULL;
  376. if (cur == NULL) return;
  377. if (cur->type == XML_NAMESPACE_DECL) {
  378. xmlFreeNsList((xmlNsPtr) cur);
  379. return;
  380. }
  381. if ((cur->type == XML_DOCUMENT_NODE) ||
  382. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  383. xmlFreeDoc((xmlDocPtr) cur);
  384. return;
  385. }
  386. while (1) {
  387. while ((cur->type != XML_DTD_NODE) &&
  388. (cur->type != XML_ENTITY_REF_NODE) &&
  389. (cur->children != NULL) &&
  390. (cur->children->parent == cur)) {
  391. cur = cur->children;
  392. depth += 1;
  393. }
  394. next = cur->next;
  395. parent = cur->parent;
  396. /* unroll to speed up freeing the document */
  397. if (cur->type != XML_DTD_NODE) {
  398. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  399. xmlDeregisterNodeDefaultValue(cur);
  400. if (((cur->type == XML_ELEMENT_NODE) ||
  401. (cur->type == XML_XINCLUDE_START) ||
  402. (cur->type == XML_XINCLUDE_END)) &&
  403. (cur->properties != NULL))
  404. xmlTextReaderFreePropList(reader, cur->properties);
  405. if ((cur->content != (xmlChar *) &(cur->properties)) &&
  406. (cur->type != XML_ELEMENT_NODE) &&
  407. (cur->type != XML_XINCLUDE_START) &&
  408. (cur->type != XML_XINCLUDE_END) &&
  409. (cur->type != XML_ENTITY_REF_NODE)) {
  410. DICT_FREE(cur->content);
  411. }
  412. if (((cur->type == XML_ELEMENT_NODE) ||
  413. (cur->type == XML_XINCLUDE_START) ||
  414. (cur->type == XML_XINCLUDE_END)) &&
  415. (cur->nsDef != NULL))
  416. xmlFreeNsList(cur->nsDef);
  417. /*
  418. * we don't free element names here they are interned now
  419. */
  420. if ((cur->type != XML_TEXT_NODE) &&
  421. (cur->type != XML_COMMENT_NODE))
  422. DICT_FREE(cur->name);
  423. if (((cur->type == XML_ELEMENT_NODE) ||
  424. (cur->type == XML_TEXT_NODE)) &&
  425. (reader != NULL) && (reader->ctxt != NULL) &&
  426. (reader->ctxt->freeElemsNr < MAX_FREE_NODES)) {
  427. cur->next = reader->ctxt->freeElems;
  428. reader->ctxt->freeElems = cur;
  429. reader->ctxt->freeElemsNr++;
  430. } else {
  431. xmlFree(cur);
  432. }
  433. }
  434. if (next != NULL) {
  435. cur = next;
  436. } else {
  437. if ((depth == 0) || (parent == NULL))
  438. break;
  439. depth -= 1;
  440. cur = parent;
  441. cur->children = NULL;
  442. }
  443. }
  444. }
  445. /**
  446. * xmlTextReaderFreeNode:
  447. * @reader: the xmlTextReaderPtr used
  448. * @cur: the node
  449. *
  450. * Free a node, this is a recursive behaviour, all the children are freed too.
  451. * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
  452. */
  453. static void
  454. xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
  455. xmlDictPtr dict;
  456. if ((reader != NULL) && (reader->ctxt != NULL))
  457. dict = reader->ctxt->dict;
  458. else
  459. dict = NULL;
  460. if (cur->type == XML_DTD_NODE) {
  461. xmlFreeDtd((xmlDtdPtr) cur);
  462. return;
  463. }
  464. if (cur->type == XML_NAMESPACE_DECL) {
  465. xmlFreeNs((xmlNsPtr) cur);
  466. return;
  467. }
  468. if (cur->type == XML_ATTRIBUTE_NODE) {
  469. xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
  470. return;
  471. }
  472. if ((cur->children != NULL) &&
  473. (cur->type != XML_ENTITY_REF_NODE)) {
  474. if (cur->children->parent == cur)
  475. xmlTextReaderFreeNodeList(reader, cur->children);
  476. cur->children = NULL;
  477. }
  478. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  479. xmlDeregisterNodeDefaultValue(cur);
  480. if (((cur->type == XML_ELEMENT_NODE) ||
  481. (cur->type == XML_XINCLUDE_START) ||
  482. (cur->type == XML_XINCLUDE_END)) &&
  483. (cur->properties != NULL))
  484. xmlTextReaderFreePropList(reader, cur->properties);
  485. if ((cur->content != (xmlChar *) &(cur->properties)) &&
  486. (cur->type != XML_ELEMENT_NODE) &&
  487. (cur->type != XML_XINCLUDE_START) &&
  488. (cur->type != XML_XINCLUDE_END) &&
  489. (cur->type != XML_ENTITY_REF_NODE)) {
  490. DICT_FREE(cur->content);
  491. }
  492. if (((cur->type == XML_ELEMENT_NODE) ||
  493. (cur->type == XML_XINCLUDE_START) ||
  494. (cur->type == XML_XINCLUDE_END)) &&
  495. (cur->nsDef != NULL))
  496. xmlFreeNsList(cur->nsDef);
  497. /*
  498. * we don't free names here they are interned now
  499. */
  500. if ((cur->type != XML_TEXT_NODE) &&
  501. (cur->type != XML_COMMENT_NODE))
  502. DICT_FREE(cur->name);
  503. if (((cur->type == XML_ELEMENT_NODE) ||
  504. (cur->type == XML_TEXT_NODE)) &&
  505. (reader != NULL) && (reader->ctxt != NULL) &&
  506. (reader->ctxt->freeElemsNr < MAX_FREE_NODES)) {
  507. cur->next = reader->ctxt->freeElems;
  508. reader->ctxt->freeElems = cur;
  509. reader->ctxt->freeElemsNr++;
  510. } else {
  511. xmlFree(cur);
  512. }
  513. }
  514. static void
  515. xmlTextReaderFreeIDTableEntry(void *id, const xmlChar *name ATTRIBUTE_UNUSED) {
  516. xmlFreeID((xmlIDPtr) id);
  517. }
  518. /**
  519. * xmlTextReaderFreeIDTable:
  520. * @table: An id table
  521. *
  522. * Deallocate the memory used by an ID hash table.
  523. */
  524. static void
  525. xmlTextReaderFreeIDTable(xmlIDTablePtr table) {
  526. xmlHashFree(table, xmlTextReaderFreeIDTableEntry);
  527. }
  528. /**
  529. * xmlTextReaderFreeDoc:
  530. * @reader: the xmlTextReaderPtr used
  531. * @cur: pointer to the document
  532. *
  533. * Free up all the structures used by a document, tree included.
  534. */
  535. static void
  536. xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
  537. xmlDtdPtr extSubset, intSubset;
  538. if (cur == NULL) return;
  539. if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
  540. xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
  541. /*
  542. * Do this before freeing the children list to avoid ID lookups
  543. */
  544. if (cur->ids != NULL) xmlTextReaderFreeIDTable((xmlIDTablePtr) cur->ids);
  545. cur->ids = NULL;
  546. if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
  547. cur->refs = NULL;
  548. extSubset = cur->extSubset;
  549. intSubset = cur->intSubset;
  550. if (intSubset == extSubset)
  551. extSubset = NULL;
  552. if (extSubset != NULL) {
  553. xmlUnlinkNode((xmlNodePtr) cur->extSubset);
  554. cur->extSubset = NULL;
  555. xmlFreeDtd(extSubset);
  556. }
  557. if (intSubset != NULL) {
  558. xmlUnlinkNode((xmlNodePtr) cur->intSubset);
  559. cur->intSubset = NULL;
  560. xmlFreeDtd(intSubset);
  561. }
  562. if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
  563. if (cur->version != NULL) xmlFree((char *) cur->version);
  564. if (cur->name != NULL) xmlFree((char *) cur->name);
  565. if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
  566. if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
  567. if (cur->URL != NULL) xmlFree((char *) cur->URL);
  568. if (cur->dict != NULL) xmlDictFree(cur->dict);
  569. xmlFree(cur);
  570. }
  571. /************************************************************************
  572. * *
  573. * The reader core parser *
  574. * *
  575. ************************************************************************/
  576. #ifdef DEBUG_READER
  577. static void
  578. xmlTextReaderDebug(xmlTextReaderPtr reader) {
  579. if ((reader == NULL) || (reader->ctxt == NULL)) {
  580. fprintf(stderr, "xmlTextReader NULL\n");
  581. return;
  582. }
  583. fprintf(stderr, "xmlTextReader: state %d depth %d ",
  584. reader->state, reader->depth);
  585. if (reader->node == NULL) {
  586. fprintf(stderr, "node = NULL\n");
  587. } else {
  588. fprintf(stderr, "node %s\n", reader->node->name);
  589. }
  590. fprintf(stderr, " input: base %d, cur %d, depth %d: ",
  591. reader->base, reader->cur, reader->ctxt->nodeNr);
  592. if (reader->input->buffer == NULL) {
  593. fprintf(stderr, "buffer is NULL\n");
  594. } else {
  595. #ifdef LIBXML_DEBUG_ENABLED
  596. xmlDebugDumpString(stderr,
  597. &reader->input->buffer->content[reader->cur]);
  598. #endif
  599. fprintf(stderr, "\n");
  600. }
  601. }
  602. #endif
  603. /**
  604. * xmlTextReaderEntPush:
  605. * @reader: the xmlTextReaderPtr used
  606. * @value: the entity reference node
  607. *
  608. * Pushes a new entity reference node on top of the entities stack
  609. *
  610. * Returns 0 in case of error, the index in the stack otherwise
  611. */
  612. static int
  613. xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
  614. {
  615. if (reader->entMax <= 0) {
  616. reader->entMax = 10;
  617. reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
  618. sizeof(reader->entTab[0]));
  619. if (reader->entTab == NULL) {
  620. xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
  621. return (0);
  622. }
  623. }
  624. if (reader->entNr >= reader->entMax) {
  625. reader->entMax *= 2;
  626. reader->entTab =
  627. (xmlNodePtr *) xmlRealloc(reader->entTab,
  628. reader->entMax *
  629. sizeof(reader->entTab[0]));
  630. if (reader->entTab == NULL) {
  631. xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
  632. return (0);
  633. }
  634. }
  635. reader->entTab[reader->entNr] = value;
  636. reader->ent = value;
  637. return (reader->entNr++);
  638. }
  639. /**
  640. * xmlTextReaderEntPop:
  641. * @reader: the xmlTextReaderPtr used
  642. *
  643. * Pops the top element entity from the entities stack
  644. *
  645. * Returns the entity just removed
  646. */
  647. static xmlNodePtr
  648. xmlTextReaderEntPop(xmlTextReaderPtr reader)
  649. {
  650. xmlNodePtr ret;
  651. if (reader->entNr <= 0)
  652. return (NULL);
  653. reader->entNr--;
  654. if (reader->entNr > 0)
  655. reader->ent = reader->entTab[reader->entNr - 1];
  656. else
  657. reader->ent = NULL;
  658. ret = reader->entTab[reader->entNr];
  659. reader->entTab[reader->entNr] = NULL;
  660. return (ret);
  661. }
  662. /**
  663. * xmlTextReaderStartElement:
  664. * @ctx: the user data (XML parser context)
  665. * @fullname: The element name, including namespace prefix
  666. * @atts: An array of name/value attributes pairs, NULL terminated
  667. *
  668. * called when an opening tag has been processed.
  669. */
  670. static void
  671. xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
  672. const xmlChar **atts) {
  673. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  674. xmlTextReaderPtr reader = ctxt->_private;
  675. #ifdef DEBUG_CALLBACKS
  676. printf("xmlTextReaderStartElement(%s)\n", fullname);
  677. #endif
  678. if ((reader != NULL) && (reader->startElement != NULL)) {
  679. reader->startElement(ctx, fullname, atts);
  680. if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
  681. (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
  682. (ctxt->input->cur[1] == '>'))
  683. ctxt->node->extra = NODE_IS_EMPTY;
  684. }
  685. if (reader != NULL)
  686. reader->state = XML_TEXTREADER_ELEMENT;
  687. }
  688. /**
  689. * xmlTextReaderEndElement:
  690. * @ctx: the user data (XML parser context)
  691. * @fullname: The element name, including namespace prefix
  692. *
  693. * called when an ending tag has been processed.
  694. */
  695. static void
  696. xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
  697. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  698. xmlTextReaderPtr reader = ctxt->_private;
  699. #ifdef DEBUG_CALLBACKS
  700. printf("xmlTextReaderEndElement(%s)\n", fullname);
  701. #endif
  702. if ((reader != NULL) && (reader->endElement != NULL)) {
  703. reader->endElement(ctx, fullname);
  704. }
  705. }
  706. /**
  707. * xmlTextReaderStartElementNs:
  708. * @ctx: the user data (XML parser context)
  709. * @localname: the local name of the element
  710. * @prefix: the element namespace prefix if available
  711. * @URI: the element namespace name if available
  712. * @nb_namespaces: number of namespace definitions on that node
  713. * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
  714. * @nb_attributes: the number of attributes on that node
  715. * nb_defaulted: the number of defaulted attributes.
  716. * @attributes: pointer to the array of (localname/prefix/URI/value/end)
  717. * attribute values.
  718. *
  719. * called when an opening tag has been processed.
  720. */
  721. static void
  722. xmlTextReaderStartElementNs(void *ctx,
  723. const xmlChar *localname,
  724. const xmlChar *prefix,
  725. const xmlChar *URI,
  726. int nb_namespaces,
  727. const xmlChar **namespaces,
  728. int nb_attributes,
  729. int nb_defaulted,
  730. const xmlChar **attributes)
  731. {
  732. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  733. xmlTextReaderPtr reader = ctxt->_private;
  734. #ifdef DEBUG_CALLBACKS
  735. printf("xmlTextReaderStartElementNs(%s)\n", localname);
  736. #endif
  737. if ((reader != NULL) && (reader->startElementNs != NULL)) {
  738. reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
  739. namespaces, nb_attributes, nb_defaulted,
  740. attributes);
  741. if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
  742. (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
  743. (ctxt->input->cur[1] == '>'))
  744. ctxt->node->extra = NODE_IS_EMPTY;
  745. }
  746. if (reader != NULL)
  747. reader->state = XML_TEXTREADER_ELEMENT;
  748. }
  749. /**
  750. * xmlTextReaderEndElementNs:
  751. * @ctx: the user data (XML parser context)
  752. * @localname: the local name of the element
  753. * @prefix: the element namespace prefix if available
  754. * @URI: the element namespace name if available
  755. *
  756. * called when an ending tag has been processed.
  757. */
  758. static void
  759. xmlTextReaderEndElementNs(void *ctx,
  760. const xmlChar * localname,
  761. const xmlChar * prefix,
  762. const xmlChar * URI)
  763. {
  764. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  765. xmlTextReaderPtr reader = ctxt->_private;
  766. #ifdef DEBUG_CALLBACKS
  767. printf("xmlTextReaderEndElementNs(%s)\n", localname);
  768. #endif
  769. if ((reader != NULL) && (reader->endElementNs != NULL)) {
  770. reader->endElementNs(ctx, localname, prefix, URI);
  771. }
  772. }
  773. /**
  774. * xmlTextReaderCharacters:
  775. * @ctx: the user data (XML parser context)
  776. * @ch: a xmlChar string
  777. * @len: the number of xmlChar
  778. *
  779. * receiving some chars from the parser.
  780. */
  781. static void
  782. xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
  783. {
  784. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  785. xmlTextReaderPtr reader = ctxt->_private;
  786. #ifdef DEBUG_CALLBACKS
  787. printf("xmlTextReaderCharacters()\n");
  788. #endif
  789. if ((reader != NULL) && (reader->characters != NULL)) {
  790. reader->characters(ctx, ch, len);
  791. }
  792. }
  793. /**
  794. * xmlTextReaderCDataBlock:
  795. * @ctx: the user data (XML parser context)
  796. * @value: The pcdata content
  797. * @len: the block length
  798. *
  799. * called when a pcdata block has been parsed
  800. */
  801. static void
  802. xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
  803. {
  804. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  805. xmlTextReaderPtr reader = ctxt->_private;
  806. #ifdef DEBUG_CALLBACKS
  807. printf("xmlTextReaderCDataBlock()\n");
  808. #endif
  809. if ((reader != NULL) && (reader->cdataBlock != NULL)) {
  810. reader->cdataBlock(ctx, ch, len);
  811. }
  812. }
  813. /**
  814. * xmlTextReaderPushData:
  815. * @reader: the xmlTextReaderPtr used
  816. *
  817. * Push data down the progressive parser until a significant callback
  818. * got raised.
  819. *
  820. * Returns -1 in case of failure, 0 otherwise
  821. */
  822. static int
  823. xmlTextReaderPushData(xmlTextReaderPtr reader) {
  824. xmlBufPtr inbuf;
  825. int val, s;
  826. xmlTextReaderState oldstate;
  827. int alloc;
  828. if ((reader->input == NULL) || (reader->input->buffer == NULL))
  829. return(-1);
  830. oldstate = reader->state;
  831. reader->state = XML_TEXTREADER_NONE;
  832. inbuf = reader->input->buffer;
  833. alloc = xmlBufGetAllocationScheme(inbuf);
  834. while (reader->state == XML_TEXTREADER_NONE) {
  835. if (xmlBufUse(inbuf) < reader->cur + CHUNK_SIZE) {
  836. /*
  837. * Refill the buffer unless we are at the end of the stream
  838. */
  839. if (reader->mode != XML_TEXTREADER_MODE_EOF) {
  840. val = xmlParserInputBufferRead(reader->input, 4096);
  841. if ((val == 0) &&
  842. (alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
  843. if (xmlBufUse(inbuf) == reader->cur) {
  844. reader->mode = XML_TEXTREADER_MODE_EOF;
  845. reader->state = oldstate;
  846. }
  847. } else if (val < 0) {
  848. reader->mode = XML_TEXTREADER_MODE_EOF;
  849. reader->state = oldstate;
  850. if ((oldstate != XML_TEXTREADER_START) ||
  851. (reader->ctxt->myDoc != NULL))
  852. return(val);
  853. } else if (val == 0) {
  854. /* mark the end of the stream and process the remains */
  855. reader->mode = XML_TEXTREADER_MODE_EOF;
  856. break;
  857. }
  858. } else
  859. break;
  860. }
  861. /*
  862. * parse by block of CHUNK_SIZE bytes, various tests show that
  863. * it's the best tradeoff at least on a 1.2GH Duron
  864. */
  865. if (xmlBufUse(inbuf) >= reader->cur + CHUNK_SIZE) {
  866. val = xmlParseChunk(reader->ctxt,
  867. (const char *) xmlBufContent(inbuf) + reader->cur,
  868. CHUNK_SIZE, 0);
  869. reader->cur += CHUNK_SIZE;
  870. if (val != 0)
  871. reader->ctxt->wellFormed = 0;
  872. if (reader->ctxt->wellFormed == 0)
  873. break;
  874. } else {
  875. s = xmlBufUse(inbuf) - reader->cur;
  876. val = xmlParseChunk(reader->ctxt,
  877. (const char *) xmlBufContent(inbuf) + reader->cur,
  878. s, 0);
  879. reader->cur += s;
  880. if (val != 0)
  881. reader->ctxt->wellFormed = 0;
  882. break;
  883. }
  884. }
  885. /*
  886. * Discard the consumed input when needed and possible
  887. */
  888. if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
  889. if (alloc != XML_BUFFER_ALLOC_IMMUTABLE) {
  890. if ((reader->cur >= 4096) &&
  891. (xmlBufUse(inbuf) - reader->cur <= CHUNK_SIZE)) {
  892. val = xmlBufShrink(inbuf, reader->cur);
  893. if (val >= 0) {
  894. reader->cur -= val;
  895. }
  896. }
  897. }
  898. }
  899. /*
  900. * At the end of the stream signal that the work is done to the Push
  901. * parser.
  902. */
  903. else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
  904. if (reader->state != XML_TEXTREADER_DONE) {
  905. s = xmlBufUse(inbuf) - reader->cur;
  906. val = xmlParseChunk(reader->ctxt,
  907. (const char *) xmlBufContent(inbuf) + reader->cur,
  908. s, 1);
  909. reader->cur = xmlBufUse(inbuf);
  910. reader->state = XML_TEXTREADER_DONE;
  911. if (val != 0) {
  912. if (reader->ctxt->wellFormed)
  913. reader->ctxt->wellFormed = 0;
  914. else
  915. return(-1);
  916. }
  917. }
  918. }
  919. reader->state = oldstate;
  920. if (reader->ctxt->wellFormed == 0) {
  921. reader->mode = XML_TEXTREADER_MODE_EOF;
  922. return(-1);
  923. }
  924. return(0);
  925. }
  926. #ifdef LIBXML_REGEXP_ENABLED
  927. /**
  928. * xmlTextReaderValidatePush:
  929. * @reader: the xmlTextReaderPtr used
  930. *
  931. * Push the current node for validation
  932. */
  933. static void
  934. xmlTextReaderValidatePush(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
  935. xmlNodePtr node = reader->node;
  936. #ifdef LIBXML_VALID_ENABLED
  937. if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
  938. (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
  939. if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
  940. reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
  941. reader->ctxt->myDoc, node, node->name);
  942. } else {
  943. /* TODO use the BuildQName interface */
  944. xmlChar *qname;
  945. qname = xmlStrdup(node->ns->prefix);
  946. qname = xmlStrcat(qname, BAD_CAST ":");
  947. qname = xmlStrcat(qname, node->name);
  948. reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
  949. reader->ctxt->myDoc, node, qname);
  950. if (qname != NULL)
  951. xmlFree(qname);
  952. }
  953. }
  954. #endif /* LIBXML_VALID_ENABLED */
  955. #ifdef LIBXML_SCHEMAS_ENABLED
  956. if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
  957. (reader->rngValidCtxt != NULL)) {
  958. int ret;
  959. if (reader->rngFullNode != NULL) return;
  960. ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
  961. reader->ctxt->myDoc,
  962. node);
  963. if (ret == 0) {
  964. /*
  965. * this element requires a full tree
  966. */
  967. node = xmlTextReaderExpand(reader);
  968. if (node == NULL) {
  969. ret = -1;
  970. } else {
  971. ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
  972. reader->ctxt->myDoc,
  973. node);
  974. reader->rngFullNode = node;
  975. }
  976. }
  977. if (ret != 1)
  978. reader->rngValidErrors++;
  979. }
  980. #endif
  981. }
  982. /**
  983. * xmlTextReaderValidateCData:
  984. * @reader: the xmlTextReaderPtr used
  985. * @data: pointer to the CData
  986. * @len: length of the CData block in bytes.
  987. *
  988. * Push some CData for validation
  989. */
  990. static void
  991. xmlTextReaderValidateCData(xmlTextReaderPtr reader,
  992. const xmlChar *data, int len) {
  993. #ifdef LIBXML_VALID_ENABLED
  994. if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
  995. (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
  996. reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
  997. data, len);
  998. }
  999. #endif /* LIBXML_VALID_ENABLED */
  1000. #ifdef LIBXML_SCHEMAS_ENABLED
  1001. if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
  1002. (reader->rngValidCtxt != NULL)) {
  1003. int ret;
  1004. if (reader->rngFullNode != NULL) return;
  1005. ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
  1006. if (ret != 1)
  1007. reader->rngValidErrors++;
  1008. }
  1009. #endif
  1010. }
  1011. /**
  1012. * xmlTextReaderValidatePop:
  1013. * @reader: the xmlTextReaderPtr used
  1014. *
  1015. * Pop the current node from validation
  1016. */
  1017. static void
  1018. xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
  1019. xmlNodePtr node = reader->node;
  1020. #ifdef LIBXML_VALID_ENABLED
  1021. if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
  1022. (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
  1023. if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
  1024. reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
  1025. reader->ctxt->myDoc, node, node->name);
  1026. } else {
  1027. /* TODO use the BuildQName interface */
  1028. xmlChar *qname;
  1029. qname = xmlStrdup(node->ns->prefix);
  1030. qname = xmlStrcat(qname, BAD_CAST ":");
  1031. qname = xmlStrcat(qname, node->name);
  1032. reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
  1033. reader->ctxt->myDoc, node, qname);
  1034. if (qname != NULL)
  1035. xmlFree(qname);
  1036. }
  1037. }
  1038. #endif /* LIBXML_VALID_ENABLED */
  1039. #ifdef LIBXML_SCHEMAS_ENABLED
  1040. if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
  1041. (reader->rngValidCtxt != NULL)) {
  1042. int ret;
  1043. if (reader->rngFullNode != NULL) {
  1044. if (node == reader->rngFullNode)
  1045. reader->rngFullNode = NULL;
  1046. return;
  1047. }
  1048. ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
  1049. reader->ctxt->myDoc,
  1050. node);
  1051. if (ret != 1)
  1052. reader->rngValidErrors++;
  1053. }
  1054. #endif
  1055. }
  1056. /**
  1057. * xmlTextReaderValidateEntity:
  1058. * @reader: the xmlTextReaderPtr used
  1059. *
  1060. * Handle the validation when an entity reference is encountered and
  1061. * entity substitution is not activated. As a result the parser interface
  1062. * must walk through the entity and do the validation calls
  1063. */
  1064. static void
  1065. xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
  1066. xmlNodePtr oldnode = reader->node;
  1067. xmlNodePtr node = reader->node;
  1068. do {
  1069. if (node->type == XML_ENTITY_REF_NODE) {
  1070. if ((node->children != NULL) &&
  1071. (node->children->type == XML_ENTITY_DECL) &&
  1072. (node->children->children != NULL)) {
  1073. xmlTextReaderEntPush(reader, node);
  1074. node = node->children->children;
  1075. continue;
  1076. } else {
  1077. /*
  1078. * The error has probably been raised already.
  1079. */
  1080. if (node == oldnode)
  1081. break;
  1082. goto skip_children;
  1083. }
  1084. #ifdef LIBXML_REGEXP_ENABLED
  1085. } else if (node->type == XML_ELEMENT_NODE) {
  1086. reader->node = node;
  1087. xmlTextReaderValidatePush(reader);
  1088. } else if ((node->type == XML_TEXT_NODE) ||
  1089. (node->type == XML_CDATA_SECTION_NODE)) {
  1090. xmlTextReaderValidateCData(reader, node->content,
  1091. xmlStrlen(node->content));
  1092. #endif
  1093. }
  1094. /*
  1095. * go to next node
  1096. */
  1097. if (node->children != NULL) {
  1098. node = node->children;
  1099. continue;
  1100. } else if (node->type == XML_ELEMENT_NODE) {
  1101. xmlTextReaderValidatePop(reader);
  1102. }
  1103. skip_children:
  1104. if (node->next != NULL) {
  1105. node = node->next;
  1106. continue;
  1107. }
  1108. do {
  1109. node = node->parent;
  1110. if (node->type == XML_ELEMENT_NODE) {
  1111. xmlNodePtr tmp;
  1112. if (reader->entNr == 0) {
  1113. while ((tmp = node->last) != NULL) {
  1114. if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
  1115. xmlUnlinkNode(tmp);
  1116. xmlTextReaderFreeNode(reader, tmp);
  1117. } else
  1118. break;
  1119. }
  1120. }
  1121. reader->node = node;
  1122. xmlTextReaderValidatePop(reader);
  1123. }
  1124. if ((node->type == XML_ENTITY_DECL) &&
  1125. (reader->ent != NULL) && (reader->ent->children == node)) {
  1126. node = xmlTextReaderEntPop(reader);
  1127. }
  1128. if (node == oldnode)
  1129. break;
  1130. if (node->next != NULL) {
  1131. node = node->next;
  1132. break;
  1133. }
  1134. } while ((node != NULL) && (node != oldnode));
  1135. } while ((node != NULL) && (node != oldnode));
  1136. reader->node = oldnode;
  1137. }
  1138. #endif /* LIBXML_REGEXP_ENABLED */
  1139. /**
  1140. * xmlTextReaderGetSuccessor:
  1141. * @cur: the current node
  1142. *
  1143. * Get the successor of a node if available.
  1144. *
  1145. * Returns the successor node or NULL
  1146. */
  1147. static xmlNodePtr
  1148. xmlTextReaderGetSuccessor(xmlNodePtr cur) {
  1149. if (cur == NULL) return(NULL) ; /* ERROR */
  1150. if (cur->next != NULL) return(cur->next) ;
  1151. do {
  1152. cur = cur->parent;
  1153. if (cur == NULL) break;
  1154. if (cur->next != NULL) return(cur->next);
  1155. } while (cur != NULL);
  1156. return(cur);
  1157. }
  1158. /**
  1159. * xmlTextReaderDoExpand:
  1160. * @reader: the xmlTextReaderPtr used
  1161. *
  1162. * Makes sure that the current node is fully read as well as all its
  1163. * descendant. It means the full DOM subtree must be available at the
  1164. * end of the call.
  1165. *
  1166. * Returns 1 if the node was expanded successfully, 0 if there is no more
  1167. * nodes to read, or -1 in case of error
  1168. */
  1169. static int
  1170. xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
  1171. int val;
  1172. if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
  1173. return(-1);
  1174. do {
  1175. if (reader->ctxt->instate == XML_PARSER_EOF) return(1);
  1176. if (xmlTextReaderGetSuccessor(reader->node) != NULL)
  1177. return(1);
  1178. if (reader->ctxt->nodeNr < reader->depth)
  1179. return(1);
  1180. if (reader->mode == XML_TEXTREADER_MODE_EOF)
  1181. return(1);
  1182. val = xmlTextReaderPushData(reader);
  1183. if (val < 0){
  1184. reader->mode = XML_TEXTREADER_MODE_ERROR;
  1185. return(-1);
  1186. }
  1187. } while(reader->mode != XML_TEXTREADER_MODE_EOF);
  1188. return(1);
  1189. }
  1190. /**
  1191. * xmlTextReaderCollectSiblings:
  1192. * @node: the first child
  1193. *
  1194. * Traverse depth-first through all sibling nodes and their children
  1195. * nodes and concatenate their content. This is an auxiliary function
  1196. * to xmlTextReaderReadString.
  1197. *
  1198. * Returns a string containing the content, or NULL in case of error.
  1199. */
  1200. static xmlChar *
  1201. xmlTextReaderCollectSiblings(xmlNodePtr node)
  1202. {
  1203. xmlBufferPtr buffer;
  1204. xmlChar *ret;
  1205. if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
  1206. return(NULL);
  1207. buffer = xmlBufferCreate();
  1208. if (buffer == NULL)
  1209. return NULL;
  1210. for ( ; node != NULL; node = node->next) {
  1211. switch (node->type) {
  1212. case XML_TEXT_NODE:
  1213. case XML_CDATA_SECTION_NODE:
  1214. xmlBufferCat(buffer, node->content);
  1215. break;
  1216. case XML_ELEMENT_NODE: {
  1217. xmlChar *tmp;
  1218. tmp = xmlTextReaderCollectSiblings(node->children);
  1219. xmlBufferCat(buffer, tmp);
  1220. xmlFree(tmp);
  1221. break;
  1222. }
  1223. default:
  1224. break;
  1225. }
  1226. }
  1227. ret = buffer->content;
  1228. buffer->content = NULL;
  1229. xmlBufferFree(buffer);
  1230. return(ret);
  1231. }
  1232. /**
  1233. * xmlTextReaderRead:
  1234. * @reader: the xmlTextReaderPtr used
  1235. *
  1236. * Moves the position of the current instance to the next node in
  1237. * the stream, exposing its properties.
  1238. *
  1239. * Returns 1 if the node was read successfully, 0 if there is no more
  1240. * nodes to read, or -1 in case of error
  1241. */
  1242. int
  1243. xmlTextReaderRead(xmlTextReaderPtr reader) {
  1244. int val, olddepth = 0;
  1245. xmlTextReaderState oldstate = XML_TEXTREADER_START;
  1246. xmlNodePtr oldnode = NULL;
  1247. if (reader == NULL)
  1248. return(-1);
  1249. reader->curnode = NULL;
  1250. if (reader->doc != NULL)
  1251. return(xmlTextReaderReadTree(reader));
  1252. if (reader->ctxt == NULL)
  1253. return(-1);
  1254. #ifdef DEBUG_READER
  1255. fprintf(stderr, "\nREAD ");
  1256. DUMP_READER
  1257. #endif
  1258. if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
  1259. reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
  1260. /*
  1261. * Initial state
  1262. */
  1263. do {
  1264. val = xmlTextReaderPushData(reader);
  1265. if (val < 0){
  1266. reader->mode = XML_TEXTREADER_MODE_ERROR;
  1267. reader->state = XML_TEXTREADER_ERROR;
  1268. return(-1);
  1269. }
  1270. } while ((reader->ctxt->node == NULL) &&
  1271. ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
  1272. (reader->state != XML_TEXTREADER_DONE)));
  1273. if (reader->ctxt->node == NULL) {
  1274. if (reader->ctxt->myDoc != NULL) {
  1275. reader->node = reader->ctxt->myDoc->children;
  1276. }
  1277. if (reader->node == NULL){
  1278. reader->mode = XML_TEXTREADER_MODE_ERROR;
  1279. reader->state = XML_TEXTREADER_ERROR;
  1280. return(-1);
  1281. }
  1282. reader->state = XML_TEXTREADER_ELEMENT;
  1283. } else {
  1284. if (reader->ctxt->myDoc != NULL) {
  1285. reader->node = reader->ctxt->myDoc->children;
  1286. }
  1287. if (reader->node == NULL)
  1288. reader->node = reader->ctxt->nodeTab[0];
  1289. reader->state = XML_TEXTREADER_ELEMENT;
  1290. }
  1291. reader->depth = 0;
  1292. reader->ctxt->parseMode = XML_PARSE_READER;
  1293. goto node_found;
  1294. }
  1295. oldstate = reader->state;
  1296. olddepth = reader->ctxt->nodeNr;
  1297. oldnode = reader->node;
  1298. get_next_node:
  1299. if (reader->node == NULL) {
  1300. if (reader->mode == XML_TEXTREADER_MODE_EOF)
  1301. return(0);
  1302. else
  1303. return(-1);
  1304. }
  1305. /*
  1306. * If we are not backtracking on ancestors or examined nodes,
  1307. * that the parser didn't finished or that we aren't at the end
  1308. * of stream, continue processing.
  1309. */
  1310. while ((reader->node != NULL) && (reader->node->next == NULL) &&
  1311. (reader->ctxt->nodeNr == olddepth) &&
  1312. ((oldstate == XML_TEXTREADER_BACKTRACK) ||
  1313. (reader->node->children == NULL) ||
  1314. (reader->node->type == XML_ENTITY_REF_NODE) ||
  1315. ((reader->node->children != NULL) &&
  1316. (reader->node->children->type == XML_TEXT_NODE) &&
  1317. (reader->node->children->next == NULL)) ||
  1318. (reader->node->type == XML_DTD_NODE) ||
  1319. (reader->node->type == XML_DOCUMENT_NODE) ||
  1320. (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
  1321. ((reader->ctxt->node == NULL) ||
  1322. (reader->ctxt->node == reader->node) ||
  1323. (reader->ctxt->node == reader->node->parent)) &&
  1324. (reader->ctxt->instate != XML_PARSER_EOF)) {
  1325. val = xmlTextReaderPushData(reader);
  1326. if (val < 0){
  1327. reader->mode = XML_TEXTREADER_MODE_ERROR;
  1328. reader->state = XML_TEXTREADER_ERROR;
  1329. return(-1);
  1330. }
  1331. if (reader->node == NULL)
  1332. goto node_end;
  1333. }
  1334. if (oldstate != XML_TEXTREADER_BACKTRACK) {
  1335. if ((reader->node->children != NULL) &&
  1336. (reader->node->type != XML_ENTITY_REF_NODE) &&
  1337. (reader->node->type != XML_XINCLUDE_START) &&
  1338. (reader->node->type != XML_DTD_NODE)) {
  1339. reader->node = reader->node->children;
  1340. reader->depth++;
  1341. reader->state = XML_TEXTREADER_ELEMENT;
  1342. goto node_found;
  1343. }
  1344. }
  1345. if (reader->node->next != NULL) {
  1346. if ((oldstate == XML_TEXTREADER_ELEMENT) &&
  1347. (reader->node->type == XML_ELEMENT_NODE) &&
  1348. (reader->node->children == NULL) &&
  1349. ((reader->node->extra & NODE_IS_EMPTY) == 0)
  1350. #ifdef LIBXML_XINCLUDE_ENABLED
  1351. && (reader->in_xinclude <= 0)
  1352. #endif
  1353. ) {
  1354. reader->state = XML_TEXTREADER_END;
  1355. goto node_found;
  1356. }
  1357. #ifdef LIBXML_REGEXP_ENABLED
  1358. if ((reader->validate) &&
  1359. (reader->node->type == XML_ELEMENT_NODE))
  1360. xmlTextReaderValidatePop(reader);
  1361. #endif /* LIBXML_REGEXP_ENABLED */
  1362. if ((reader->preserves > 0) &&
  1363. (reader->node->extra & NODE_IS_SPRESERVED))
  1364. reader->preserves--;
  1365. reader->node = reader->node->next;
  1366. reader->state = XML_TEXTREADER_ELEMENT;
  1367. /*
  1368. * Cleanup of the old node
  1369. */
  1370. if ((reader->preserves == 0) &&
  1371. #ifdef LIBXML_XINCLUDE_ENABLED
  1372. (reader->in_xinclude == 0) &&
  1373. #endif
  1374. (reader->entNr == 0) &&
  1375. (reader->node->prev != NULL) &&
  1376. (reader->node->prev->type != XML_DTD_NODE)) {
  1377. xmlNodePtr tmp = reader->node->prev;
  1378. if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
  1379. if (oldnode == tmp)
  1380. oldnode = NULL;
  1381. xmlUnlinkNode(tmp);
  1382. xmlTextReaderFreeNode(reader, tmp);
  1383. }
  1384. }
  1385. goto node_found;
  1386. }
  1387. if ((oldstate == XML_TEXTREADER_ELEMENT) &&
  1388. (reader->node->type == XML_ELEMENT_NODE) &&
  1389. (reader->node->children == NULL) &&
  1390. ((reader->node->extra & NODE_IS_EMPTY) == 0)) {;
  1391. reader->state = XML_TEXTREADER_END;
  1392. goto node_found;
  1393. }
  1394. #ifdef LIBXML_REGEXP_ENABLED
  1395. if ((reader->validate != XML_TEXTREADER_NOT_VALIDATE) && (reader->node->type == XML_ELEMENT_NODE))
  1396. xmlTextReaderValidatePop(reader);
  1397. #endif /* LIBXML_REGEXP_ENABLED */
  1398. if ((reader->preserves > 0) &&
  1399. (reader->node->extra & NODE_IS_SPRESERVED))
  1400. reader->preserves--;
  1401. reader->node = reader->node->parent;
  1402. if ((reader->node == NULL) ||
  1403. (reader->node->type == XML_DOCUMENT_NODE) ||
  1404. #ifdef LIBXML_DOCB_ENABLED
  1405. (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
  1406. #endif
  1407. (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
  1408. if (reader->mode != XML_TEXTREADER_MODE_EOF) {
  1409. val = xmlParseChunk(reader->ctxt, "", 0, 1);
  1410. reader->state = XML_TEXTREADER_DONE;
  1411. if (val != 0)
  1412. return(-1);
  1413. }
  1414. reader->node = NULL;
  1415. reader->depth = -1;
  1416. /*
  1417. * Cleanup of the old node
  1418. */
  1419. if ((oldnode != NULL) && (reader->preserves == 0) &&
  1420. #ifdef LIBXML_XINCLUDE_ENABLED
  1421. (reader->in_xinclude == 0) &&
  1422. #endif
  1423. (reader->entNr == 0) &&
  1424. (oldnode->type != XML_DTD_NODE) &&
  1425. ((oldnode->extra & NODE_IS_PRESERVED) == 0)) {
  1426. xmlUnlinkNode(oldnode);
  1427. xmlTextReaderFreeNode(reader, oldnode);
  1428. }
  1429. goto node_end;
  1430. }
  1431. if ((reader->preserves == 0) &&
  1432. #ifdef LIBXML_XINCLUDE_ENABLED
  1433. (reader->in_xinclude == 0) &&
  1434. #endif
  1435. (reader->entNr == 0) &&
  1436. (reader->node->last != NULL) &&
  1437. ((reader->node->last->extra & NODE_IS_PRESERVED) == 0)) {
  1438. xmlNodePtr tmp = reader->node->last;
  1439. xmlUnlinkNode(tmp);
  1440. xmlTextReaderFreeNode(reader, tmp);
  1441. }
  1442. reader->depth--;
  1443. reader->state = XML_TEXTREADER_BACKTRACK;
  1444. node_found:
  1445. DUMP_READER
  1446. /*
  1447. * If we are in the middle of a piece of CDATA make sure it's finished
  1448. */
  1449. if ((reader->node != NULL) &&
  1450. (reader->node->next == NULL) &&
  1451. ((reader->node->type == XML_TEXT_NODE) ||
  1452. (reader->node->type == XML_CDATA_SECTION_NODE))) {
  1453. if (xmlTextReaderExpand(reader) == NULL)
  1454. return -1;
  1455. }
  1456. #ifdef LIBXML_XINCLUDE_ENABLED
  1457. /*
  1458. * Handle XInclude if asked for
  1459. */
  1460. if ((reader->xinclude) && (reader->in_xinclude == 0) &&
  1461. (reader->node != NULL) &&
  1462. (reader->node->type == XML_ELEMENT_NODE) &&
  1463. (reader->node->ns != NULL) &&
  1464. ((xmlStrEqual(reader->node->ns->href, XINCLUDE_NS)) ||
  1465. (xmlStrEqual(reader->node->ns->href, XINCLUDE_OLD_NS)))) {
  1466. if (reader->xincctxt == NULL) {
  1467. reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc);
  1468. xmlXIncludeSetFlags(reader->xincctxt,
  1469. reader->parserFlags & (~XML_PARSE_NOXINCNODE));
  1470. }
  1471. /*
  1472. * expand that node and process it
  1473. */
  1474. if (xmlTextReaderExpand(reader) == NULL)
  1475. return -1;
  1476. xmlXIncludeProcessNode(reader->xincctxt, reader->node);
  1477. }
  1478. if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_START)) {
  1479. reader->in_xinclude++;
  1480. goto get_next_node;
  1481. }
  1482. if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_END)) {
  1483. reader->in_xinclude--;
  1484. goto get_next_node;
  1485. }
  1486. #endif
  1487. /*
  1488. * Handle entities enter and exit when in entity replacement mode
  1489. */
  1490. if ((reader->node != NULL) &&
  1491. (reader->node->type == XML_ENTITY_REF_NODE) &&
  1492. (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
  1493. if ((reader->node->children != NULL) &&
  1494. (reader->node->children->type == XML_ENTITY_DECL) &&
  1495. (reader->node->children->children != NULL)) {
  1496. xmlTextReaderEntPush(reader, reader->node);
  1497. reader->node = reader->node->children->children;
  1498. }
  1499. #ifdef LIBXML_REGEXP_ENABLED
  1500. } else if ((reader->node != NULL) &&
  1501. (reader->node->type == XML_ENTITY_REF_NODE) &&
  1502. (reader->ctxt != NULL) && (reader->validate)) {
  1503. xmlTextReaderValidateEntity(reader);
  1504. #endif /* LIBXML_REGEXP_ENABLED */
  1505. }
  1506. if ((reader->node != NULL) &&
  1507. (reader->node->type == XML_ENTITY_DECL) &&
  1508. (reader->ent != NULL) && (reader->ent->children == reader->node)) {
  1509. reader->node = xmlTextReaderEntPop(reader);
  1510. reader->depth++;
  1511. goto get_next_node;
  1512. }
  1513. #ifdef LIBXML_REGEXP_ENABLED
  1514. if ((reader->validate != XML_TEXTREADER_NOT_VALIDATE) && (reader->node != NULL)) {
  1515. xmlNodePtr node = reader->node;
  1516. if ((node->type == XML_ELEMENT_NODE) &&
  1517. ((reader->state != XML_TEXTREADER_END) &&
  1518. (reader->state != XML_TEXTREADER_BACKTRACK))) {
  1519. xmlTextReaderValidatePush(reader);
  1520. } else if ((node->type == XML_TEXT_NODE) ||
  1521. (node->type == XML_CDATA_SECTION_NODE)) {
  1522. xmlTextReaderValidateCData(reader, node->content,
  1523. xmlStrlen(node->content));
  1524. }
  1525. }
  1526. #endif /* LIBXML_REGEXP_ENABLED */
  1527. #ifdef LIBXML_PATTERN_ENABLED
  1528. if ((reader->patternNr > 0) && (reader->state != XML_TEXTREADER_END) &&
  1529. (reader->state != XML_TEXTREADER_BACKTRACK)) {
  1530. int i;
  1531. for (i = 0;i < reader->patternNr;i++) {
  1532. if (xmlPatternMatch(reader->patternTab[i], reader->node) == 1) {
  1533. xmlTextReaderPreserve(reader);
  1534. break;
  1535. }
  1536. }
  1537. }
  1538. #endif /* LIBXML_PATTERN_ENABLED */
  1539. #ifdef LIBXML_SCHEMAS_ENABLED
  1540. if ((reader->validate == XML_TEXTREADER_VALIDATE_XSD) &&
  1541. (reader->xsdValidErrors == 0) &&
  1542. (reader->xsdValidCtxt != NULL)) {
  1543. reader->xsdValidErrors = !xmlSchemaIsValid(reader->xsdValidCtxt);
  1544. }
  1545. #endif /* LIBXML_PATTERN_ENABLED */
  1546. return(1);
  1547. node_end:
  1548. reader->state = XML_TEXTREADER_DONE;
  1549. return(0);
  1550. }
  1551. /**
  1552. * xmlTextReaderReadState:
  1553. * @reader: the xmlTextReaderPtr used
  1554. *
  1555. * Gets the read state of the reader.
  1556. *
  1557. * Returns the state value, or -1 in case of error
  1558. */
  1559. int
  1560. xmlTextReaderReadState(xmlTextReaderPtr reader) {
  1561. if (reader == NULL)
  1562. return(-1);
  1563. return(reader->mode);
  1564. }
  1565. /**
  1566. * xmlTextReaderExpand:
  1567. * @reader: the xmlTextReaderPtr used
  1568. *
  1569. * Reads the contents of the current node and the full subtree. It then makes
  1570. * the subtree available until the next xmlTextReaderRead() call
  1571. *
  1572. * Returns a node pointer valid until the next xmlTextReaderRead() call
  1573. * or NULL in case of error.
  1574. */
  1575. xmlNodePtr
  1576. xmlTextReaderExpand(xmlTextReaderPtr reader) {
  1577. if ((reader == NULL) || (reader->node == NULL))
  1578. return(NULL);
  1579. if (reader->doc != NULL)
  1580. return(reader->node);
  1581. if (reader->ctxt == NULL)
  1582. return(NULL);
  1583. if (xmlTextReaderDoExpand(reader) < 0)
  1584. return(NULL);
  1585. return(reader->node);
  1586. }
  1587. /**
  1588. * xmlTextReaderNext:
  1589. * @reader: the xmlTextReaderPtr used
  1590. *
  1591. * Skip to the node following the current one in document order while
  1592. * avoiding the subtree if any.
  1593. *
  1594. * Returns 1 if the node was read successfully, 0 if there is no more
  1595. * nodes to read, or -1 in case of error
  1596. */
  1597. int
  1598. xmlTextReaderNext(xmlTextReaderPtr reader) {
  1599. int ret;
  1600. xmlNodePtr cur;
  1601. if (reader == NULL)
  1602. return(-1);
  1603. if (reader->doc != NULL)
  1604. return(xmlTextReaderNextTree(reader));
  1605. cur = reader->node;
  1606. if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
  1607. return(xmlTextReaderRead(reader));
  1608. if (reader->state == XML_TEXTREADER_END || reader->state == XML_TEXTREADER_BACKTRACK)
  1609. return(xmlTextReaderRead(reader));
  1610. if (cur->extra & NODE_IS_EMPTY)
  1611. return(xmlTextReaderRead(reader));
  1612. do {
  1613. ret = xmlTextReaderRead(reader);
  1614. if (ret != 1)
  1615. return(ret);
  1616. } while (reader->node != cur);
  1617. return(xmlTextReaderRead(reader));
  1618. }
  1619. #ifdef LIBXML_WRITER_ENABLED
  1620. /**
  1621. * xmlTextReaderReadInnerXml:
  1622. * @reader: the xmlTextReaderPtr used
  1623. *
  1624. * Reads the contents of the current node, including child nodes and markup.
  1625. *
  1626. * Returns a string containing the XML content, or NULL if the current node
  1627. * is neither an element nor attribute, or has no child nodes. The
  1628. * string must be deallocated by the caller.
  1629. */
  1630. xmlChar *
  1631. xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED)
  1632. {
  1633. xmlChar *resbuf;
  1634. xmlNodePtr node, cur_node;
  1635. xmlBufferPtr buff, buff2;
  1636. xmlDocPtr doc;
  1637. if (xmlTextReaderExpand(reader) == NULL) {
  1638. return NULL;
  1639. }
  1640. doc = reader->node->doc;
  1641. buff = xmlBufferCreate();
  1642. if (buff == NULL)
  1643. return NULL;
  1644. for (cur_node = reader->node->children; cur_node != NULL;
  1645. cur_node = cur_node->next) {
  1646. /* XXX: Why is the node copied? */
  1647. node = xmlDocCopyNode(cur_node, doc, 1);
  1648. buff2 = xmlBufferCreate();
  1649. if (xmlNodeDump(buff2, doc, node, 0, 0) == -1) {
  1650. xmlFreeNode(node);
  1651. xmlBufferFree(buff2);
  1652. xmlBufferFree(buff);
  1653. return NULL;
  1654. }
  1655. xmlBufferCat(buff, buff2->content);
  1656. xmlFreeNode(node);
  1657. xmlBufferFree(buff2);
  1658. }
  1659. resbuf = buff->content;
  1660. buff->content = NULL;
  1661. xmlBufferFree(buff);
  1662. return resbuf;
  1663. }
  1664. #endif
  1665. #ifdef LIBXML_WRITER_ENABLED
  1666. /**
  1667. * xmlTextReaderReadOuterXml:
  1668. * @reader: the xmlTextReaderPtr used
  1669. *
  1670. * Reads the contents of the current node, including child nodes and markup.
  1671. *
  1672. * Returns a string containing the node and any XML content, or NULL if the
  1673. * current node cannot be serialized. The string must be deallocated
  1674. * by the caller.
  1675. */
  1676. xmlChar *
  1677. xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED)
  1678. {
  1679. xmlChar *resbuf;
  1680. xmlNodePtr node;
  1681. xmlBufferPtr buff;
  1682. xmlDocPtr doc;
  1683. if (xmlTextReaderExpand(reader) == NULL) {
  1684. return NULL;
  1685. }
  1686. node = reader->node;
  1687. doc = node->doc;
  1688. /* XXX: Why is the node copied? */
  1689. if (node->type == XML_DTD_NODE) {
  1690. node = (xmlNodePtr) xmlCopyDtd((xmlDtdPtr) node);
  1691. } else {
  1692. node = xmlDocCopyNode(node, doc, 1);
  1693. }
  1694. buff = xmlBufferCreate();
  1695. if (xmlNodeDump(buff, doc, node, 0, 0) == -1) {
  1696. xmlFreeNode(node);
  1697. xmlBufferFree(buff);
  1698. return NULL;
  1699. }
  1700. resbuf = buff->content;
  1701. buff->content = NULL;
  1702. xmlFreeNode(node);
  1703. xmlBufferFree(buff);
  1704. return resbuf;
  1705. }
  1706. #endif
  1707. /**
  1708. * xmlTextReaderReadString:
  1709. * @reader: the xmlTextReaderPtr used
  1710. *
  1711. * Reads the contents of an element or a text node as a string.
  1712. *
  1713. * Returns a string containing the contents of the Element or Text node,
  1714. * or NULL if the reader is positioned on any other type of node.
  1715. * The string must be deallocated by the caller.
  1716. */
  1717. xmlChar *
  1718. xmlTextReaderReadString(xmlTextReaderPtr reader)
  1719. {
  1720. xmlNodePtr node;
  1721. if ((reader == NULL) || (reader->node == NULL))
  1722. return(NULL);
  1723. node = (reader->curnode != NULL) ? reader->curnode : reader->node;
  1724. switch (node->type) {
  1725. case XML_TEXT_NODE:
  1726. if (node->content != NULL)
  1727. return(xmlStrdup(node->content));
  1728. break;
  1729. case XML_ELEMENT_NODE:
  1730. if (xmlTextReaderDoExpand(reader) != -1) {
  1731. return xmlTextReaderCollectSiblings(node->children);
  1732. }
  1733. break;
  1734. case XML_ATTRIBUTE_NODE:
  1735. TODO
  1736. break;
  1737. default:
  1738. break;
  1739. }
  1740. return(NULL);
  1741. }
  1742. #if 0
  1743. /**
  1744. * xmlTextReaderReadBase64:
  1745. * @reader: the xmlTextReaderPtr used
  1746. * @array: a byte array to store the content.
  1747. * @offset: the zero-based index into array where the method should
  1748. * begin to write.
  1749. * @len: the number of bytes to write.
  1750. *
  1751. * Reads and decodes the Base64 encoded contents of an element and
  1752. * stores the result in a byte buffer.
  1753. *
  1754. * Returns the number of bytes written to array, or zero if the current
  1755. * instance is not positioned on an element or -1 in case of error.
  1756. */
  1757. int
  1758. xmlTextReaderReadBase64(xmlTextReaderPtr reader,
  1759. unsigned char *array ATTRIBUTE_UNUSED,
  1760. int offset ATTRIBUTE_UNUSED,
  1761. int len ATTRIBUTE_UNUSED) {
  1762. if ((reader == NULL) || (reader->ctxt == NULL))
  1763. return(-1);
  1764. if (reader->ctxt->wellFormed != 1)
  1765. return(-1);
  1766. if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
  1767. return(0);
  1768. TODO
  1769. return(0);
  1770. }
  1771. /**
  1772. * xmlTextReaderReadBinHex:
  1773. * @reader: the xmlTextReaderPtr used
  1774. * @array: a byte array to store the content.
  1775. * @offset: the zero-based index into array where the method should
  1776. * begin to write.
  1777. * @len: the number of bytes to write.
  1778. *
  1779. * Reads and decodes the BinHex encoded contents of an element and
  1780. * stores the result in a byte buffer.
  1781. *
  1782. * Returns the number of bytes written to array, or zero if the current
  1783. * instance is not positioned on an element or -1 in case of error.
  1784. */
  1785. int
  1786. xmlTextReaderReadBinHex(xmlTextReaderPtr reader,
  1787. unsigned char *array ATTRIBUTE_UNUSED,
  1788. int offset ATTRIBUTE_UNUSED,
  1789. int len ATTRIBUTE_UNUSED) {
  1790. if ((reader == NULL) || (reader->ctxt == NULL))
  1791. return(-1);
  1792. if (reader->ctxt->wellFormed != 1)
  1793. return(-1);
  1794. if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
  1795. return(0);
  1796. TODO
  1797. return(0);
  1798. }
  1799. #endif
  1800. /************************************************************************
  1801. * *
  1802. * Operating on a preparsed tree *
  1803. * *
  1804. ************************************************************************/
  1805. static int
  1806. xmlTextReaderNextTree(xmlTextReaderPtr reader)
  1807. {
  1808. if (reader == NULL)
  1809. return(-1);
  1810. if (reader->state == XML_TEXTREADER_END)
  1811. return(0);
  1812. if (reader->node == NULL) {
  1813. if (reader->doc->children == NULL) {
  1814. reader->state = XML_TEXTREADER_END;
  1815. return(0);
  1816. }
  1817. reader->node = reader->doc->children;
  1818. reader->state = XML_TEXTREADER_START;
  1819. return(1);
  1820. }
  1821. if (reader->state != XML_TEXTREADER_BACKTRACK) {
  1822. /* Here removed traversal to child, because we want to skip the subtree,
  1823. replace with traversal to sibling to skip subtree */
  1824. if (reader->node->next != 0) {
  1825. /* Move to sibling if present,skipping sub-tree */
  1826. reader->node = reader->node->next;
  1827. reader->state = XML_TEXTREADER_START;
  1828. return(1);
  1829. }
  1830. /* if reader->node->next is NULL mean no subtree for current node,
  1831. so need to move to sibling of parent node if present */
  1832. reader->state = XML_TEXTREADER_BACKTRACK;
  1833. /* This will move to parent if present */
  1834. xmlTextReaderRead(reader);
  1835. }
  1836. if (reader->node->next != 0) {
  1837. reader->node = reader->node->next;
  1838. reader->state = XML_TEXTREADER_START;
  1839. return(1);
  1840. }
  1841. if (reader->node->parent != 0) {
  1842. if (reader->node->parent->type == XML_DOCUMENT_NODE) {
  1843. reader->state = XML_TEXTREADER_END;
  1844. return(0);
  1845. }
  1846. reader->node = reader->node->parent;
  1847. reader->depth--;
  1848. reader->state = XML_TEXTREADER_BACKTRACK;
  1849. /* Repeat process to move to sibling of parent node if present */
  1850. xmlTextReaderNextTree(reader);
  1851. }
  1852. reader->state = XML_TEXTREADER_END;
  1853. return(1);
  1854. }
  1855. /**
  1856. * xmlTextReaderReadTree:
  1857. * @reader: the xmlTextReaderPtr used
  1858. *
  1859. * Moves the position of the current instance to the next node in
  1860. * the stream, exposing its properties.
  1861. *
  1862. * Returns 1 if the node was read successfully, 0 if there is no more
  1863. * nodes to read, or -1 in case of error
  1864. */
  1865. static int
  1866. xmlTextReaderReadTree(xmlTextReaderPtr reader) {
  1867. if (reader->state == XML_TEXTREADER_END)
  1868. return(0);
  1869. next_node:
  1870. if (reader->node == NULL) {
  1871. if (reader->doc->children == NULL) {
  1872. reader->state = XML_TEXTREADER_END;
  1873. return(0);
  1874. }
  1875. reader->node = reader->doc->children;
  1876. reader->state = XML_TEXTREADER_START;
  1877. goto found_node;
  1878. }
  1879. if ((reader->state != XML_TEXTREADER_BACKTRACK) &&
  1880. (reader->node->type != XML_DTD_NODE) &&
  1881. (reader->node->type != XML_XINCLUDE_START) &&
  1882. (reader->node->type != XML_ENTITY_REF_NODE)) {
  1883. if (reader->node->children != NULL) {
  1884. reader->node = reader->node->children;
  1885. reader->depth++;
  1886. reader->state = XML_TEXTREADER_START;
  1887. goto found_node;
  1888. }
  1889. if (reader->node->type == XML_ATTRIBUTE_NODE) {
  1890. reader->state = XML_TEXTREADER_BACKTRACK;
  1891. goto found_node;
  1892. }
  1893. }
  1894. if (reader->node->next != NULL) {
  1895. reader->node = reader->node->next;
  1896. reader->state = XML_TEXTREADER_START;
  1897. goto found_node;
  1898. }
  1899. if (reader->node->parent != NULL) {
  1900. if ((reader->node->parent->type == XML_DOCUMENT_NODE) ||
  1901. (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) {
  1902. reader->state = XML_TEXTREADER_END;
  1903. return(0);
  1904. }
  1905. reader->node = reader->node->parent;
  1906. reader->depth--;
  1907. reader->state = XML_TEXTREADER_BACKTRACK;
  1908. goto found_node;
  1909. }
  1910. reader->state = XML_TEXTREADER_END;
  1911. found_node:
  1912. if ((reader->node->type == XML_XINCLUDE_START) ||
  1913. (reader->node->type == XML_XINCLUDE_END))
  1914. goto next_node;
  1915. return(1);
  1916. }
  1917. /**
  1918. * xmlTextReaderNextSibling:
  1919. * @reader: the xmlTextReaderPtr used
  1920. *
  1921. * Skip to the node following the current one in document order while
  1922. * avoiding the subtree if any.
  1923. * Currently implemented only for Readers built on a document
  1924. *
  1925. * Returns 1 if the node was read successfully, 0 if there is no more
  1926. * nodes to read, or -1 in case of error
  1927. */
  1928. int
  1929. xmlTextReaderNextSibling(xmlTextReaderPtr reader) {
  1930. if (reader == NULL)
  1931. return(-1);
  1932. if (reader->doc == NULL) {
  1933. /* TODO */
  1934. return(-1);
  1935. }
  1936. if (reader->state == XML_TEXTREADER_END)
  1937. return(0);
  1938. if (reader->node == NULL)
  1939. return(xmlTextReaderNextTree(reader));
  1940. if (reader->node->next != NULL) {
  1941. reader->node = reader->node->next;
  1942. reader->state = XML_TEXTREADER_START;
  1943. return(1);
  1944. }
  1945. return(0);
  1946. }
  1947. /************************************************************************
  1948. * *
  1949. * Constructor and destructors *
  1950. * *
  1951. ************************************************************************/
  1952. /**
  1953. * xmlNewTextReader:
  1954. * @input: the xmlParserInputBufferPtr used to read data
  1955. * @URI: the URI information for the source if available
  1956. *
  1957. * Create an xmlTextReader structure fed with @input
  1958. *
  1959. * Returns the new xmlTextReaderPtr or NULL in case of error
  1960. */
  1961. xmlTextReaderPtr
  1962. xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
  1963. xmlTextReaderPtr ret;
  1964. if (input == NULL)
  1965. return(NULL);
  1966. ret = xmlMalloc(sizeof(xmlTextReader));
  1967. if (ret == NULL) {
  1968. xmlGenericError(xmlGenericErrorContext,
  1969. "xmlNewTextReader : malloc failed\n");
  1970. return(NULL);
  1971. }
  1972. memset(ret, 0, sizeof(xmlTextReader));
  1973. ret->doc = NULL;
  1974. ret->entTab = NULL;
  1975. ret->entMax = 0;
  1976. ret->entNr = 0;
  1977. ret->input = input;
  1978. ret->buffer = xmlBufCreateSize(100);
  1979. if (ret->buffer == NULL) {
  1980. xmlFree(ret);
  1981. xmlGenericError(xmlGenericErrorContext,
  1982. "xmlNewTextReader : malloc failed\n");
  1983. return(NULL);
  1984. }
  1985. /* no operation on a reader should require a huge buffer */
  1986. xmlBufSetAllocationScheme(ret->buffer,
  1987. XML_BUFFER_ALLOC_BOUNDED);
  1988. ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
  1989. if (ret->sax == NULL) {
  1990. xmlBufFree(ret->buffer);
  1991. xmlFree(ret);
  1992. xmlGenericError(xmlGenericErrorContext,
  1993. "xmlNewTextReader : malloc failed\n");
  1994. return(NULL);
  1995. }
  1996. xmlSAXVersion(ret->sax, 2);
  1997. ret->startElement = ret->sax->startElement;
  1998. ret->sax->startElement = xmlTextReaderStartElement;
  1999. ret->endElement = ret->sax->endElement;
  2000. ret->sax->endElement = xmlTextReaderEndElement;
  2001. #ifdef LIBXML_SAX1_ENABLED
  2002. if (ret->sax->initialized == XML_SAX2_MAGIC) {
  2003. #endif /* LIBXML_SAX1_ENABLED */
  2004. ret->startElementNs = ret->sax->startElementNs;
  2005. ret->sax->startElementNs = xmlTextReaderStartElementNs;
  2006. ret->endElementNs = ret->sax->endElementNs;
  2007. ret->sax->endElementNs = xmlTextReaderEndElementNs;
  2008. #ifdef LIBXML_SAX1_ENABLED
  2009. } else {
  2010. ret->startElementNs = NULL;
  2011. ret->endElementNs = NULL;
  2012. }
  2013. #endif /* LIBXML_SAX1_ENABLED */
  2014. ret->characters = ret->sax->characters;
  2015. ret->sax->characters = xmlTextReaderCharacters;
  2016. ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
  2017. ret->cdataBlock = ret->sax->cdataBlock;
  2018. ret->sax->cdataBlock = xmlTextReaderCDataBlock;
  2019. ret->mode = XML_TEXTREADER_MODE_INITIAL;
  2020. ret->node = NULL;
  2021. ret->curnode = NULL;
  2022. if (xmlBufUse(ret->input->buffer) < 4) {
  2023. xmlParserInputBufferRead(input, 4);
  2024. }
  2025. if (xmlBufUse(ret->input->buffer) >= 4) {
  2026. ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
  2027. (const char *) xmlBufContent(ret->input->buffer),
  2028. 4, URI);
  2029. ret->base = 0;
  2030. ret->cur = 4;
  2031. } else {
  2032. ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
  2033. ret->base = 0;
  2034. ret->cur = 0;
  2035. }
  2036. if (ret->ctxt == NULL) {
  2037. xmlGenericError(xmlGenericErrorContext,
  2038. "xmlNewTextReader : malloc failed\n");
  2039. xmlBufFree(ret->buffer);
  2040. xmlFree(ret->sax);
  2041. xmlFree(ret);
  2042. return(NULL);
  2043. }
  2044. ret->ctxt->parseMode = XML_PARSE_READER;
  2045. ret->ctxt->_private = ret;
  2046. ret->ctxt->linenumbers = 1;
  2047. ret->ctxt->dictNames = 1;
  2048. ret->allocs = XML_TEXTREADER_CTXT;
  2049. /*
  2050. * use the parser dictionary to allocate all elements and attributes names
  2051. */
  2052. ret->ctxt->docdict = 1;
  2053. ret->dict = ret->ctxt->dict;
  2054. #ifdef LIBXML_XINCLUDE_ENABLED
  2055. ret->xinclude = 0;
  2056. #endif
  2057. #ifdef LIBXML_PATTERN_ENABLED
  2058. ret->patternMax = 0;
  2059. ret->patternTab = NULL;
  2060. #endif
  2061. return(ret);
  2062. }
  2063. /**
  2064. * xmlNewTextReaderFilename:
  2065. * @URI: the URI of the resource to process
  2066. *
  2067. * Create an xmlTextReader structure fed with the resource at @URI
  2068. *
  2069. * Returns the new xmlTextReaderPtr or NULL in case of error
  2070. */
  2071. xmlTextReaderPtr
  2072. xmlNewTextReaderFilename(const char *URI) {
  2073. xmlParserInputBufferPtr input;
  2074. xmlTextReaderPtr ret;
  2075. char *directory = NULL;
  2076. input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
  2077. if (input == NULL)
  2078. return(NULL);
  2079. ret = xmlNewTextReader(input, URI);
  2080. if (ret == NULL) {
  2081. xmlFreeParserInputBuffer(input);
  2082. return(NULL);
  2083. }
  2084. ret->allocs |= XML_TEXTREADER_INPUT;
  2085. if (ret->ctxt->directory == NULL)
  2086. directory = xmlParserGetDirectory(URI);
  2087. if ((ret->ctxt->directory == NULL) && (directory != NULL))
  2088. ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
  2089. if (directory != NULL)
  2090. xmlFree(directory);
  2091. return(ret);
  2092. }
  2093. /**
  2094. * xmlFreeTextReader:
  2095. * @reader: the xmlTextReaderPtr
  2096. *
  2097. * Deallocate all the resources associated to the reader
  2098. */
  2099. void
  2100. xmlFreeTextReader(xmlTextReaderPtr reader) {
  2101. if (reader == NULL)
  2102. return;
  2103. #ifdef LIBXML_SCHEMAS_ENABLED
  2104. if (reader->rngSchemas != NULL) {
  2105. xmlRelaxNGFree(reader->rngSchemas);
  2106. reader->rngSchemas = NULL;
  2107. }
  2108. if (reader->rngValidCtxt != NULL) {
  2109. if (! reader->rngPreserveCtxt)
  2110. xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
  2111. reader->rngValidCtxt = NULL;
  2112. }
  2113. if (reader->xsdPlug != NULL) {
  2114. xmlSchemaSAXUnplug(reader->xsdPlug);
  2115. reader->xsdPlug = NULL;
  2116. }
  2117. if (reader->xsdValidCtxt != NULL) {
  2118. if (! reader->xsdPreserveCtxt)
  2119. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  2120. reader->xsdValidCtxt = NULL;
  2121. }
  2122. if (reader->xsdSchemas != NULL) {
  2123. xmlSchemaFree(reader->xsdSchemas);
  2124. reader->xsdSchemas = NULL;
  2125. }
  2126. #endif
  2127. #ifdef LIBXML_XINCLUDE_ENABLED
  2128. if (reader->xincctxt != NULL)
  2129. xmlXIncludeFreeContext(reader->xincctxt);
  2130. #endif
  2131. #ifdef LIBXML_PATTERN_ENABLED
  2132. if (reader->patternTab != NULL) {
  2133. int i;
  2134. for (i = 0;i < reader->patternNr;i++) {
  2135. if (reader->patternTab[i] != NULL)
  2136. xmlFreePattern(reader->patternTab[i]);
  2137. }
  2138. xmlFree(reader->patternTab);
  2139. }
  2140. #endif
  2141. if (reader->faketext != NULL) {
  2142. xmlFreeNode(reader->faketext);
  2143. }
  2144. if (reader->ctxt != NULL) {
  2145. if (reader->dict == reader->ctxt->dict)
  2146. reader->dict = NULL;
  2147. #ifdef LIBXML_VALID_ENABLED
  2148. if ((reader->ctxt->vctxt.vstateTab != NULL) &&
  2149. (reader->ctxt->vctxt.vstateMax > 0)){
  2150. #ifdef LIBXML_REGEXP_ENABLED
  2151. while (reader->ctxt->vctxt.vstateNr > 0)
  2152. xmlValidatePopElement(&reader->ctxt->vctxt, NULL, NULL, NULL);
  2153. #endif /* LIBXML_REGEXP_ENABLED */
  2154. xmlFree(reader->ctxt->vctxt.vstateTab);
  2155. reader->ctxt->vctxt.vstateTab = NULL;
  2156. reader->ctxt->vctxt.vstateMax = 0;
  2157. }
  2158. #endif /* LIBXML_VALID_ENABLED */
  2159. if (reader->ctxt->myDoc != NULL) {
  2160. if (reader->preserve == 0)
  2161. xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
  2162. reader->ctxt->myDoc = NULL;
  2163. }
  2164. if (reader->allocs & XML_TEXTREADER_CTXT)
  2165. xmlFreeParserCtxt(reader->ctxt);
  2166. }
  2167. if (reader->sax != NULL)
  2168. xmlFree(reader->sax);
  2169. if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
  2170. xmlFreeParserInputBuffer(reader->input);
  2171. if (reader->buffer != NULL)
  2172. xmlBufFree(reader->buffer);
  2173. if (reader->entTab != NULL)
  2174. xmlFree(reader->entTab);
  2175. if (reader->dict != NULL)
  2176. xmlDictFree(reader->dict);
  2177. xmlFree(reader);
  2178. }
  2179. /************************************************************************
  2180. * *
  2181. * Methods for XmlTextReader *
  2182. * *
  2183. ************************************************************************/
  2184. /**
  2185. * xmlTextReaderClose:
  2186. * @reader: the xmlTextReaderPtr used
  2187. *
  2188. * This method releases any resources allocated by the current instance
  2189. * changes the state to Closed and close any underlying input.
  2190. *
  2191. * Returns 0 or -1 in case of error
  2192. */
  2193. int
  2194. xmlTextReaderClose(xmlTextReaderPtr reader) {
  2195. if (reader == NULL)
  2196. return(-1);
  2197. reader->node = NULL;
  2198. reader->curnode = NULL;
  2199. reader->mode = XML_TEXTREADER_MODE_CLOSED;
  2200. if (reader->ctxt != NULL) {
  2201. xmlStopParser(reader->ctxt);
  2202. if (reader->ctxt->myDoc != NULL) {
  2203. if (reader->preserve == 0)
  2204. xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
  2205. reader->ctxt->myDoc = NULL;
  2206. }
  2207. }
  2208. if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
  2209. xmlFreeParserInputBuffer(reader->input);
  2210. reader->allocs -= XML_TEXTREADER_INPUT;
  2211. }
  2212. return(0);
  2213. }
  2214. /**
  2215. * xmlTextReaderGetAttributeNo:
  2216. * @reader: the xmlTextReaderPtr used
  2217. * @no: the zero-based index of the attribute relative to the containing element
  2218. *
  2219. * Provides the value of the attribute with the specified index relative
  2220. * to the containing element.
  2221. *
  2222. * Returns a string containing the value of the specified attribute, or NULL
  2223. * in case of error. The string must be deallocated by the caller.
  2224. */
  2225. xmlChar *
  2226. xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
  2227. xmlChar *ret;
  2228. int i;
  2229. xmlAttrPtr cur;
  2230. xmlNsPtr ns;
  2231. if (reader == NULL)
  2232. return(NULL);
  2233. if (reader->node == NULL)
  2234. return(NULL);
  2235. if (reader->curnode != NULL)
  2236. return(NULL);
  2237. /* TODO: handle the xmlDecl */
  2238. if (reader->node->type != XML_ELEMENT_NODE)
  2239. return(NULL);
  2240. ns = reader->node->nsDef;
  2241. for (i = 0;(i < no) && (ns != NULL);i++) {
  2242. ns = ns->next;
  2243. }
  2244. if (ns != NULL)
  2245. return(xmlStrdup(ns->href));
  2246. cur = reader->node->properties;
  2247. if (cur == NULL)
  2248. return(NULL);
  2249. for (;i < no;i++) {
  2250. cur = cur->next;
  2251. if (cur == NULL)
  2252. return(NULL);
  2253. }
  2254. /* TODO walk the DTD if present */
  2255. ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
  2256. if (ret == NULL) return(xmlStrdup((xmlChar *)""));
  2257. return(ret);
  2258. }
  2259. /**
  2260. * xmlTextReaderGetAttribute:
  2261. * @reader: the xmlTextReaderPtr used
  2262. * @name: the qualified name of the attribute.
  2263. *
  2264. * Provides the value of the attribute with the specified qualified name.
  2265. *
  2266. * Returns a string containing the value of the specified attribute, or NULL
  2267. * in case of error. The string must be deallocated by the caller.
  2268. */
  2269. xmlChar *
  2270. xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
  2271. xmlChar *prefix = NULL;
  2272. xmlChar *localname;
  2273. xmlNsPtr ns;
  2274. xmlChar *ret = NULL;
  2275. if ((reader == NULL) || (name == NULL))
  2276. return(NULL);
  2277. if (reader->node == NULL)
  2278. return(NULL);
  2279. if (reader->curnode != NULL)
  2280. return(NULL);
  2281. /* TODO: handle the xmlDecl */
  2282. if (reader->node->type != XML_ELEMENT_NODE)
  2283. return(NULL);
  2284. localname = xmlSplitQName2(name, &prefix);
  2285. if (localname == NULL) {
  2286. /*
  2287. * Namespace default decl
  2288. */
  2289. if (xmlStrEqual(name, BAD_CAST "xmlns")) {
  2290. ns = reader->node->nsDef;
  2291. while (ns != NULL) {
  2292. if (ns->prefix == NULL) {
  2293. return(xmlStrdup(ns->href));
  2294. }
  2295. ns = ns->next;
  2296. }
  2297. return NULL;
  2298. }
  2299. return(xmlGetNoNsProp(reader->node, name));
  2300. }
  2301. /*
  2302. * Namespace default decl
  2303. */
  2304. if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
  2305. ns = reader->node->nsDef;
  2306. while (ns != NULL) {
  2307. if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
  2308. ret = xmlStrdup(ns->href);
  2309. break;
  2310. }
  2311. ns = ns->next;
  2312. }
  2313. } else {
  2314. ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
  2315. if (ns != NULL)
  2316. ret = xmlGetNsProp(reader->node, localname, ns->href);
  2317. }
  2318. xmlFree(localname);
  2319. if (prefix != NULL)
  2320. xmlFree(prefix);
  2321. return(ret);
  2322. }
  2323. /**
  2324. * xmlTextReaderGetAttributeNs:
  2325. * @reader: the xmlTextReaderPtr used
  2326. * @localName: the local name of the attribute.
  2327. * @namespaceURI: the namespace URI of the attribute.
  2328. *
  2329. * Provides the value of the specified attribute
  2330. *
  2331. * Returns a string containing the value of the specified attribute, or NULL
  2332. * in case of error. The string must be deallocated by the caller.
  2333. */
  2334. xmlChar *
  2335. xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
  2336. const xmlChar *namespaceURI) {
  2337. xmlChar *prefix = NULL;
  2338. xmlNsPtr ns;
  2339. if ((reader == NULL) || (localName == NULL))
  2340. return(NULL);
  2341. if (reader->node == NULL)
  2342. return(NULL);
  2343. if (reader->curnode != NULL)
  2344. return(NULL);
  2345. /* TODO: handle the xmlDecl */
  2346. if (reader->node->type != XML_ELEMENT_NODE)
  2347. return(NULL);
  2348. if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
  2349. if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
  2350. prefix = BAD_CAST localName;
  2351. }
  2352. ns = reader->node->nsDef;
  2353. while (ns != NULL) {
  2354. if ((prefix == NULL && ns->prefix == NULL) ||
  2355. ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
  2356. return xmlStrdup(ns->href);
  2357. }
  2358. ns = ns->next;
  2359. }
  2360. return NULL;
  2361. }
  2362. return(xmlGetNsProp(reader->node, localName, namespaceURI));
  2363. }
  2364. /**
  2365. * xmlTextReaderGetRemainder:
  2366. * @reader: the xmlTextReaderPtr used
  2367. *
  2368. * Method to get the remainder of the buffered XML. this method stops the
  2369. * parser, set its state to End Of File and return the input stream with
  2370. * what is left that the parser did not use.
  2371. *
  2372. * The implementation is not good, the parser certainly progressed past
  2373. * what's left in reader->input, and there is an allocation problem. Best
  2374. * would be to rewrite it differently.
  2375. *
  2376. * Returns the xmlParserInputBufferPtr attached to the XML or NULL
  2377. * in case of error.
  2378. */
  2379. xmlParserInputBufferPtr
  2380. xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
  2381. xmlParserInputBufferPtr ret = NULL;
  2382. if (reader == NULL)
  2383. return(NULL);
  2384. if (reader->node == NULL)
  2385. return(NULL);
  2386. reader->node = NULL;
  2387. reader->curnode = NULL;
  2388. reader->mode = XML_TEXTREADER_MODE_EOF;
  2389. if (reader->ctxt != NULL) {
  2390. xmlStopParser(reader->ctxt);
  2391. if (reader->ctxt->myDoc != NULL) {
  2392. if (reader->preserve == 0)
  2393. xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
  2394. reader->ctxt->myDoc = NULL;
  2395. }
  2396. }
  2397. if (reader->allocs & XML_TEXTREADER_INPUT) {
  2398. ret = reader->input;
  2399. reader->input = NULL;
  2400. reader->allocs -= XML_TEXTREADER_INPUT;
  2401. } else {
  2402. /*
  2403. * Hum, one may need to duplicate the data structure because
  2404. * without reference counting the input may be freed twice:
  2405. * - by the layer which allocated it.
  2406. * - by the layer to which would have been returned to.
  2407. */
  2408. TODO
  2409. return(NULL);
  2410. }
  2411. return(ret);
  2412. }
  2413. /**
  2414. * xmlTextReaderLookupNamespace:
  2415. * @reader: the xmlTextReaderPtr used
  2416. * @prefix: the prefix whose namespace URI is to be resolved. To return
  2417. * the default namespace, specify NULL
  2418. *
  2419. * Resolves a namespace prefix in the scope of the current element.
  2420. *
  2421. * Returns a string containing the namespace URI to which the prefix maps
  2422. * or NULL in case of error. The string must be deallocated by the caller.
  2423. */
  2424. xmlChar *
  2425. xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
  2426. xmlNsPtr ns;
  2427. if (reader == NULL)
  2428. return(NULL);
  2429. if (reader->node == NULL)
  2430. return(NULL);
  2431. ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
  2432. if (ns == NULL)
  2433. return(NULL);
  2434. return(xmlStrdup(ns->href));
  2435. }
  2436. /**
  2437. * xmlTextReaderMoveToAttributeNo:
  2438. * @reader: the xmlTextReaderPtr used
  2439. * @no: the zero-based index of the attribute relative to the containing
  2440. * element.
  2441. *
  2442. * Moves the position of the current instance to the attribute with
  2443. * the specified index relative to the containing element.
  2444. *
  2445. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2446. */
  2447. int
  2448. xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
  2449. int i;
  2450. xmlAttrPtr cur;
  2451. xmlNsPtr ns;
  2452. if (reader == NULL)
  2453. return(-1);
  2454. if (reader->node == NULL)
  2455. return(-1);
  2456. /* TODO: handle the xmlDecl */
  2457. if (reader->node->type != XML_ELEMENT_NODE)
  2458. return(-1);
  2459. reader->curnode = NULL;
  2460. ns = reader->node->nsDef;
  2461. for (i = 0;(i < no) && (ns != NULL);i++) {
  2462. ns = ns->next;
  2463. }
  2464. if (ns != NULL) {
  2465. reader->curnode = (xmlNodePtr) ns;
  2466. return(1);
  2467. }
  2468. cur = reader->node->properties;
  2469. if (cur == NULL)
  2470. return(0);
  2471. for (;i < no;i++) {
  2472. cur = cur->next;
  2473. if (cur == NULL)
  2474. return(0);
  2475. }
  2476. /* TODO walk the DTD if present */
  2477. reader->curnode = (xmlNodePtr) cur;
  2478. return(1);
  2479. }
  2480. /**
  2481. * xmlTextReaderMoveToAttribute:
  2482. * @reader: the xmlTextReaderPtr used
  2483. * @name: the qualified name of the attribute.
  2484. *
  2485. * Moves the position of the current instance to the attribute with
  2486. * the specified qualified name.
  2487. *
  2488. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2489. */
  2490. int
  2491. xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
  2492. xmlChar *prefix = NULL;
  2493. xmlChar *localname;
  2494. xmlNsPtr ns;
  2495. xmlAttrPtr prop;
  2496. if ((reader == NULL) || (name == NULL))
  2497. return(-1);
  2498. if (reader->node == NULL)
  2499. return(-1);
  2500. /* TODO: handle the xmlDecl */
  2501. if (reader->node->type != XML_ELEMENT_NODE)
  2502. return(0);
  2503. localname = xmlSplitQName2(name, &prefix);
  2504. if (localname == NULL) {
  2505. /*
  2506. * Namespace default decl
  2507. */
  2508. if (xmlStrEqual(name, BAD_CAST "xmlns")) {
  2509. ns = reader->node->nsDef;
  2510. while (ns != NULL) {
  2511. if (ns->prefix == NULL) {
  2512. reader->curnode = (xmlNodePtr) ns;
  2513. return(1);
  2514. }
  2515. ns = ns->next;
  2516. }
  2517. return(0);
  2518. }
  2519. prop = reader->node->properties;
  2520. while (prop != NULL) {
  2521. /*
  2522. * One need to have
  2523. * - same attribute names
  2524. * - and the attribute carrying that namespace
  2525. */
  2526. if ((xmlStrEqual(prop->name, name)) &&
  2527. ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
  2528. reader->curnode = (xmlNodePtr) prop;
  2529. return(1);
  2530. }
  2531. prop = prop->next;
  2532. }
  2533. return(0);
  2534. }
  2535. /*
  2536. * Namespace default decl
  2537. */
  2538. if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
  2539. ns = reader->node->nsDef;
  2540. while (ns != NULL) {
  2541. if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
  2542. reader->curnode = (xmlNodePtr) ns;
  2543. goto found;
  2544. }
  2545. ns = ns->next;
  2546. }
  2547. goto not_found;
  2548. }
  2549. prop = reader->node->properties;
  2550. while (prop != NULL) {
  2551. /*
  2552. * One need to have
  2553. * - same attribute names
  2554. * - and the attribute carrying that namespace
  2555. */
  2556. if ((xmlStrEqual(prop->name, localname)) &&
  2557. (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
  2558. reader->curnode = (xmlNodePtr) prop;
  2559. goto found;
  2560. }
  2561. prop = prop->next;
  2562. }
  2563. not_found:
  2564. if (localname != NULL)
  2565. xmlFree(localname);
  2566. if (prefix != NULL)
  2567. xmlFree(prefix);
  2568. return(0);
  2569. found:
  2570. if (localname != NULL)
  2571. xmlFree(localname);
  2572. if (prefix != NULL)
  2573. xmlFree(prefix);
  2574. return(1);
  2575. }
  2576. /**
  2577. * xmlTextReaderMoveToAttributeNs:
  2578. * @reader: the xmlTextReaderPtr used
  2579. * @localName: the local name of the attribute.
  2580. * @namespaceURI: the namespace URI of the attribute.
  2581. *
  2582. * Moves the position of the current instance to the attribute with the
  2583. * specified local name and namespace URI.
  2584. *
  2585. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2586. */
  2587. int
  2588. xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
  2589. const xmlChar *localName, const xmlChar *namespaceURI) {
  2590. xmlAttrPtr prop;
  2591. xmlNodePtr node;
  2592. xmlNsPtr ns;
  2593. xmlChar *prefix = NULL;
  2594. if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
  2595. return(-1);
  2596. if (reader->node == NULL)
  2597. return(-1);
  2598. if (reader->node->type != XML_ELEMENT_NODE)
  2599. return(0);
  2600. node = reader->node;
  2601. if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
  2602. if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
  2603. prefix = BAD_CAST localName;
  2604. }
  2605. ns = reader->node->nsDef;
  2606. while (ns != NULL) {
  2607. if ((prefix == NULL && ns->prefix == NULL) ||
  2608. ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
  2609. reader->curnode = (xmlNodePtr) ns;
  2610. return(1);
  2611. }
  2612. ns = ns->next;
  2613. }
  2614. return(0);
  2615. }
  2616. prop = node->properties;
  2617. while (prop != NULL) {
  2618. /*
  2619. * One need to have
  2620. * - same attribute names
  2621. * - and the attribute carrying that namespace
  2622. */
  2623. if (xmlStrEqual(prop->name, localName) &&
  2624. ((prop->ns != NULL) &&
  2625. (xmlStrEqual(prop->ns->href, namespaceURI)))) {
  2626. reader->curnode = (xmlNodePtr) prop;
  2627. return(1);
  2628. }
  2629. prop = prop->next;
  2630. }
  2631. return(0);
  2632. }
  2633. /**
  2634. * xmlTextReaderMoveToFirstAttribute:
  2635. * @reader: the xmlTextReaderPtr used
  2636. *
  2637. * Moves the position of the current instance to the first attribute
  2638. * associated with the current node.
  2639. *
  2640. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2641. */
  2642. int
  2643. xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
  2644. if (reader == NULL)
  2645. return(-1);
  2646. if (reader->node == NULL)
  2647. return(-1);
  2648. if (reader->node->type != XML_ELEMENT_NODE)
  2649. return(0);
  2650. if (reader->node->nsDef != NULL) {
  2651. reader->curnode = (xmlNodePtr) reader->node->nsDef;
  2652. return(1);
  2653. }
  2654. if (reader->node->properties != NULL) {
  2655. reader->curnode = (xmlNodePtr) reader->node->properties;
  2656. return(1);
  2657. }
  2658. return(0);
  2659. }
  2660. /**
  2661. * xmlTextReaderMoveToNextAttribute:
  2662. * @reader: the xmlTextReaderPtr used
  2663. *
  2664. * Moves the position of the current instance to the next attribute
  2665. * associated with the current node.
  2666. *
  2667. * Returns 1 in case of success, -1 in case of error, 0 if not found
  2668. */
  2669. int
  2670. xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
  2671. if (reader == NULL)
  2672. return(-1);
  2673. if (reader->node == NULL)
  2674. return(-1);
  2675. if (reader->node->type != XML_ELEMENT_NODE)
  2676. return(0);
  2677. if (reader->curnode == NULL)
  2678. return(xmlTextReaderMoveToFirstAttribute(reader));
  2679. if (reader->curnode->type == XML_NAMESPACE_DECL) {
  2680. xmlNsPtr ns = (xmlNsPtr) reader->curnode;
  2681. if (ns->next != NULL) {
  2682. reader->curnode = (xmlNodePtr) ns->next;
  2683. return(1);
  2684. }
  2685. if (reader->node->properties != NULL) {
  2686. reader->curnode = (xmlNodePtr) reader->node->properties;
  2687. return(1);
  2688. }
  2689. return(0);
  2690. } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
  2691. (reader->curnode->next != NULL)) {
  2692. reader->curnode = reader->curnode->next;
  2693. return(1);
  2694. }
  2695. return(0);
  2696. }
  2697. /**
  2698. * xmlTextReaderMoveToElement:
  2699. * @reader: the xmlTextReaderPtr used
  2700. *
  2701. * Moves the position of the current instance to the node that
  2702. * contains the current Attribute node.
  2703. *
  2704. * Returns 1 in case of success, -1 in case of error, 0 if not moved
  2705. */
  2706. int
  2707. xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
  2708. if (reader == NULL)
  2709. return(-1);
  2710. if (reader->node == NULL)
  2711. return(-1);
  2712. if (reader->node->type != XML_ELEMENT_NODE)
  2713. return(0);
  2714. if (reader->curnode != NULL) {
  2715. reader->curnode = NULL;
  2716. return(1);
  2717. }
  2718. return(0);
  2719. }
  2720. /**
  2721. * xmlTextReaderReadAttributeValue:
  2722. * @reader: the xmlTextReaderPtr used
  2723. *
  2724. * Parses an attribute value into one or more Text and EntityReference nodes.
  2725. *
  2726. * Returns 1 in case of success, 0 if the reader was not positioned on an
  2727. * attribute node or all the attribute values have been read, or -1
  2728. * in case of error.
  2729. */
  2730. int
  2731. xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
  2732. if (reader == NULL)
  2733. return(-1);
  2734. if (reader->node == NULL)
  2735. return(-1);
  2736. if (reader->curnode == NULL)
  2737. return(0);
  2738. if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
  2739. if (reader->curnode->children == NULL)
  2740. return(0);
  2741. reader->curnode = reader->curnode->children;
  2742. } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
  2743. xmlNsPtr ns = (xmlNsPtr) reader->curnode;
  2744. if (reader->faketext == NULL) {
  2745. reader->faketext = xmlNewDocText(reader->node->doc,
  2746. ns->href);
  2747. } else {
  2748. if ((reader->faketext->content != NULL) &&
  2749. (reader->faketext->content !=
  2750. (xmlChar *) &(reader->faketext->properties)))
  2751. xmlFree(reader->faketext->content);
  2752. reader->faketext->content = xmlStrdup(ns->href);
  2753. }
  2754. reader->curnode = reader->faketext;
  2755. } else {
  2756. if (reader->curnode->next == NULL)
  2757. return(0);
  2758. reader->curnode = reader->curnode->next;
  2759. }
  2760. return(1);
  2761. }
  2762. /**
  2763. * xmlTextReaderConstEncoding:
  2764. * @reader: the xmlTextReaderPtr used
  2765. *
  2766. * Determine the encoding of the document being read.
  2767. *
  2768. * Returns a string containing the encoding of the document or NULL in
  2769. * case of error. The string is deallocated with the reader.
  2770. */
  2771. const xmlChar *
  2772. xmlTextReaderConstEncoding(xmlTextReaderPtr reader) {
  2773. xmlDocPtr doc = NULL;
  2774. if (reader == NULL)
  2775. return(NULL);
  2776. if (reader->doc != NULL)
  2777. doc = reader->doc;
  2778. else if (reader->ctxt != NULL)
  2779. doc = reader->ctxt->myDoc;
  2780. if (doc == NULL)
  2781. return(NULL);
  2782. if (doc->encoding == NULL)
  2783. return(NULL);
  2784. else
  2785. return(CONSTSTR(doc->encoding));
  2786. }
  2787. /************************************************************************
  2788. * *
  2789. * Access API to the current node *
  2790. * *
  2791. ************************************************************************/
  2792. /**
  2793. * xmlTextReaderAttributeCount:
  2794. * @reader: the xmlTextReaderPtr used
  2795. *
  2796. * Provides the number of attributes of the current node
  2797. *
  2798. * Returns 0 i no attributes, -1 in case of error or the attribute count
  2799. */
  2800. int
  2801. xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
  2802. int ret;
  2803. xmlAttrPtr attr;
  2804. xmlNsPtr ns;
  2805. xmlNodePtr node;
  2806. if (reader == NULL)
  2807. return(-1);
  2808. if (reader->node == NULL)
  2809. return(0);
  2810. if (reader->curnode != NULL)
  2811. node = reader->curnode;
  2812. else
  2813. node = reader->node;
  2814. if (node->type != XML_ELEMENT_NODE)
  2815. return(0);
  2816. if ((reader->state == XML_TEXTREADER_END) ||
  2817. (reader->state == XML_TEXTREADER_BACKTRACK))
  2818. return(0);
  2819. ret = 0;
  2820. attr = node->properties;
  2821. while (attr != NULL) {
  2822. ret++;
  2823. attr = attr->next;
  2824. }
  2825. ns = node->nsDef;
  2826. while (ns != NULL) {
  2827. ret++;
  2828. ns = ns->next;
  2829. }
  2830. return(ret);
  2831. }
  2832. /**
  2833. * xmlTextReaderNodeType:
  2834. * @reader: the xmlTextReaderPtr used
  2835. *
  2836. * Get the node type of the current node
  2837. * Reference:
  2838. * http://www.gnu.org/software/dotgnu/pnetlib-doc/System/Xml/XmlNodeType.html
  2839. *
  2840. * Returns the xmlReaderTypes of the current node or -1 in case of error
  2841. */
  2842. int
  2843. xmlTextReaderNodeType(xmlTextReaderPtr reader) {
  2844. xmlNodePtr node;
  2845. if (reader == NULL)
  2846. return(-1);
  2847. if (reader->node == NULL)
  2848. return(XML_READER_TYPE_NONE);
  2849. if (reader->curnode != NULL)
  2850. node = reader->curnode;
  2851. else
  2852. node = reader->node;
  2853. switch (node->type) {
  2854. case XML_ELEMENT_NODE:
  2855. if ((reader->state == XML_TEXTREADER_END) ||
  2856. (reader->state == XML_TEXTREADER_BACKTRACK))
  2857. return(XML_READER_TYPE_END_ELEMENT);
  2858. return(XML_READER_TYPE_ELEMENT);
  2859. case XML_NAMESPACE_DECL:
  2860. case XML_ATTRIBUTE_NODE:
  2861. return(XML_READER_TYPE_ATTRIBUTE);
  2862. case XML_TEXT_NODE:
  2863. if (xmlIsBlankNode(reader->node)) {
  2864. if (xmlNodeGetSpacePreserve(reader->node))
  2865. return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
  2866. else
  2867. return(XML_READER_TYPE_WHITESPACE);
  2868. } else {
  2869. return(XML_READER_TYPE_TEXT);
  2870. }
  2871. case XML_CDATA_SECTION_NODE:
  2872. return(XML_READER_TYPE_CDATA);
  2873. case XML_ENTITY_REF_NODE:
  2874. return(XML_READER_TYPE_ENTITY_REFERENCE);
  2875. case XML_ENTITY_NODE:
  2876. return(XML_READER_TYPE_ENTITY);
  2877. case XML_PI_NODE:
  2878. return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
  2879. case XML_COMMENT_NODE:
  2880. return(XML_READER_TYPE_COMMENT);
  2881. case XML_DOCUMENT_NODE:
  2882. case XML_HTML_DOCUMENT_NODE:
  2883. #ifdef LIBXML_DOCB_ENABLED
  2884. case XML_DOCB_DOCUMENT_NODE:
  2885. #endif
  2886. return(XML_READER_TYPE_DOCUMENT);
  2887. case XML_DOCUMENT_FRAG_NODE:
  2888. return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
  2889. case XML_NOTATION_NODE:
  2890. return(XML_READER_TYPE_NOTATION);
  2891. case XML_DOCUMENT_TYPE_NODE:
  2892. case XML_DTD_NODE:
  2893. return(XML_READER_TYPE_DOCUMENT_TYPE);
  2894. case XML_ELEMENT_DECL:
  2895. case XML_ATTRIBUTE_DECL:
  2896. case XML_ENTITY_DECL:
  2897. case XML_XINCLUDE_START:
  2898. case XML_XINCLUDE_END:
  2899. return(XML_READER_TYPE_NONE);
  2900. }
  2901. return(-1);
  2902. }
  2903. /**
  2904. * xmlTextReaderIsEmptyElement:
  2905. * @reader: the xmlTextReaderPtr used
  2906. *
  2907. * Check if the current node is empty
  2908. *
  2909. * Returns 1 if empty, 0 if not and -1 in case of error
  2910. */
  2911. int
  2912. xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
  2913. if ((reader == NULL) || (reader->node == NULL))
  2914. return(-1);
  2915. if (reader->node->type != XML_ELEMENT_NODE)
  2916. return(0);
  2917. if (reader->curnode != NULL)
  2918. return(0);
  2919. if (reader->node->children != NULL)
  2920. return(0);
  2921. if (reader->state == XML_TEXTREADER_END)
  2922. return(0);
  2923. if (reader->doc != NULL)
  2924. return(1);
  2925. #ifdef LIBXML_XINCLUDE_ENABLED
  2926. if (reader->in_xinclude > 0)
  2927. return(1);
  2928. #endif
  2929. return((reader->node->extra & NODE_IS_EMPTY) != 0);
  2930. }
  2931. /**
  2932. * xmlTextReaderLocalName:
  2933. * @reader: the xmlTextReaderPtr used
  2934. *
  2935. * The local name of the node.
  2936. *
  2937. * Returns the local name or NULL if not available,
  2938. * if non NULL it need to be freed by the caller.
  2939. */
  2940. xmlChar *
  2941. xmlTextReaderLocalName(xmlTextReaderPtr reader) {
  2942. xmlNodePtr node;
  2943. if ((reader == NULL) || (reader->node == NULL))
  2944. return(NULL);
  2945. if (reader->curnode != NULL)
  2946. node = reader->curnode;
  2947. else
  2948. node = reader->node;
  2949. if (node->type == XML_NAMESPACE_DECL) {
  2950. xmlNsPtr ns = (xmlNsPtr) node;
  2951. if (ns->prefix == NULL)
  2952. return(xmlStrdup(BAD_CAST "xmlns"));
  2953. else
  2954. return(xmlStrdup(ns->prefix));
  2955. }
  2956. if ((node->type != XML_ELEMENT_NODE) &&
  2957. (node->type != XML_ATTRIBUTE_NODE))
  2958. return(xmlTextReaderName(reader));
  2959. return(xmlStrdup(node->name));
  2960. }
  2961. /**
  2962. * xmlTextReaderConstLocalName:
  2963. * @reader: the xmlTextReaderPtr used
  2964. *
  2965. * The local name of the node.
  2966. *
  2967. * Returns the local name or NULL if not available, the
  2968. * string will be deallocated with the reader.
  2969. */
  2970. const xmlChar *
  2971. xmlTextReaderConstLocalName(xmlTextReaderPtr reader) {
  2972. xmlNodePtr node;
  2973. if ((reader == NULL) || (reader->node == NULL))
  2974. return(NULL);
  2975. if (reader->curnode != NULL)
  2976. node = reader->curnode;
  2977. else
  2978. node = reader->node;
  2979. if (node->type == XML_NAMESPACE_DECL) {
  2980. xmlNsPtr ns = (xmlNsPtr) node;
  2981. if (ns->prefix == NULL)
  2982. return(CONSTSTR(BAD_CAST "xmlns"));
  2983. else
  2984. return(ns->prefix);
  2985. }
  2986. if ((node->type != XML_ELEMENT_NODE) &&
  2987. (node->type != XML_ATTRIBUTE_NODE))
  2988. return(xmlTextReaderConstName(reader));
  2989. return(node->name);
  2990. }
  2991. /**
  2992. * xmlTextReaderName:
  2993. * @reader: the xmlTextReaderPtr used
  2994. *
  2995. * The qualified name of the node, equal to Prefix :LocalName.
  2996. *
  2997. * Returns the local name or NULL if not available,
  2998. * if non NULL it need to be freed by the caller.
  2999. */
  3000. xmlChar *
  3001. xmlTextReaderName(xmlTextReaderPtr reader) {
  3002. xmlNodePtr node;
  3003. xmlChar *ret;
  3004. if ((reader == NULL) || (reader->node == NULL))
  3005. return(NULL);
  3006. if (reader->curnode != NULL)
  3007. node = reader->curnode;
  3008. else
  3009. node = reader->node;
  3010. switch (node->type) {
  3011. case XML_ELEMENT_NODE:
  3012. case XML_ATTRIBUTE_NODE:
  3013. if ((node->ns == NULL) ||
  3014. (node->ns->prefix == NULL))
  3015. return(xmlStrdup(node->name));
  3016. ret = xmlStrdup(node->ns->prefix);
  3017. ret = xmlStrcat(ret, BAD_CAST ":");
  3018. ret = xmlStrcat(ret, node->name);
  3019. return(ret);
  3020. case XML_TEXT_NODE:
  3021. return(xmlStrdup(BAD_CAST "#text"));
  3022. case XML_CDATA_SECTION_NODE:
  3023. return(xmlStrdup(BAD_CAST "#cdata-section"));
  3024. case XML_ENTITY_NODE:
  3025. case XML_ENTITY_REF_NODE:
  3026. return(xmlStrdup(node->name));
  3027. case XML_PI_NODE:
  3028. return(xmlStrdup(node->name));
  3029. case XML_COMMENT_NODE:
  3030. return(xmlStrdup(BAD_CAST "#comment"));
  3031. case XML_DOCUMENT_NODE:
  3032. case XML_HTML_DOCUMENT_NODE:
  3033. #ifdef LIBXML_DOCB_ENABLED
  3034. case XML_DOCB_DOCUMENT_NODE:
  3035. #endif
  3036. return(xmlStrdup(BAD_CAST "#document"));
  3037. case XML_DOCUMENT_FRAG_NODE:
  3038. return(xmlStrdup(BAD_CAST "#document-fragment"));
  3039. case XML_NOTATION_NODE:
  3040. return(xmlStrdup(node->name));
  3041. case XML_DOCUMENT_TYPE_NODE:
  3042. case XML_DTD_NODE:
  3043. return(xmlStrdup(node->name));
  3044. case XML_NAMESPACE_DECL: {
  3045. xmlNsPtr ns = (xmlNsPtr) node;
  3046. ret = xmlStrdup(BAD_CAST "xmlns");
  3047. if (ns->prefix == NULL)
  3048. return(ret);
  3049. ret = xmlStrcat(ret, BAD_CAST ":");
  3050. ret = xmlStrcat(ret, ns->prefix);
  3051. return(ret);
  3052. }
  3053. case XML_ELEMENT_DECL:
  3054. case XML_ATTRIBUTE_DECL:
  3055. case XML_ENTITY_DECL:
  3056. case XML_XINCLUDE_START:
  3057. case XML_XINCLUDE_END:
  3058. return(NULL);
  3059. }
  3060. return(NULL);
  3061. }
  3062. /**
  3063. * xmlTextReaderConstName:
  3064. * @reader: the xmlTextReaderPtr used
  3065. *
  3066. * The qualified name of the node, equal to Prefix :LocalName.
  3067. *
  3068. * Returns the local name or NULL if not available, the string is
  3069. * deallocated with the reader.
  3070. */
  3071. const xmlChar *
  3072. xmlTextReaderConstName(xmlTextReaderPtr reader) {
  3073. xmlNodePtr node;
  3074. if ((reader == NULL) || (reader->node == NULL))
  3075. return(NULL);
  3076. if (reader->curnode != NULL)
  3077. node = reader->curnode;
  3078. else
  3079. node = reader->node;
  3080. switch (node->type) {
  3081. case XML_ELEMENT_NODE:
  3082. case XML_ATTRIBUTE_NODE:
  3083. if ((node->ns == NULL) ||
  3084. (node->ns->prefix == NULL))
  3085. return(node->name);
  3086. return(CONSTQSTR(node->ns->prefix, node->name));
  3087. case XML_TEXT_NODE:
  3088. return(CONSTSTR(BAD_CAST "#text"));
  3089. case XML_CDATA_SECTION_NODE:
  3090. return(CONSTSTR(BAD_CAST "#cdata-section"));
  3091. case XML_ENTITY_NODE:
  3092. case XML_ENTITY_REF_NODE:
  3093. return(CONSTSTR(node->name));
  3094. case XML_PI_NODE:
  3095. return(CONSTSTR(node->name));
  3096. case XML_COMMENT_NODE:
  3097. return(CONSTSTR(BAD_CAST "#comment"));
  3098. case XML_DOCUMENT_NODE:
  3099. case XML_HTML_DOCUMENT_NODE:
  3100. #ifdef LIBXML_DOCB_ENABLED
  3101. case XML_DOCB_DOCUMENT_NODE:
  3102. #endif
  3103. return(CONSTSTR(BAD_CAST "#document"));
  3104. case XML_DOCUMENT_FRAG_NODE:
  3105. return(CONSTSTR(BAD_CAST "#document-fragment"));
  3106. case XML_NOTATION_NODE:
  3107. return(CONSTSTR(node->name));
  3108. case XML_DOCUMENT_TYPE_NODE:
  3109. case XML_DTD_NODE:
  3110. return(CONSTSTR(node->name));
  3111. case XML_NAMESPACE_DECL: {
  3112. xmlNsPtr ns = (xmlNsPtr) node;
  3113. if (ns->prefix == NULL)
  3114. return(CONSTSTR(BAD_CAST "xmlns"));
  3115. return(CONSTQSTR(BAD_CAST "xmlns", ns->prefix));
  3116. }
  3117. case XML_ELEMENT_DECL:
  3118. case XML_ATTRIBUTE_DECL:
  3119. case XML_ENTITY_DECL:
  3120. case XML_XINCLUDE_START:
  3121. case XML_XINCLUDE_END:
  3122. return(NULL);
  3123. }
  3124. return(NULL);
  3125. }
  3126. /**
  3127. * xmlTextReaderPrefix:
  3128. * @reader: the xmlTextReaderPtr used
  3129. *
  3130. * A shorthand reference to the namespace associated with the node.
  3131. *
  3132. * Returns the prefix or NULL if not available,
  3133. * if non NULL it need to be freed by the caller.
  3134. */
  3135. xmlChar *
  3136. xmlTextReaderPrefix(xmlTextReaderPtr reader) {
  3137. xmlNodePtr node;
  3138. if ((reader == NULL) || (reader->node == NULL))
  3139. return(NULL);
  3140. if (reader->curnode != NULL)
  3141. node = reader->curnode;
  3142. else
  3143. node = reader->node;
  3144. if (node->type == XML_NAMESPACE_DECL) {
  3145. xmlNsPtr ns = (xmlNsPtr) node;
  3146. if (ns->prefix == NULL)
  3147. return(NULL);
  3148. return(xmlStrdup(BAD_CAST "xmlns"));
  3149. }
  3150. if ((node->type != XML_ELEMENT_NODE) &&
  3151. (node->type != XML_ATTRIBUTE_NODE))
  3152. return(NULL);
  3153. if ((node->ns != NULL) && (node->ns->prefix != NULL))
  3154. return(xmlStrdup(node->ns->prefix));
  3155. return(NULL);
  3156. }
  3157. /**
  3158. * xmlTextReaderConstPrefix:
  3159. * @reader: the xmlTextReaderPtr used
  3160. *
  3161. * A shorthand reference to the namespace associated with the node.
  3162. *
  3163. * Returns the prefix or NULL if not available, the string is deallocated
  3164. * with the reader.
  3165. */
  3166. const xmlChar *
  3167. xmlTextReaderConstPrefix(xmlTextReaderPtr reader) {
  3168. xmlNodePtr node;
  3169. if ((reader == NULL) || (reader->node == NULL))
  3170. return(NULL);
  3171. if (reader->curnode != NULL)
  3172. node = reader->curnode;
  3173. else
  3174. node = reader->node;
  3175. if (node->type == XML_NAMESPACE_DECL) {
  3176. xmlNsPtr ns = (xmlNsPtr) node;
  3177. if (ns->prefix == NULL)
  3178. return(NULL);
  3179. return(CONSTSTR(BAD_CAST "xmlns"));
  3180. }
  3181. if ((node->type != XML_ELEMENT_NODE) &&
  3182. (node->type != XML_ATTRIBUTE_NODE))
  3183. return(NULL);
  3184. if ((node->ns != NULL) && (node->ns->prefix != NULL))
  3185. return(CONSTSTR(node->ns->prefix));
  3186. return(NULL);
  3187. }
  3188. /**
  3189. * xmlTextReaderNamespaceUri:
  3190. * @reader: the xmlTextReaderPtr used
  3191. *
  3192. * The URI defining the namespace associated with the node.
  3193. *
  3194. * Returns the namespace URI or NULL if not available,
  3195. * if non NULL it need to be freed by the caller.
  3196. */
  3197. xmlChar *
  3198. xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
  3199. xmlNodePtr node;
  3200. if ((reader == NULL) || (reader->node == NULL))
  3201. return(NULL);
  3202. if (reader->curnode != NULL)
  3203. node = reader->curnode;
  3204. else
  3205. node = reader->node;
  3206. if (node->type == XML_NAMESPACE_DECL)
  3207. return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
  3208. if ((node->type != XML_ELEMENT_NODE) &&
  3209. (node->type != XML_ATTRIBUTE_NODE))
  3210. return(NULL);
  3211. if (node->ns != NULL)
  3212. return(xmlStrdup(node->ns->href));
  3213. return(NULL);
  3214. }
  3215. /**
  3216. * xmlTextReaderConstNamespaceUri:
  3217. * @reader: the xmlTextReaderPtr used
  3218. *
  3219. * The URI defining the namespace associated with the node.
  3220. *
  3221. * Returns the namespace URI or NULL if not available, the string
  3222. * will be deallocated with the reader
  3223. */
  3224. const xmlChar *
  3225. xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) {
  3226. xmlNodePtr node;
  3227. if ((reader == NULL) || (reader->node == NULL))
  3228. return(NULL);
  3229. if (reader->curnode != NULL)
  3230. node = reader->curnode;
  3231. else
  3232. node = reader->node;
  3233. if (node->type == XML_NAMESPACE_DECL)
  3234. return(CONSTSTR(BAD_CAST "http://www.w3.org/2000/xmlns/"));
  3235. if ((node->type != XML_ELEMENT_NODE) &&
  3236. (node->type != XML_ATTRIBUTE_NODE))
  3237. return(NULL);
  3238. if (node->ns != NULL)
  3239. return(CONSTSTR(node->ns->href));
  3240. return(NULL);
  3241. }
  3242. /**
  3243. * xmlTextReaderBaseUri:
  3244. * @reader: the xmlTextReaderPtr used
  3245. *
  3246. * The base URI of the node.
  3247. *
  3248. * Returns the base URI or NULL if not available,
  3249. * if non NULL it need to be freed by the caller.
  3250. */
  3251. xmlChar *
  3252. xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
  3253. if ((reader == NULL) || (reader->node == NULL))
  3254. return(NULL);
  3255. return(xmlNodeGetBase(NULL, reader->node));
  3256. }
  3257. /**
  3258. * xmlTextReaderConstBaseUri:
  3259. * @reader: the xmlTextReaderPtr used
  3260. *
  3261. * The base URI of the node.
  3262. *
  3263. * Returns the base URI or NULL if not available, the string
  3264. * will be deallocated with the reader
  3265. */
  3266. const xmlChar *
  3267. xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) {
  3268. xmlChar *tmp;
  3269. const xmlChar *ret;
  3270. if ((reader == NULL) || (reader->node == NULL))
  3271. return(NULL);
  3272. tmp = xmlNodeGetBase(NULL, reader->node);
  3273. if (tmp == NULL)
  3274. return(NULL);
  3275. ret = CONSTSTR(tmp);
  3276. xmlFree(tmp);
  3277. return(ret);
  3278. }
  3279. /**
  3280. * xmlTextReaderDepth:
  3281. * @reader: the xmlTextReaderPtr used
  3282. *
  3283. * The depth of the node in the tree.
  3284. *
  3285. * Returns the depth or -1 in case of error
  3286. */
  3287. int
  3288. xmlTextReaderDepth(xmlTextReaderPtr reader) {
  3289. if (reader == NULL)
  3290. return(-1);
  3291. if (reader->node == NULL)
  3292. return(0);
  3293. if (reader->curnode != NULL) {
  3294. if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
  3295. (reader->curnode->type == XML_NAMESPACE_DECL))
  3296. return(reader->depth + 1);
  3297. return(reader->depth + 2);
  3298. }
  3299. return(reader->depth);
  3300. }
  3301. /**
  3302. * xmlTextReaderHasAttributes:
  3303. * @reader: the xmlTextReaderPtr used
  3304. *
  3305. * Whether the node has attributes.
  3306. *
  3307. * Returns 1 if true, 0 if false, and -1 in case or error
  3308. */
  3309. int
  3310. xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
  3311. xmlNodePtr node;
  3312. if (reader == NULL)
  3313. return(-1);
  3314. if (reader->node == NULL)
  3315. return(0);
  3316. if (reader->curnode != NULL)
  3317. node = reader->curnode;
  3318. else
  3319. node = reader->node;
  3320. if ((node->type == XML_ELEMENT_NODE) &&
  3321. ((node->properties != NULL) || (node->nsDef != NULL)))
  3322. return(1);
  3323. /* TODO: handle the xmlDecl */
  3324. return(0);
  3325. }
  3326. /**
  3327. * xmlTextReaderHasValue:
  3328. * @reader: the xmlTextReaderPtr used
  3329. *
  3330. * Whether the node can have a text value.
  3331. *
  3332. * Returns 1 if true, 0 if false, and -1 in case or error
  3333. */
  3334. int
  3335. xmlTextReaderHasValue(xmlTextReaderPtr reader) {
  3336. xmlNodePtr node;
  3337. if (reader == NULL)
  3338. return(-1);
  3339. if (reader->node == NULL)
  3340. return(0);
  3341. if (reader->curnode != NULL)
  3342. node = reader->curnode;
  3343. else
  3344. node = reader->node;
  3345. switch (node->type) {
  3346. case XML_ATTRIBUTE_NODE:
  3347. case XML_TEXT_NODE:
  3348. case XML_CDATA_SECTION_NODE:
  3349. case XML_PI_NODE:
  3350. case XML_COMMENT_NODE:
  3351. case XML_NAMESPACE_DECL:
  3352. return(1);
  3353. default:
  3354. break;
  3355. }
  3356. return(0);
  3357. }
  3358. /**
  3359. * xmlTextReaderValue:
  3360. * @reader: the xmlTextReaderPtr used
  3361. *
  3362. * Provides the text value of the node if present
  3363. *
  3364. * Returns the string or NULL if not available. The result must be deallocated
  3365. * with xmlFree()
  3366. */
  3367. xmlChar *
  3368. xmlTextReaderValue(xmlTextReaderPtr reader) {
  3369. xmlNodePtr node;
  3370. if (reader == NULL)
  3371. return(NULL);
  3372. if (reader->node == NULL)
  3373. return(NULL);
  3374. if (reader->curnode != NULL)
  3375. node = reader->curnode;
  3376. else
  3377. node = reader->node;
  3378. switch (node->type) {
  3379. case XML_NAMESPACE_DECL:
  3380. return(xmlStrdup(((xmlNsPtr) node)->href));
  3381. case XML_ATTRIBUTE_NODE:{
  3382. xmlAttrPtr attr = (xmlAttrPtr) node;
  3383. if (attr->parent != NULL)
  3384. return (xmlNodeListGetString
  3385. (attr->parent->doc, attr->children, 1));
  3386. else
  3387. return (xmlNodeListGetString(NULL, attr->children, 1));
  3388. break;
  3389. }
  3390. case XML_TEXT_NODE:
  3391. case XML_CDATA_SECTION_NODE:
  3392. case XML_PI_NODE:
  3393. case XML_COMMENT_NODE:
  3394. if (node->content != NULL)
  3395. return (xmlStrdup(node->content));
  3396. default:
  3397. break;
  3398. }
  3399. return(NULL);
  3400. }
  3401. /**
  3402. * xmlTextReaderConstValue:
  3403. * @reader: the xmlTextReaderPtr used
  3404. *
  3405. * Provides the text value of the node if present
  3406. *
  3407. * Returns the string or NULL if not available. The result will be
  3408. * deallocated on the next Read() operation.
  3409. */
  3410. const xmlChar *
  3411. xmlTextReaderConstValue(xmlTextReaderPtr reader) {
  3412. xmlNodePtr node;
  3413. if (reader == NULL)
  3414. return(NULL);
  3415. if (reader->node == NULL)
  3416. return(NULL);
  3417. if (reader->curnode != NULL)
  3418. node = reader->curnode;
  3419. else
  3420. node = reader->node;
  3421. switch (node->type) {
  3422. case XML_NAMESPACE_DECL:
  3423. return(((xmlNsPtr) node)->href);
  3424. case XML_ATTRIBUTE_NODE:{
  3425. xmlAttrPtr attr = (xmlAttrPtr) node;
  3426. const xmlChar *ret;
  3427. if ((attr->children != NULL) &&
  3428. (attr->children->type == XML_TEXT_NODE) &&
  3429. (attr->children->next == NULL))
  3430. return(attr->children->content);
  3431. else {
  3432. if (reader->buffer == NULL) {
  3433. reader->buffer = xmlBufCreateSize(100);
  3434. if (reader->buffer == NULL) {
  3435. xmlGenericError(xmlGenericErrorContext,
  3436. "xmlTextReaderSetup : malloc failed\n");
  3437. return (NULL);
  3438. }
  3439. xmlBufSetAllocationScheme(reader->buffer,
  3440. XML_BUFFER_ALLOC_BOUNDED);
  3441. } else
  3442. xmlBufEmpty(reader->buffer);
  3443. xmlBufGetNodeContent(reader->buffer, node);
  3444. ret = xmlBufContent(reader->buffer);
  3445. if (ret == NULL) {
  3446. /* error on the buffer best to reallocate */
  3447. xmlBufFree(reader->buffer);
  3448. reader->buffer = xmlBufCreateSize(100);
  3449. xmlBufSetAllocationScheme(reader->buffer,
  3450. XML_BUFFER_ALLOC_BOUNDED);
  3451. ret = BAD_CAST "";
  3452. }
  3453. return(ret);
  3454. }
  3455. break;
  3456. }
  3457. case XML_TEXT_NODE:
  3458. case XML_CDATA_SECTION_NODE:
  3459. case XML_PI_NODE:
  3460. case XML_COMMENT_NODE:
  3461. return(node->content);
  3462. default:
  3463. break;
  3464. }
  3465. return(NULL);
  3466. }
  3467. /**
  3468. * xmlTextReaderIsDefault:
  3469. * @reader: the xmlTextReaderPtr used
  3470. *
  3471. * Whether an Attribute node was generated from the default value
  3472. * defined in the DTD or schema.
  3473. *
  3474. * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
  3475. */
  3476. int
  3477. xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
  3478. if (reader == NULL)
  3479. return(-1);
  3480. return(0);
  3481. }
  3482. /**
  3483. * xmlTextReaderQuoteChar:
  3484. * @reader: the xmlTextReaderPtr used
  3485. *
  3486. * The quotation mark character used to enclose the value of an attribute.
  3487. *
  3488. * Returns " or ' and -1 in case of error
  3489. */
  3490. int
  3491. xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
  3492. if (reader == NULL)
  3493. return(-1);
  3494. /* TODO maybe lookup the attribute value for " first */
  3495. return((int) '"');
  3496. }
  3497. /**
  3498. * xmlTextReaderXmlLang:
  3499. * @reader: the xmlTextReaderPtr used
  3500. *
  3501. * The xml:lang scope within which the node resides.
  3502. *
  3503. * Returns the xml:lang value or NULL if none exists.,
  3504. * if non NULL it need to be freed by the caller.
  3505. */
  3506. xmlChar *
  3507. xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
  3508. if (reader == NULL)
  3509. return(NULL);
  3510. if (reader->node == NULL)
  3511. return(NULL);
  3512. return(xmlNodeGetLang(reader->node));
  3513. }
  3514. /**
  3515. * xmlTextReaderConstXmlLang:
  3516. * @reader: the xmlTextReaderPtr used
  3517. *
  3518. * The xml:lang scope within which the node resides.
  3519. *
  3520. * Returns the xml:lang value or NULL if none exists.
  3521. */
  3522. const xmlChar *
  3523. xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) {
  3524. xmlChar *tmp;
  3525. const xmlChar *ret;
  3526. if (reader == NULL)
  3527. return(NULL);
  3528. if (reader->node == NULL)
  3529. return(NULL);
  3530. tmp = xmlNodeGetLang(reader->node);
  3531. if (tmp == NULL)
  3532. return(NULL);
  3533. ret = CONSTSTR(tmp);
  3534. xmlFree(tmp);
  3535. return(ret);
  3536. }
  3537. /**
  3538. * xmlTextReaderConstString:
  3539. * @reader: the xmlTextReaderPtr used
  3540. * @str: the string to intern.
  3541. *
  3542. * Get an interned string from the reader, allows for example to
  3543. * speedup string name comparisons
  3544. *
  3545. * Returns an interned copy of the string or NULL in case of error. The
  3546. * string will be deallocated with the reader.
  3547. */
  3548. const xmlChar *
  3549. xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) {
  3550. if (reader == NULL)
  3551. return(NULL);
  3552. return(CONSTSTR(str));
  3553. }
  3554. /**
  3555. * xmlTextReaderNormalization:
  3556. * @reader: the xmlTextReaderPtr used
  3557. *
  3558. * The value indicating whether to normalize white space and attribute values.
  3559. * Since attribute value and end of line normalizations are a MUST in the XML
  3560. * specification only the value true is accepted. The broken behaviour of
  3561. * accepting out of range character entities like &#0; is of course not
  3562. * supported either.
  3563. *
  3564. * Returns 1 or -1 in case of error.
  3565. */
  3566. int
  3567. xmlTextReaderNormalization(xmlTextReaderPtr reader) {
  3568. if (reader == NULL)
  3569. return(-1);
  3570. return(1);
  3571. }
  3572. /************************************************************************
  3573. * *
  3574. * Extensions to the base APIs *
  3575. * *
  3576. ************************************************************************/
  3577. /**
  3578. * xmlTextReaderSetParserProp:
  3579. * @reader: the xmlTextReaderPtr used
  3580. * @prop: the xmlParserProperties to set
  3581. * @value: usually 0 or 1 to (de)activate it
  3582. *
  3583. * Change the parser processing behaviour by changing some of its internal
  3584. * properties. Note that some properties can only be changed before any
  3585. * read has been done.
  3586. *
  3587. * Returns 0 if the call was successful, or -1 in case of error
  3588. */
  3589. int
  3590. xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
  3591. xmlParserProperties p = (xmlParserProperties) prop;
  3592. xmlParserCtxtPtr ctxt;
  3593. if ((reader == NULL) || (reader->ctxt == NULL))
  3594. return(-1);
  3595. ctxt = reader->ctxt;
  3596. switch (p) {
  3597. case XML_PARSER_LOADDTD:
  3598. if (value != 0) {
  3599. if (ctxt->loadsubset == 0) {
  3600. if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
  3601. return(-1);
  3602. ctxt->loadsubset = XML_DETECT_IDS;
  3603. }
  3604. } else {
  3605. ctxt->loadsubset = 0;
  3606. }
  3607. return(0);
  3608. case XML_PARSER_DEFAULTATTRS:
  3609. if (value != 0) {
  3610. ctxt->loadsubset |= XML_COMPLETE_ATTRS;
  3611. } else {
  3612. if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
  3613. ctxt->loadsubset -= XML_COMPLETE_ATTRS;
  3614. }
  3615. return(0);
  3616. case XML_PARSER_VALIDATE:
  3617. if (value != 0) {
  3618. ctxt->options |= XML_PARSE_DTDVALID;
  3619. ctxt->validate = 1;
  3620. reader->validate = XML_TEXTREADER_VALIDATE_DTD;
  3621. } else {
  3622. ctxt->options &= ~XML_PARSE_DTDVALID;
  3623. ctxt->validate = 0;
  3624. }
  3625. return(0);
  3626. case XML_PARSER_SUBST_ENTITIES:
  3627. if (value != 0) {
  3628. ctxt->options |= XML_PARSE_NOENT;
  3629. ctxt->replaceEntities = 1;
  3630. } else {
  3631. ctxt->options &= ~XML_PARSE_NOENT;
  3632. ctxt->replaceEntities = 0;
  3633. }
  3634. return(0);
  3635. }
  3636. return(-1);
  3637. }
  3638. /**
  3639. * xmlTextReaderGetParserProp:
  3640. * @reader: the xmlTextReaderPtr used
  3641. * @prop: the xmlParserProperties to get
  3642. *
  3643. * Read the parser internal property.
  3644. *
  3645. * Returns the value, usually 0 or 1, or -1 in case of error.
  3646. */
  3647. int
  3648. xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
  3649. xmlParserProperties p = (xmlParserProperties) prop;
  3650. xmlParserCtxtPtr ctxt;
  3651. if ((reader == NULL) || (reader->ctxt == NULL))
  3652. return(-1);
  3653. ctxt = reader->ctxt;
  3654. switch (p) {
  3655. case XML_PARSER_LOADDTD:
  3656. if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
  3657. return(1);
  3658. return(0);
  3659. case XML_PARSER_DEFAULTATTRS:
  3660. if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
  3661. return(1);
  3662. return(0);
  3663. case XML_PARSER_VALIDATE:
  3664. return(reader->validate);
  3665. case XML_PARSER_SUBST_ENTITIES:
  3666. return(ctxt->replaceEntities);
  3667. }
  3668. return(-1);
  3669. }
  3670. /**
  3671. * xmlTextReaderGetParserLineNumber:
  3672. * @reader: the user data (XML reader context)
  3673. *
  3674. * Provide the line number of the current parsing point.
  3675. *
  3676. * Returns an int or 0 if not available
  3677. */
  3678. int
  3679. xmlTextReaderGetParserLineNumber(xmlTextReaderPtr reader)
  3680. {
  3681. if ((reader == NULL) || (reader->ctxt == NULL) ||
  3682. (reader->ctxt->input == NULL)) {
  3683. return (0);
  3684. }
  3685. return (reader->ctxt->input->line);
  3686. }
  3687. /**
  3688. * xmlTextReaderGetParserColumnNumber:
  3689. * @reader: the user data (XML reader context)
  3690. *
  3691. * Provide the column number of the current parsing point.
  3692. *
  3693. * Returns an int or 0 if not available
  3694. */
  3695. int
  3696. xmlTextReaderGetParserColumnNumber(xmlTextReaderPtr reader)
  3697. {
  3698. if ((reader == NULL) || (reader->ctxt == NULL) ||
  3699. (reader->ctxt->input == NULL)) {
  3700. return (0);
  3701. }
  3702. return (reader->ctxt->input->col);
  3703. }
  3704. /**
  3705. * xmlTextReaderCurrentNode:
  3706. * @reader: the xmlTextReaderPtr used
  3707. *
  3708. * Hacking interface allowing to get the xmlNodePtr corresponding to the
  3709. * current node being accessed by the xmlTextReader. This is dangerous
  3710. * because the underlying node may be destroyed on the next Reads.
  3711. *
  3712. * Returns the xmlNodePtr or NULL in case of error.
  3713. */
  3714. xmlNodePtr
  3715. xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
  3716. if (reader == NULL)
  3717. return(NULL);
  3718. if (reader->curnode != NULL)
  3719. return(reader->curnode);
  3720. return(reader->node);
  3721. }
  3722. /**
  3723. * xmlTextReaderPreserve:
  3724. * @reader: the xmlTextReaderPtr used
  3725. *
  3726. * This tells the XML Reader to preserve the current node.
  3727. * The caller must also use xmlTextReaderCurrentDoc() to
  3728. * keep an handle on the resulting document once parsing has finished
  3729. *
  3730. * Returns the xmlNodePtr or NULL in case of error.
  3731. */
  3732. xmlNodePtr
  3733. xmlTextReaderPreserve(xmlTextReaderPtr reader) {
  3734. xmlNodePtr cur, parent;
  3735. if (reader == NULL)
  3736. return(NULL);
  3737. if (reader->curnode != NULL)
  3738. cur = reader->curnode;
  3739. else
  3740. cur = reader->node;
  3741. if (cur == NULL)
  3742. return(NULL);
  3743. if ((cur->type != XML_DOCUMENT_NODE) && (cur->type != XML_DTD_NODE)) {
  3744. cur->extra |= NODE_IS_PRESERVED;
  3745. cur->extra |= NODE_IS_SPRESERVED;
  3746. }
  3747. reader->preserves++;
  3748. parent = cur->parent;;
  3749. while (parent != NULL) {
  3750. if (parent->type == XML_ELEMENT_NODE)
  3751. parent->extra |= NODE_IS_PRESERVED;
  3752. parent = parent->parent;
  3753. }
  3754. return(cur);
  3755. }
  3756. #ifdef LIBXML_PATTERN_ENABLED
  3757. /**
  3758. * xmlTextReaderPreservePattern:
  3759. * @reader: the xmlTextReaderPtr used
  3760. * @pattern: an XPath subset pattern
  3761. * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
  3762. *
  3763. * This tells the XML Reader to preserve all nodes matched by the
  3764. * pattern. The caller must also use xmlTextReaderCurrentDoc() to
  3765. * keep an handle on the resulting document once parsing has finished
  3766. *
  3767. * Returns a non-negative number in case of success and -1 in case of error
  3768. */
  3769. int
  3770. xmlTextReaderPreservePattern(xmlTextReaderPtr reader, const xmlChar *pattern,
  3771. const xmlChar **namespaces)
  3772. {
  3773. xmlPatternPtr comp;
  3774. if ((reader == NULL) || (pattern == NULL))
  3775. return(-1);
  3776. comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces);
  3777. if (comp == NULL)
  3778. return(-1);
  3779. if (reader->patternMax <= 0) {
  3780. reader->patternMax = 4;
  3781. reader->patternTab = (xmlPatternPtr *) xmlMalloc(reader->patternMax *
  3782. sizeof(reader->patternTab[0]));
  3783. if (reader->patternTab == NULL) {
  3784. xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
  3785. return (-1);
  3786. }
  3787. }
  3788. if (reader->patternNr >= reader->patternMax) {
  3789. xmlPatternPtr *tmp;
  3790. reader->patternMax *= 2;
  3791. tmp = (xmlPatternPtr *) xmlRealloc(reader->patternTab,
  3792. reader->patternMax *
  3793. sizeof(reader->patternTab[0]));
  3794. if (tmp == NULL) {
  3795. xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
  3796. reader->patternMax /= 2;
  3797. return (-1);
  3798. }
  3799. reader->patternTab = tmp;
  3800. }
  3801. reader->patternTab[reader->patternNr] = comp;
  3802. return(reader->patternNr++);
  3803. }
  3804. #endif
  3805. /**
  3806. * xmlTextReaderCurrentDoc:
  3807. * @reader: the xmlTextReaderPtr used
  3808. *
  3809. * Hacking interface allowing to get the xmlDocPtr corresponding to the
  3810. * current document being accessed by the xmlTextReader.
  3811. * NOTE: as a result of this call, the reader will not destroy the
  3812. * associated XML document and calling xmlFreeDoc() on the result
  3813. * is needed once the reader parsing has finished.
  3814. *
  3815. * Returns the xmlDocPtr or NULL in case of error.
  3816. */
  3817. xmlDocPtr
  3818. xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
  3819. if (reader == NULL)
  3820. return(NULL);
  3821. if (reader->doc != NULL)
  3822. return(reader->doc);
  3823. if ((reader->ctxt == NULL) || (reader->ctxt->myDoc == NULL))
  3824. return(NULL);
  3825. reader->preserve = 1;
  3826. return(reader->ctxt->myDoc);
  3827. }
  3828. #ifdef LIBXML_SCHEMAS_ENABLED
  3829. static char *xmlTextReaderBuildMessage(const char *msg, va_list ap) LIBXML_ATTR_FORMAT(1,0);
  3830. static void XMLCDECL
  3831. xmlTextReaderValidityError(void *ctxt, const char *msg, ...) LIBXML_ATTR_FORMAT(2,3);
  3832. static void XMLCDECL
  3833. xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) LIBXML_ATTR_FORMAT(2,3);
  3834. static void XMLCDECL
  3835. xmlTextReaderValidityErrorRelay(void *ctx, const char *msg, ...) LIBXML_ATTR_FORMAT(2,3);
  3836. static void XMLCDECL
  3837. xmlTextReaderValidityWarningRelay(void *ctx, const char *msg, ...) LIBXML_ATTR_FORMAT(2,3);
  3838. static void XMLCDECL
  3839. xmlTextReaderValidityErrorRelay(void *ctx, const char *msg, ...)
  3840. {
  3841. xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx;
  3842. char *str;
  3843. va_list ap;
  3844. va_start(ap, msg);
  3845. str = xmlTextReaderBuildMessage(msg, ap);
  3846. if (!reader->errorFunc) {
  3847. xmlTextReaderValidityError(ctx, "%s", str);
  3848. } else {
  3849. reader->errorFunc(reader->errorFuncArg, str,
  3850. XML_PARSER_SEVERITY_VALIDITY_ERROR,
  3851. NULL /* locator */ );
  3852. }
  3853. if (str != NULL)
  3854. xmlFree(str);
  3855. va_end(ap);
  3856. }
  3857. static void XMLCDECL
  3858. xmlTextReaderValidityWarningRelay(void *ctx, const char *msg, ...)
  3859. {
  3860. xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx;
  3861. char *str;
  3862. va_list ap;
  3863. va_start(ap, msg);
  3864. str = xmlTextReaderBuildMessage(msg, ap);
  3865. if (!reader->errorFunc) {
  3866. xmlTextReaderValidityWarning(ctx, "%s", str);
  3867. } else {
  3868. reader->errorFunc(reader->errorFuncArg, str,
  3869. XML_PARSER_SEVERITY_VALIDITY_WARNING,
  3870. NULL /* locator */ );
  3871. }
  3872. if (str != NULL)
  3873. xmlFree(str);
  3874. va_end(ap);
  3875. }
  3876. static void
  3877. xmlTextReaderStructuredError(void *ctxt, xmlErrorPtr error);
  3878. static void
  3879. xmlTextReaderValidityStructuredRelay(void *userData, xmlErrorPtr error)
  3880. {
  3881. xmlTextReaderPtr reader = (xmlTextReaderPtr) userData;
  3882. if (reader->sErrorFunc) {
  3883. reader->sErrorFunc(reader->errorFuncArg, error);
  3884. } else {
  3885. xmlTextReaderStructuredError(reader, error);
  3886. }
  3887. }
  3888. /**
  3889. * xmlTextReaderRelaxNGSetSchema:
  3890. * @reader: the xmlTextReaderPtr used
  3891. * @schema: a precompiled RelaxNG schema
  3892. *
  3893. * Use RelaxNG to validate the document as it is processed.
  3894. * Activation is only possible before the first Read().
  3895. * if @schema is NULL, then RelaxNG validation is deactivated.
  3896. @ The @schema should not be freed until the reader is deallocated
  3897. * or its use has been deactivated.
  3898. *
  3899. * Returns 0 in case the RelaxNG validation could be (de)activated and
  3900. * -1 in case of error.
  3901. */
  3902. int
  3903. xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
  3904. if (reader == NULL)
  3905. return(-1);
  3906. if (schema == NULL) {
  3907. if (reader->rngSchemas != NULL) {
  3908. xmlRelaxNGFree(reader->rngSchemas);
  3909. reader->rngSchemas = NULL;
  3910. }
  3911. if (reader->rngValidCtxt != NULL) {
  3912. if (! reader->rngPreserveCtxt)
  3913. xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
  3914. reader->rngValidCtxt = NULL;
  3915. }
  3916. reader->rngPreserveCtxt = 0;
  3917. return(0);
  3918. }
  3919. if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
  3920. return(-1);
  3921. if (reader->rngSchemas != NULL) {
  3922. xmlRelaxNGFree(reader->rngSchemas);
  3923. reader->rngSchemas = NULL;
  3924. }
  3925. if (reader->rngValidCtxt != NULL) {
  3926. if (! reader->rngPreserveCtxt)
  3927. xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
  3928. reader->rngValidCtxt = NULL;
  3929. }
  3930. reader->rngPreserveCtxt = 0;
  3931. reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
  3932. if (reader->rngValidCtxt == NULL)
  3933. return(-1);
  3934. if (reader->errorFunc != NULL) {
  3935. xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
  3936. xmlTextReaderValidityErrorRelay,
  3937. xmlTextReaderValidityWarningRelay,
  3938. reader);
  3939. }
  3940. if (reader->sErrorFunc != NULL) {
  3941. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
  3942. xmlTextReaderValidityStructuredRelay,
  3943. reader);
  3944. }
  3945. reader->rngValidErrors = 0;
  3946. reader->rngFullNode = NULL;
  3947. reader->validate = XML_TEXTREADER_VALIDATE_RNG;
  3948. return(0);
  3949. }
  3950. /**
  3951. * xmlTextReaderLocator:
  3952. * @ctx: the xmlTextReaderPtr used
  3953. * @file: returned file information
  3954. * @line: returned line information
  3955. *
  3956. * Internal locator function for the readers
  3957. *
  3958. * Returns 0 in case the Schema validation could be (de)activated and
  3959. * -1 in case of error.
  3960. */
  3961. static int
  3962. xmlTextReaderLocator(void *ctx, const char **file, unsigned long *line) {
  3963. xmlTextReaderPtr reader;
  3964. if ((ctx == NULL) || ((file == NULL) && (line == NULL)))
  3965. return(-1);
  3966. if (file != NULL)
  3967. *file = NULL;
  3968. if (line != NULL)
  3969. *line = 0;
  3970. reader = (xmlTextReaderPtr) ctx;
  3971. if ((reader->ctxt != NULL) && (reader->ctxt->input != NULL)) {
  3972. if (file != NULL)
  3973. *file = reader->ctxt->input->filename;
  3974. if (line != NULL)
  3975. *line = reader->ctxt->input->line;
  3976. return(0);
  3977. }
  3978. if (reader->node != NULL) {
  3979. long res;
  3980. int ret = 0;
  3981. if (line != NULL) {
  3982. res = xmlGetLineNo(reader->node);
  3983. if (res > 0)
  3984. *line = (unsigned long) res;
  3985. else
  3986. ret = -1;
  3987. }
  3988. if (file != NULL) {
  3989. xmlDocPtr doc = reader->node->doc;
  3990. if ((doc != NULL) && (doc->URL != NULL))
  3991. *file = (const char *) doc->URL;
  3992. else
  3993. ret = -1;
  3994. }
  3995. return(ret);
  3996. }
  3997. return(-1);
  3998. }
  3999. /**
  4000. * xmlTextReaderSetSchema:
  4001. * @reader: the xmlTextReaderPtr used
  4002. * @schema: a precompiled Schema schema
  4003. *
  4004. * Use XSD Schema to validate the document as it is processed.
  4005. * Activation is only possible before the first Read().
  4006. * if @schema is NULL, then Schema validation is deactivated.
  4007. * The @schema should not be freed until the reader is deallocated
  4008. * or its use has been deactivated.
  4009. *
  4010. * Returns 0 in case the Schema validation could be (de)activated and
  4011. * -1 in case of error.
  4012. */
  4013. int
  4014. xmlTextReaderSetSchema(xmlTextReaderPtr reader, xmlSchemaPtr schema) {
  4015. if (reader == NULL)
  4016. return(-1);
  4017. if (schema == NULL) {
  4018. if (reader->xsdPlug != NULL) {
  4019. xmlSchemaSAXUnplug(reader->xsdPlug);
  4020. reader->xsdPlug = NULL;
  4021. }
  4022. if (reader->xsdValidCtxt != NULL) {
  4023. if (! reader->xsdPreserveCtxt)
  4024. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  4025. reader->xsdValidCtxt = NULL;
  4026. }
  4027. reader->xsdPreserveCtxt = 0;
  4028. if (reader->xsdSchemas != NULL) {
  4029. xmlSchemaFree(reader->xsdSchemas);
  4030. reader->xsdSchemas = NULL;
  4031. }
  4032. return(0);
  4033. }
  4034. if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
  4035. return(-1);
  4036. if (reader->xsdPlug != NULL) {
  4037. xmlSchemaSAXUnplug(reader->xsdPlug);
  4038. reader->xsdPlug = NULL;
  4039. }
  4040. if (reader->xsdValidCtxt != NULL) {
  4041. if (! reader->xsdPreserveCtxt)
  4042. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  4043. reader->xsdValidCtxt = NULL;
  4044. }
  4045. reader->xsdPreserveCtxt = 0;
  4046. if (reader->xsdSchemas != NULL) {
  4047. xmlSchemaFree(reader->xsdSchemas);
  4048. reader->xsdSchemas = NULL;
  4049. }
  4050. reader->xsdValidCtxt = xmlSchemaNewValidCtxt(schema);
  4051. if (reader->xsdValidCtxt == NULL) {
  4052. xmlSchemaFree(reader->xsdSchemas);
  4053. reader->xsdSchemas = NULL;
  4054. return(-1);
  4055. }
  4056. reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
  4057. &(reader->ctxt->sax),
  4058. &(reader->ctxt->userData));
  4059. if (reader->xsdPlug == NULL) {
  4060. xmlSchemaFree(reader->xsdSchemas);
  4061. reader->xsdSchemas = NULL;
  4062. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  4063. reader->xsdValidCtxt = NULL;
  4064. return(-1);
  4065. }
  4066. xmlSchemaValidateSetLocator(reader->xsdValidCtxt,
  4067. xmlTextReaderLocator,
  4068. (void *) reader);
  4069. if (reader->errorFunc != NULL) {
  4070. xmlSchemaSetValidErrors(reader->xsdValidCtxt,
  4071. xmlTextReaderValidityErrorRelay,
  4072. xmlTextReaderValidityWarningRelay,
  4073. reader);
  4074. }
  4075. if (reader->sErrorFunc != NULL) {
  4076. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
  4077. xmlTextReaderValidityStructuredRelay,
  4078. reader);
  4079. }
  4080. reader->xsdValidErrors = 0;
  4081. reader->validate = XML_TEXTREADER_VALIDATE_XSD;
  4082. return(0);
  4083. }
  4084. /**
  4085. * xmlTextReaderRelaxNGValidateInternal:
  4086. * @reader: the xmlTextReaderPtr used
  4087. * @rng: the path to a RelaxNG schema or NULL
  4088. * @ctxt: the RelaxNG schema validation context or NULL
  4089. * @options: options (not yet used)
  4090. *
  4091. * Use RelaxNG to validate the document as it is processed.
  4092. * Activation is only possible before the first Read().
  4093. * If both @rng and @ctxt are NULL, then RelaxNG validation is deactivated.
  4094. *
  4095. * Returns 0 in case the RelaxNG validation could be (de)activated and
  4096. * -1 in case of error.
  4097. */
  4098. static int
  4099. xmlTextReaderRelaxNGValidateInternal(xmlTextReaderPtr reader,
  4100. const char *rng,
  4101. xmlRelaxNGValidCtxtPtr ctxt,
  4102. int options ATTRIBUTE_UNUSED)
  4103. {
  4104. if (reader == NULL)
  4105. return(-1);
  4106. if ((rng != NULL) && (ctxt != NULL))
  4107. return (-1);
  4108. if (((rng != NULL) || (ctxt != NULL)) &&
  4109. ((reader->mode != XML_TEXTREADER_MODE_INITIAL) ||
  4110. (reader->ctxt == NULL)))
  4111. return(-1);
  4112. /* Cleanup previous validation stuff. */
  4113. if (reader->rngValidCtxt != NULL) {
  4114. if ( !reader->rngPreserveCtxt)
  4115. xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
  4116. reader->rngValidCtxt = NULL;
  4117. }
  4118. reader->rngPreserveCtxt = 0;
  4119. if (reader->rngSchemas != NULL) {
  4120. xmlRelaxNGFree(reader->rngSchemas);
  4121. reader->rngSchemas = NULL;
  4122. }
  4123. if ((rng == NULL) && (ctxt == NULL)) {
  4124. /* We just want to deactivate the validation, so get out. */
  4125. return(0);
  4126. }
  4127. if (rng != NULL) {
  4128. xmlRelaxNGParserCtxtPtr pctxt;
  4129. /* Parse the schema and create validation environment. */
  4130. pctxt = xmlRelaxNGNewParserCtxt(rng);
  4131. if (reader->errorFunc != NULL) {
  4132. xmlRelaxNGSetParserErrors(pctxt,
  4133. xmlTextReaderValidityErrorRelay,
  4134. xmlTextReaderValidityWarningRelay,
  4135. reader);
  4136. }
  4137. if (reader->sErrorFunc != NULL) {
  4138. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
  4139. xmlTextReaderValidityStructuredRelay,
  4140. reader);
  4141. }
  4142. reader->rngSchemas = xmlRelaxNGParse(pctxt);
  4143. xmlRelaxNGFreeParserCtxt(pctxt);
  4144. if (reader->rngSchemas == NULL)
  4145. return(-1);
  4146. reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
  4147. if (reader->rngValidCtxt == NULL) {
  4148. xmlRelaxNGFree(reader->rngSchemas);
  4149. reader->rngSchemas = NULL;
  4150. return(-1);
  4151. }
  4152. } else {
  4153. /* Use the given validation context. */
  4154. reader->rngValidCtxt = ctxt;
  4155. reader->rngPreserveCtxt = 1;
  4156. }
  4157. /*
  4158. * Redirect the validation context's error channels to use
  4159. * the reader channels.
  4160. * TODO: In case the user provides the validation context we
  4161. * could make this redirection optional.
  4162. */
  4163. if (reader->errorFunc != NULL) {
  4164. xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
  4165. xmlTextReaderValidityErrorRelay,
  4166. xmlTextReaderValidityWarningRelay,
  4167. reader);
  4168. }
  4169. if (reader->sErrorFunc != NULL) {
  4170. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
  4171. xmlTextReaderValidityStructuredRelay,
  4172. reader);
  4173. }
  4174. reader->rngValidErrors = 0;
  4175. reader->rngFullNode = NULL;
  4176. reader->validate = XML_TEXTREADER_VALIDATE_RNG;
  4177. return(0);
  4178. }
  4179. /**
  4180. * xmlTextReaderSchemaValidateInternal:
  4181. * @reader: the xmlTextReaderPtr used
  4182. * @xsd: the path to a W3C XSD schema or NULL
  4183. * @ctxt: the XML Schema validation context or NULL
  4184. * @options: options (not used yet)
  4185. *
  4186. * Validate the document as it is processed using XML Schema.
  4187. * Activation is only possible before the first Read().
  4188. * If both @xsd and @ctxt are NULL then XML Schema validation is deactivated.
  4189. *
  4190. * Returns 0 in case the schemas validation could be (de)activated and
  4191. * -1 in case of error.
  4192. */
  4193. static int
  4194. xmlTextReaderSchemaValidateInternal(xmlTextReaderPtr reader,
  4195. const char *xsd,
  4196. xmlSchemaValidCtxtPtr ctxt,
  4197. int options ATTRIBUTE_UNUSED)
  4198. {
  4199. if (reader == NULL)
  4200. return(-1);
  4201. if ((xsd != NULL) && (ctxt != NULL))
  4202. return(-1);
  4203. if (((xsd != NULL) || (ctxt != NULL)) &&
  4204. ((reader->mode != XML_TEXTREADER_MODE_INITIAL) ||
  4205. (reader->ctxt == NULL)))
  4206. return(-1);
  4207. /* Cleanup previous validation stuff. */
  4208. if (reader->xsdPlug != NULL) {
  4209. xmlSchemaSAXUnplug(reader->xsdPlug);
  4210. reader->xsdPlug = NULL;
  4211. }
  4212. if (reader->xsdValidCtxt != NULL) {
  4213. if (! reader->xsdPreserveCtxt)
  4214. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  4215. reader->xsdValidCtxt = NULL;
  4216. }
  4217. reader->xsdPreserveCtxt = 0;
  4218. if (reader->xsdSchemas != NULL) {
  4219. xmlSchemaFree(reader->xsdSchemas);
  4220. reader->xsdSchemas = NULL;
  4221. }
  4222. if ((xsd == NULL) && (ctxt == NULL)) {
  4223. /* We just want to deactivate the validation, so get out. */
  4224. return(0);
  4225. }
  4226. if (xsd != NULL) {
  4227. xmlSchemaParserCtxtPtr pctxt;
  4228. /* Parse the schema and create validation environment. */
  4229. pctxt = xmlSchemaNewParserCtxt(xsd);
  4230. if (reader->errorFunc != NULL) {
  4231. xmlSchemaSetParserErrors(pctxt,
  4232. xmlTextReaderValidityErrorRelay,
  4233. xmlTextReaderValidityWarningRelay,
  4234. reader);
  4235. }
  4236. reader->xsdSchemas = xmlSchemaParse(pctxt);
  4237. xmlSchemaFreeParserCtxt(pctxt);
  4238. if (reader->xsdSchemas == NULL)
  4239. return(-1);
  4240. reader->xsdValidCtxt = xmlSchemaNewValidCtxt(reader->xsdSchemas);
  4241. if (reader->xsdValidCtxt == NULL) {
  4242. xmlSchemaFree(reader->xsdSchemas);
  4243. reader->xsdSchemas = NULL;
  4244. return(-1);
  4245. }
  4246. reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
  4247. &(reader->ctxt->sax),
  4248. &(reader->ctxt->userData));
  4249. if (reader->xsdPlug == NULL) {
  4250. xmlSchemaFree(reader->xsdSchemas);
  4251. reader->xsdSchemas = NULL;
  4252. xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
  4253. reader->xsdValidCtxt = NULL;
  4254. return(-1);
  4255. }
  4256. } else {
  4257. /* Use the given validation context. */
  4258. reader->xsdValidCtxt = ctxt;
  4259. reader->xsdPreserveCtxt = 1;
  4260. reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
  4261. &(reader->ctxt->sax),
  4262. &(reader->ctxt->userData));
  4263. if (reader->xsdPlug == NULL) {
  4264. reader->xsdValidCtxt = NULL;
  4265. reader->xsdPreserveCtxt = 0;
  4266. return(-1);
  4267. }
  4268. }
  4269. xmlSchemaValidateSetLocator(reader->xsdValidCtxt,
  4270. xmlTextReaderLocator,
  4271. (void *) reader);
  4272. /*
  4273. * Redirect the validation context's error channels to use
  4274. * the reader channels.
  4275. * TODO: In case the user provides the validation context we
  4276. * could make this redirection optional.
  4277. */
  4278. if (reader->errorFunc != NULL) {
  4279. xmlSchemaSetValidErrors(reader->xsdValidCtxt,
  4280. xmlTextReaderValidityErrorRelay,
  4281. xmlTextReaderValidityWarningRelay,
  4282. reader);
  4283. }
  4284. if (reader->sErrorFunc != NULL) {
  4285. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
  4286. xmlTextReaderValidityStructuredRelay,
  4287. reader);
  4288. }
  4289. reader->xsdValidErrors = 0;
  4290. reader->validate = XML_TEXTREADER_VALIDATE_XSD;
  4291. return(0);
  4292. }
  4293. /**
  4294. * xmlTextReaderSchemaValidateCtxt:
  4295. * @reader: the xmlTextReaderPtr used
  4296. * @ctxt: the XML Schema validation context or NULL
  4297. * @options: options (not used yet)
  4298. *
  4299. * Use W3C XSD schema context to validate the document as it is processed.
  4300. * Activation is only possible before the first Read().
  4301. * If @ctxt is NULL, then XML Schema validation is deactivated.
  4302. *
  4303. * Returns 0 in case the schemas validation could be (de)activated and
  4304. * -1 in case of error.
  4305. */
  4306. int
  4307. xmlTextReaderSchemaValidateCtxt(xmlTextReaderPtr reader,
  4308. xmlSchemaValidCtxtPtr ctxt,
  4309. int options)
  4310. {
  4311. return(xmlTextReaderSchemaValidateInternal(reader, NULL, ctxt, options));
  4312. }
  4313. /**
  4314. * xmlTextReaderSchemaValidate:
  4315. * @reader: the xmlTextReaderPtr used
  4316. * @xsd: the path to a W3C XSD schema or NULL
  4317. *
  4318. * Use W3C XSD schema to validate the document as it is processed.
  4319. * Activation is only possible before the first Read().
  4320. * If @xsd is NULL, then XML Schema validation is deactivated.
  4321. *
  4322. * Returns 0 in case the schemas validation could be (de)activated and
  4323. * -1 in case of error.
  4324. */
  4325. int
  4326. xmlTextReaderSchemaValidate(xmlTextReaderPtr reader, const char *xsd)
  4327. {
  4328. return(xmlTextReaderSchemaValidateInternal(reader, xsd, NULL, 0));
  4329. }
  4330. /**
  4331. * xmlTextReaderRelaxNGValidateCtxt:
  4332. * @reader: the xmlTextReaderPtr used
  4333. * @ctxt: the RelaxNG schema validation context or NULL
  4334. * @options: options (not used yet)
  4335. *
  4336. * Use RelaxNG schema context to validate the document as it is processed.
  4337. * Activation is only possible before the first Read().
  4338. * If @ctxt is NULL, then RelaxNG schema validation is deactivated.
  4339. *
  4340. * Returns 0 in case the schemas validation could be (de)activated and
  4341. * -1 in case of error.
  4342. */
  4343. int
  4344. xmlTextReaderRelaxNGValidateCtxt(xmlTextReaderPtr reader,
  4345. xmlRelaxNGValidCtxtPtr ctxt,
  4346. int options)
  4347. {
  4348. return(xmlTextReaderRelaxNGValidateInternal(reader, NULL, ctxt, options));
  4349. }
  4350. /**
  4351. * xmlTextReaderRelaxNGValidate:
  4352. * @reader: the xmlTextReaderPtr used
  4353. * @rng: the path to a RelaxNG schema or NULL
  4354. *
  4355. * Use RelaxNG schema to validate the document as it is processed.
  4356. * Activation is only possible before the first Read().
  4357. * If @rng is NULL, then RelaxNG schema validation is deactivated.
  4358. *
  4359. * Returns 0 in case the schemas validation could be (de)activated and
  4360. * -1 in case of error.
  4361. */
  4362. int
  4363. xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng)
  4364. {
  4365. return(xmlTextReaderRelaxNGValidateInternal(reader, rng, NULL, 0));
  4366. }
  4367. #endif
  4368. /**
  4369. * xmlTextReaderIsNamespaceDecl:
  4370. * @reader: the xmlTextReaderPtr used
  4371. *
  4372. * Determine whether the current node is a namespace declaration
  4373. * rather than a regular attribute.
  4374. *
  4375. * Returns 1 if the current node is a namespace declaration, 0 if it
  4376. * is a regular attribute or other type of node, or -1 in case of
  4377. * error.
  4378. */
  4379. int
  4380. xmlTextReaderIsNamespaceDecl(xmlTextReaderPtr reader) {
  4381. xmlNodePtr node;
  4382. if (reader == NULL)
  4383. return(-1);
  4384. if (reader->node == NULL)
  4385. return(-1);
  4386. if (reader->curnode != NULL)
  4387. node = reader->curnode;
  4388. else
  4389. node = reader->node;
  4390. if (XML_NAMESPACE_DECL == node->type)
  4391. return(1);
  4392. else
  4393. return(0);
  4394. }
  4395. /**
  4396. * xmlTextReaderConstXmlVersion:
  4397. * @reader: the xmlTextReaderPtr used
  4398. *
  4399. * Determine the XML version of the document being read.
  4400. *
  4401. * Returns a string containing the XML version of the document or NULL
  4402. * in case of error. The string is deallocated with the reader.
  4403. */
  4404. const xmlChar *
  4405. xmlTextReaderConstXmlVersion(xmlTextReaderPtr reader) {
  4406. xmlDocPtr doc = NULL;
  4407. if (reader == NULL)
  4408. return(NULL);
  4409. if (reader->doc != NULL)
  4410. doc = reader->doc;
  4411. else if (reader->ctxt != NULL)
  4412. doc = reader->ctxt->myDoc;
  4413. if (doc == NULL)
  4414. return(NULL);
  4415. if (doc->version == NULL)
  4416. return(NULL);
  4417. else
  4418. return(CONSTSTR(doc->version));
  4419. }
  4420. /**
  4421. * xmlTextReaderStandalone:
  4422. * @reader: the xmlTextReaderPtr used
  4423. *
  4424. * Determine the standalone status of the document being read.
  4425. *
  4426. * Returns 1 if the document was declared to be standalone, 0 if it
  4427. * was declared to be not standalone, or -1 if the document did not
  4428. * specify its standalone status or in case of error.
  4429. */
  4430. int
  4431. xmlTextReaderStandalone(xmlTextReaderPtr reader) {
  4432. xmlDocPtr doc = NULL;
  4433. if (reader == NULL)
  4434. return(-1);
  4435. if (reader->doc != NULL)
  4436. doc = reader->doc;
  4437. else if (reader->ctxt != NULL)
  4438. doc = reader->ctxt->myDoc;
  4439. if (doc == NULL)
  4440. return(-1);
  4441. return(doc->standalone);
  4442. }
  4443. /************************************************************************
  4444. * *
  4445. * Error Handling Extensions *
  4446. * *
  4447. ************************************************************************/
  4448. /* helper to build a xmlMalloc'ed string from a format and va_list */
  4449. static char *
  4450. xmlTextReaderBuildMessage(const char *msg, va_list ap) {
  4451. int size = 0;
  4452. int chars;
  4453. char *larger;
  4454. char *str = NULL;
  4455. va_list aq;
  4456. while (1) {
  4457. VA_COPY(aq, ap);
  4458. chars = vsnprintf(str, size, msg, aq);
  4459. va_end(aq);
  4460. if (chars < 0) {
  4461. xmlGenericError(xmlGenericErrorContext, "vsnprintf failed !\n");
  4462. if (str)
  4463. xmlFree(str);
  4464. return NULL;
  4465. }
  4466. if ((chars < size) || (size == MAX_ERR_MSG_SIZE))
  4467. break;
  4468. if (chars < MAX_ERR_MSG_SIZE)
  4469. size = chars + 1;
  4470. else
  4471. size = MAX_ERR_MSG_SIZE;
  4472. if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
  4473. xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
  4474. if (str)
  4475. xmlFree(str);
  4476. return NULL;
  4477. }
  4478. str = larger;
  4479. }
  4480. return str;
  4481. }
  4482. /**
  4483. * xmlTextReaderLocatorLineNumber:
  4484. * @locator: the xmlTextReaderLocatorPtr used
  4485. *
  4486. * Obtain the line number for the given locator.
  4487. *
  4488. * Returns the line number or -1 in case of error.
  4489. */
  4490. int
  4491. xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
  4492. /* we know that locator is a xmlParserCtxtPtr */
  4493. xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
  4494. int ret = -1;
  4495. if (locator == NULL)
  4496. return(-1);
  4497. if (ctx->node != NULL) {
  4498. ret = xmlGetLineNo(ctx->node);
  4499. }
  4500. else {
  4501. /* inspired from error.c */
  4502. xmlParserInputPtr input;
  4503. input = ctx->input;
  4504. if ((input->filename == NULL) && (ctx->inputNr > 1))
  4505. input = ctx->inputTab[ctx->inputNr - 2];
  4506. if (input != NULL) {
  4507. ret = input->line;
  4508. }
  4509. else {
  4510. ret = -1;
  4511. }
  4512. }
  4513. return ret;
  4514. }
  4515. /**
  4516. * xmlTextReaderLocatorBaseURI:
  4517. * @locator: the xmlTextReaderLocatorPtr used
  4518. *
  4519. * Obtain the base URI for the given locator.
  4520. *
  4521. * Returns the base URI or NULL in case of error,
  4522. * if non NULL it need to be freed by the caller.
  4523. */
  4524. xmlChar *
  4525. xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
  4526. /* we know that locator is a xmlParserCtxtPtr */
  4527. xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
  4528. xmlChar *ret = NULL;
  4529. if (locator == NULL)
  4530. return(NULL);
  4531. if (ctx->node != NULL) {
  4532. ret = xmlNodeGetBase(NULL,ctx->node);
  4533. }
  4534. else {
  4535. /* inspired from error.c */
  4536. xmlParserInputPtr input;
  4537. input = ctx->input;
  4538. if ((input->filename == NULL) && (ctx->inputNr > 1))
  4539. input = ctx->inputTab[ctx->inputNr - 2];
  4540. if (input != NULL) {
  4541. ret = xmlStrdup(BAD_CAST input->filename);
  4542. }
  4543. else {
  4544. ret = NULL;
  4545. }
  4546. }
  4547. return ret;
  4548. }
  4549. static void
  4550. xmlTextReaderGenericError(void *ctxt, xmlParserSeverities severity,
  4551. char *str)
  4552. {
  4553. xmlParserCtxtPtr ctx = (xmlParserCtxtPtr) ctxt;
  4554. xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx->_private;
  4555. if (str != NULL) {
  4556. if (reader->errorFunc)
  4557. reader->errorFunc(reader->errorFuncArg, str, severity,
  4558. (xmlTextReaderLocatorPtr) ctx);
  4559. xmlFree(str);
  4560. }
  4561. }
  4562. static void
  4563. xmlTextReaderStructuredError(void *ctxt, xmlErrorPtr error)
  4564. {
  4565. xmlParserCtxtPtr ctx = (xmlParserCtxtPtr) ctxt;
  4566. xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx->_private;
  4567. if (error && reader->sErrorFunc) {
  4568. reader->sErrorFunc(reader->errorFuncArg, (xmlErrorPtr) error);
  4569. }
  4570. }
  4571. static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
  4572. xmlTextReaderError(void *ctxt, const char *msg, ...)
  4573. {
  4574. va_list ap;
  4575. va_start(ap, msg);
  4576. xmlTextReaderGenericError(ctxt,
  4577. XML_PARSER_SEVERITY_ERROR,
  4578. xmlTextReaderBuildMessage(msg, ap));
  4579. va_end(ap);
  4580. }
  4581. static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
  4582. xmlTextReaderWarning(void *ctxt, const char *msg, ...)
  4583. {
  4584. va_list ap;
  4585. va_start(ap, msg);
  4586. xmlTextReaderGenericError(ctxt,
  4587. XML_PARSER_SEVERITY_WARNING,
  4588. xmlTextReaderBuildMessage(msg, ap));
  4589. va_end(ap);
  4590. }
  4591. static void XMLCDECL
  4592. xmlTextReaderValidityError(void *ctxt, const char *msg, ...)
  4593. {
  4594. va_list ap;
  4595. int len = xmlStrlen((const xmlChar *) msg);
  4596. if ((len > 1) && (msg[len - 2] != ':')) {
  4597. /*
  4598. * some callbacks only report locator information:
  4599. * skip them (mimicking behaviour in error.c)
  4600. */
  4601. va_start(ap, msg);
  4602. xmlTextReaderGenericError(ctxt,
  4603. XML_PARSER_SEVERITY_VALIDITY_ERROR,
  4604. xmlTextReaderBuildMessage(msg, ap));
  4605. va_end(ap);
  4606. }
  4607. }
  4608. static void XMLCDECL
  4609. xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...)
  4610. {
  4611. va_list ap;
  4612. int len = xmlStrlen((const xmlChar *) msg);
  4613. if ((len != 0) && (msg[len - 1] != ':')) {
  4614. /*
  4615. * some callbacks only report locator information:
  4616. * skip them (mimicking behaviour in error.c)
  4617. */
  4618. va_start(ap, msg);
  4619. xmlTextReaderGenericError(ctxt,
  4620. XML_PARSER_SEVERITY_VALIDITY_WARNING,
  4621. xmlTextReaderBuildMessage(msg, ap));
  4622. va_end(ap);
  4623. }
  4624. }
  4625. /**
  4626. * xmlTextReaderSetErrorHandler:
  4627. * @reader: the xmlTextReaderPtr used
  4628. * @f: the callback function to call on error and warnings
  4629. * @arg: a user argument to pass to the callback function
  4630. *
  4631. * Register a callback function that will be called on error and warnings.
  4632. *
  4633. * If @f is NULL, the default error and warning handlers are restored.
  4634. */
  4635. void
  4636. xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
  4637. xmlTextReaderErrorFunc f, void *arg)
  4638. {
  4639. if (f != NULL) {
  4640. reader->ctxt->sax->error = xmlTextReaderError;
  4641. reader->ctxt->sax->serror = NULL;
  4642. reader->ctxt->vctxt.error = xmlTextReaderValidityError;
  4643. reader->ctxt->sax->warning = xmlTextReaderWarning;
  4644. reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
  4645. reader->errorFunc = f;
  4646. reader->sErrorFunc = NULL;
  4647. reader->errorFuncArg = arg;
  4648. #ifdef LIBXML_SCHEMAS_ENABLED
  4649. if (reader->rngValidCtxt) {
  4650. xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
  4651. xmlTextReaderValidityErrorRelay,
  4652. xmlTextReaderValidityWarningRelay,
  4653. reader);
  4654. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
  4655. reader);
  4656. }
  4657. if (reader->xsdValidCtxt) {
  4658. xmlSchemaSetValidErrors(reader->xsdValidCtxt,
  4659. xmlTextReaderValidityErrorRelay,
  4660. xmlTextReaderValidityWarningRelay,
  4661. reader);
  4662. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
  4663. reader);
  4664. }
  4665. #endif
  4666. } else {
  4667. /* restore defaults */
  4668. reader->ctxt->sax->error = xmlParserError;
  4669. reader->ctxt->vctxt.error = xmlParserValidityError;
  4670. reader->ctxt->sax->warning = xmlParserWarning;
  4671. reader->ctxt->vctxt.warning = xmlParserValidityWarning;
  4672. reader->errorFunc = NULL;
  4673. reader->sErrorFunc = NULL;
  4674. reader->errorFuncArg = NULL;
  4675. #ifdef LIBXML_SCHEMAS_ENABLED
  4676. if (reader->rngValidCtxt) {
  4677. xmlRelaxNGSetValidErrors(reader->rngValidCtxt, NULL, NULL,
  4678. reader);
  4679. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
  4680. reader);
  4681. }
  4682. if (reader->xsdValidCtxt) {
  4683. xmlSchemaSetValidErrors(reader->xsdValidCtxt, NULL, NULL,
  4684. reader);
  4685. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
  4686. reader);
  4687. }
  4688. #endif
  4689. }
  4690. }
  4691. /**
  4692. * xmlTextReaderSetStructuredErrorHandler:
  4693. * @reader: the xmlTextReaderPtr used
  4694. * @f: the callback function to call on error and warnings
  4695. * @arg: a user argument to pass to the callback function
  4696. *
  4697. * Register a callback function that will be called on error and warnings.
  4698. *
  4699. * If @f is NULL, the default error and warning handlers are restored.
  4700. */
  4701. void
  4702. xmlTextReaderSetStructuredErrorHandler(xmlTextReaderPtr reader,
  4703. xmlStructuredErrorFunc f, void *arg)
  4704. {
  4705. if (f != NULL) {
  4706. reader->ctxt->sax->error = NULL;
  4707. reader->ctxt->sax->serror = xmlTextReaderStructuredError;
  4708. reader->ctxt->vctxt.error = xmlTextReaderValidityError;
  4709. reader->ctxt->sax->warning = xmlTextReaderWarning;
  4710. reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
  4711. reader->sErrorFunc = f;
  4712. reader->errorFunc = NULL;
  4713. reader->errorFuncArg = arg;
  4714. #ifdef LIBXML_SCHEMAS_ENABLED
  4715. if (reader->rngValidCtxt) {
  4716. xmlRelaxNGSetValidErrors(reader->rngValidCtxt, NULL, NULL,
  4717. reader);
  4718. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
  4719. xmlTextReaderValidityStructuredRelay,
  4720. reader);
  4721. }
  4722. if (reader->xsdValidCtxt) {
  4723. xmlSchemaSetValidErrors(reader->xsdValidCtxt, NULL, NULL,
  4724. reader);
  4725. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
  4726. xmlTextReaderValidityStructuredRelay,
  4727. reader);
  4728. }
  4729. #endif
  4730. } else {
  4731. /* restore defaults */
  4732. reader->ctxt->sax->error = xmlParserError;
  4733. reader->ctxt->sax->serror = NULL;
  4734. reader->ctxt->vctxt.error = xmlParserValidityError;
  4735. reader->ctxt->sax->warning = xmlParserWarning;
  4736. reader->ctxt->vctxt.warning = xmlParserValidityWarning;
  4737. reader->errorFunc = NULL;
  4738. reader->sErrorFunc = NULL;
  4739. reader->errorFuncArg = NULL;
  4740. #ifdef LIBXML_SCHEMAS_ENABLED
  4741. if (reader->rngValidCtxt) {
  4742. xmlRelaxNGSetValidErrors(reader->rngValidCtxt, NULL, NULL,
  4743. reader);
  4744. xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
  4745. reader);
  4746. }
  4747. if (reader->xsdValidCtxt) {
  4748. xmlSchemaSetValidErrors(reader->xsdValidCtxt, NULL, NULL,
  4749. reader);
  4750. xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
  4751. reader);
  4752. }
  4753. #endif
  4754. }
  4755. }
  4756. /**
  4757. * xmlTextReaderIsValid:
  4758. * @reader: the xmlTextReaderPtr used
  4759. *
  4760. * Retrieve the validity status from the parser context
  4761. *
  4762. * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
  4763. */
  4764. int
  4765. xmlTextReaderIsValid(xmlTextReaderPtr reader)
  4766. {
  4767. if (reader == NULL)
  4768. return (-1);
  4769. #ifdef LIBXML_SCHEMAS_ENABLED
  4770. if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
  4771. return (reader->rngValidErrors == 0);
  4772. if (reader->validate == XML_TEXTREADER_VALIDATE_XSD)
  4773. return (reader->xsdValidErrors == 0);
  4774. #endif
  4775. if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1))
  4776. return (reader->ctxt->valid);
  4777. return (0);
  4778. }
  4779. /**
  4780. * xmlTextReaderGetErrorHandler:
  4781. * @reader: the xmlTextReaderPtr used
  4782. * @f: the callback function or NULL is no callback has been registered
  4783. * @arg: a user argument
  4784. *
  4785. * Retrieve the error callback function and user argument.
  4786. */
  4787. void
  4788. xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
  4789. xmlTextReaderErrorFunc * f, void **arg)
  4790. {
  4791. if (f != NULL)
  4792. *f = reader->errorFunc;
  4793. if (arg != NULL)
  4794. *arg = reader->errorFuncArg;
  4795. }
  4796. /************************************************************************
  4797. * *
  4798. * New set (2.6.0) of simpler and more flexible APIs *
  4799. * *
  4800. ************************************************************************/
  4801. /**
  4802. * xmlTextReaderSetup:
  4803. * @reader: an XML reader
  4804. * @input: xmlParserInputBufferPtr used to feed the reader, will
  4805. * be destroyed with it.
  4806. * @URL: the base URL to use for the document
  4807. * @encoding: the document encoding, or NULL
  4808. * @options: a combination of xmlParserOption
  4809. *
  4810. * Setup an XML reader with new options
  4811. *
  4812. * Returns 0 in case of success and -1 in case of error.
  4813. */
  4814. int
  4815. xmlTextReaderSetup(xmlTextReaderPtr reader,
  4816. xmlParserInputBufferPtr input, const char *URL,
  4817. const char *encoding, int options)
  4818. {
  4819. if (reader == NULL) {
  4820. if (input != NULL)
  4821. xmlFreeParserInputBuffer(input);
  4822. return (-1);
  4823. }
  4824. /*
  4825. * we force the generation of compact text nodes on the reader
  4826. * since usr applications should never modify the tree
  4827. */
  4828. options |= XML_PARSE_COMPACT;
  4829. reader->doc = NULL;
  4830. reader->entNr = 0;
  4831. reader->parserFlags = options;
  4832. reader->validate = XML_TEXTREADER_NOT_VALIDATE;
  4833. if ((input != NULL) && (reader->input != NULL) &&
  4834. (reader->allocs & XML_TEXTREADER_INPUT)) {
  4835. xmlFreeParserInputBuffer(reader->input);
  4836. reader->input = NULL;
  4837. reader->allocs -= XML_TEXTREADER_INPUT;
  4838. }
  4839. if (input != NULL) {
  4840. reader->input = input;
  4841. reader->allocs |= XML_TEXTREADER_INPUT;
  4842. }
  4843. if (reader->buffer == NULL)
  4844. reader->buffer = xmlBufCreateSize(100);
  4845. if (reader->buffer == NULL) {
  4846. xmlGenericError(xmlGenericErrorContext,
  4847. "xmlTextReaderSetup : malloc failed\n");
  4848. return (-1);
  4849. }
  4850. /* no operation on a reader should require a huge buffer */
  4851. xmlBufSetAllocationScheme(reader->buffer,
  4852. XML_BUFFER_ALLOC_BOUNDED);
  4853. if (reader->sax == NULL)
  4854. reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
  4855. if (reader->sax == NULL) {
  4856. xmlGenericError(xmlGenericErrorContext,
  4857. "xmlTextReaderSetup : malloc failed\n");
  4858. return (-1);
  4859. }
  4860. xmlSAXVersion(reader->sax, 2);
  4861. reader->startElement = reader->sax->startElement;
  4862. reader->sax->startElement = xmlTextReaderStartElement;
  4863. reader->endElement = reader->sax->endElement;
  4864. reader->sax->endElement = xmlTextReaderEndElement;
  4865. #ifdef LIBXML_SAX1_ENABLED
  4866. if (reader->sax->initialized == XML_SAX2_MAGIC) {
  4867. #endif /* LIBXML_SAX1_ENABLED */
  4868. reader->startElementNs = reader->sax->startElementNs;
  4869. reader->sax->startElementNs = xmlTextReaderStartElementNs;
  4870. reader->endElementNs = reader->sax->endElementNs;
  4871. reader->sax->endElementNs = xmlTextReaderEndElementNs;
  4872. #ifdef LIBXML_SAX1_ENABLED
  4873. } else {
  4874. reader->startElementNs = NULL;
  4875. reader->endElementNs = NULL;
  4876. }
  4877. #endif /* LIBXML_SAX1_ENABLED */
  4878. reader->characters = reader->sax->characters;
  4879. reader->sax->characters = xmlTextReaderCharacters;
  4880. reader->sax->ignorableWhitespace = xmlTextReaderCharacters;
  4881. reader->cdataBlock = reader->sax->cdataBlock;
  4882. reader->sax->cdataBlock = xmlTextReaderCDataBlock;
  4883. reader->mode = XML_TEXTREADER_MODE_INITIAL;
  4884. reader->node = NULL;
  4885. reader->curnode = NULL;
  4886. if (input != NULL) {
  4887. if (xmlBufUse(reader->input->buffer) < 4) {
  4888. xmlParserInputBufferRead(input, 4);
  4889. }
  4890. if (reader->ctxt == NULL) {
  4891. if (xmlBufUse(reader->input->buffer) >= 4) {
  4892. reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
  4893. (const char *) xmlBufContent(reader->input->buffer),
  4894. 4, URL);
  4895. reader->base = 0;
  4896. reader->cur = 4;
  4897. } else {
  4898. reader->ctxt =
  4899. xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL);
  4900. reader->base = 0;
  4901. reader->cur = 0;
  4902. }
  4903. } else {
  4904. xmlParserInputPtr inputStream;
  4905. xmlParserInputBufferPtr buf;
  4906. xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
  4907. xmlCtxtReset(reader->ctxt);
  4908. buf = xmlAllocParserInputBuffer(enc);
  4909. if (buf == NULL) return(-1);
  4910. inputStream = xmlNewInputStream(reader->ctxt);
  4911. if (inputStream == NULL) {
  4912. xmlFreeParserInputBuffer(buf);
  4913. return(-1);
  4914. }
  4915. if (URL == NULL)
  4916. inputStream->filename = NULL;
  4917. else
  4918. inputStream->filename = (char *)
  4919. xmlCanonicPath((const xmlChar *) URL);
  4920. inputStream->buf = buf;
  4921. xmlBufResetInput(buf->buffer, inputStream);
  4922. inputPush(reader->ctxt, inputStream);
  4923. reader->cur = 0;
  4924. }
  4925. if (reader->ctxt == NULL) {
  4926. xmlGenericError(xmlGenericErrorContext,
  4927. "xmlTextReaderSetup : malloc failed\n");
  4928. return (-1);
  4929. }
  4930. }
  4931. if (reader->dict != NULL) {
  4932. if (reader->ctxt->dict != NULL) {
  4933. if (reader->dict != reader->ctxt->dict) {
  4934. xmlDictFree(reader->dict);
  4935. reader->dict = reader->ctxt->dict;
  4936. }
  4937. } else {
  4938. reader->ctxt->dict = reader->dict;
  4939. }
  4940. } else {
  4941. if (reader->ctxt->dict == NULL)
  4942. reader->ctxt->dict = xmlDictCreate();
  4943. reader->dict = reader->ctxt->dict;
  4944. }
  4945. reader->ctxt->_private = reader;
  4946. reader->ctxt->linenumbers = 1;
  4947. reader->ctxt->dictNames = 1;
  4948. /*
  4949. * use the parser dictionary to allocate all elements and attributes names
  4950. */
  4951. reader->ctxt->docdict = 1;
  4952. reader->ctxt->parseMode = XML_PARSE_READER;
  4953. #ifdef LIBXML_XINCLUDE_ENABLED
  4954. if (reader->xincctxt != NULL) {
  4955. xmlXIncludeFreeContext(reader->xincctxt);
  4956. reader->xincctxt = NULL;
  4957. }
  4958. if (options & XML_PARSE_XINCLUDE) {
  4959. reader->xinclude = 1;
  4960. reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1);
  4961. options -= XML_PARSE_XINCLUDE;
  4962. } else
  4963. reader->xinclude = 0;
  4964. reader->in_xinclude = 0;
  4965. #endif
  4966. #ifdef LIBXML_PATTERN_ENABLED
  4967. if (reader->patternTab == NULL) {
  4968. reader->patternNr = 0;
  4969. reader->patternMax = 0;
  4970. }
  4971. while (reader->patternNr > 0) {
  4972. reader->patternNr--;
  4973. if (reader->patternTab[reader->patternNr] != NULL) {
  4974. xmlFreePattern(reader->patternTab[reader->patternNr]);
  4975. reader->patternTab[reader->patternNr] = NULL;
  4976. }
  4977. }
  4978. #endif
  4979. if (options & XML_PARSE_DTDVALID)
  4980. reader->validate = XML_TEXTREADER_VALIDATE_DTD;
  4981. xmlCtxtUseOptions(reader->ctxt, options);
  4982. if (encoding != NULL) {
  4983. xmlCharEncodingHandlerPtr hdlr;
  4984. hdlr = xmlFindCharEncodingHandler(encoding);
  4985. if (hdlr != NULL)
  4986. xmlSwitchToEncoding(reader->ctxt, hdlr);
  4987. }
  4988. if ((URL != NULL) && (reader->ctxt->input != NULL) &&
  4989. (reader->ctxt->input->filename == NULL))
  4990. reader->ctxt->input->filename = (char *)
  4991. xmlStrdup((const xmlChar *) URL);
  4992. reader->doc = NULL;
  4993. return (0);
  4994. }
  4995. /**
  4996. * xmlTextReaderByteConsumed:
  4997. * @reader: an XML reader
  4998. *
  4999. * This function provides the current index of the parser used
  5000. * by the reader, relative to the start of the current entity.
  5001. * This function actually just wraps a call to xmlBytesConsumed()
  5002. * for the parser context associated with the reader.
  5003. * See xmlBytesConsumed() for more information.
  5004. *
  5005. * Returns the index in bytes from the beginning of the entity or -1
  5006. * in case the index could not be computed.
  5007. */
  5008. long
  5009. xmlTextReaderByteConsumed(xmlTextReaderPtr reader) {
  5010. if ((reader == NULL) || (reader->ctxt == NULL))
  5011. return(-1);
  5012. return(xmlByteConsumed(reader->ctxt));
  5013. }
  5014. /**
  5015. * xmlReaderWalker:
  5016. * @doc: a preparsed document
  5017. *
  5018. * Create an xmltextReader for a preparsed document.
  5019. *
  5020. * Returns the new reader or NULL in case of error.
  5021. */
  5022. xmlTextReaderPtr
  5023. xmlReaderWalker(xmlDocPtr doc)
  5024. {
  5025. xmlTextReaderPtr ret;
  5026. if (doc == NULL)
  5027. return(NULL);
  5028. ret = xmlMalloc(sizeof(xmlTextReader));
  5029. if (ret == NULL) {
  5030. xmlGenericError(xmlGenericErrorContext,
  5031. "xmlNewTextReader : malloc failed\n");
  5032. return(NULL);
  5033. }
  5034. memset(ret, 0, sizeof(xmlTextReader));
  5035. ret->entNr = 0;
  5036. ret->input = NULL;
  5037. ret->mode = XML_TEXTREADER_MODE_INITIAL;
  5038. ret->node = NULL;
  5039. ret->curnode = NULL;
  5040. ret->base = 0;
  5041. ret->cur = 0;
  5042. ret->allocs = XML_TEXTREADER_CTXT;
  5043. ret->doc = doc;
  5044. ret->state = XML_TEXTREADER_START;
  5045. ret->dict = xmlDictCreate();
  5046. return(ret);
  5047. }
  5048. /**
  5049. * xmlReaderForDoc:
  5050. * @cur: a pointer to a zero terminated string
  5051. * @URL: the base URL to use for the document
  5052. * @encoding: the document encoding, or NULL
  5053. * @options: a combination of xmlParserOption
  5054. *
  5055. * Create an xmltextReader for an XML in-memory document.
  5056. * The parsing flags @options are a combination of xmlParserOption.
  5057. *
  5058. * Returns the new reader or NULL in case of error.
  5059. */
  5060. xmlTextReaderPtr
  5061. xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding,
  5062. int options)
  5063. {
  5064. int len;
  5065. if (cur == NULL)
  5066. return (NULL);
  5067. len = xmlStrlen(cur);
  5068. return (xmlReaderForMemory
  5069. ((const char *) cur, len, URL, encoding, options));
  5070. }
  5071. /**
  5072. * xmlReaderForFile:
  5073. * @filename: a file or URL
  5074. * @encoding: the document encoding, or NULL
  5075. * @options: a combination of xmlParserOption
  5076. *
  5077. * parse an XML file from the filesystem or the network.
  5078. * The parsing flags @options are a combination of xmlParserOption.
  5079. *
  5080. * Returns the new reader or NULL in case of error.
  5081. */
  5082. xmlTextReaderPtr
  5083. xmlReaderForFile(const char *filename, const char *encoding, int options)
  5084. {
  5085. xmlTextReaderPtr reader;
  5086. reader = xmlNewTextReaderFilename(filename);
  5087. if (reader == NULL)
  5088. return (NULL);
  5089. xmlTextReaderSetup(reader, NULL, NULL, encoding, options);
  5090. return (reader);
  5091. }
  5092. /**
  5093. * xmlReaderForMemory:
  5094. * @buffer: a pointer to a char array
  5095. * @size: the size of the array
  5096. * @URL: the base URL to use for the document
  5097. * @encoding: the document encoding, or NULL
  5098. * @options: a combination of xmlParserOption
  5099. *
  5100. * Create an xmltextReader for an XML in-memory document.
  5101. * The parsing flags @options are a combination of xmlParserOption.
  5102. *
  5103. * Returns the new reader or NULL in case of error.
  5104. */
  5105. xmlTextReaderPtr
  5106. xmlReaderForMemory(const char *buffer, int size, const char *URL,
  5107. const char *encoding, int options)
  5108. {
  5109. xmlTextReaderPtr reader;
  5110. xmlParserInputBufferPtr buf;
  5111. buf = xmlParserInputBufferCreateStatic(buffer, size,
  5112. XML_CHAR_ENCODING_NONE);
  5113. if (buf == NULL) {
  5114. return (NULL);
  5115. }
  5116. reader = xmlNewTextReader(buf, URL);
  5117. if (reader == NULL) {
  5118. xmlFreeParserInputBuffer(buf);
  5119. return (NULL);
  5120. }
  5121. reader->allocs |= XML_TEXTREADER_INPUT;
  5122. xmlTextReaderSetup(reader, NULL, URL, encoding, options);
  5123. return (reader);
  5124. }
  5125. /**
  5126. * xmlReaderForFd:
  5127. * @fd: an open file descriptor
  5128. * @URL: the base URL to use for the document
  5129. * @encoding: the document encoding, or NULL
  5130. * @options: a combination of xmlParserOption
  5131. *
  5132. * Create an xmltextReader for an XML from a file descriptor.
  5133. * The parsing flags @options are a combination of xmlParserOption.
  5134. * NOTE that the file descriptor will not be closed when the
  5135. * reader is closed or reset.
  5136. *
  5137. * Returns the new reader or NULL in case of error.
  5138. */
  5139. xmlTextReaderPtr
  5140. xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
  5141. {
  5142. xmlTextReaderPtr reader;
  5143. xmlParserInputBufferPtr input;
  5144. if (fd < 0)
  5145. return (NULL);
  5146. input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
  5147. if (input == NULL)
  5148. return (NULL);
  5149. input->closecallback = NULL;
  5150. reader = xmlNewTextReader(input, URL);
  5151. if (reader == NULL) {
  5152. xmlFreeParserInputBuffer(input);
  5153. return (NULL);
  5154. }
  5155. reader->allocs |= XML_TEXTREADER_INPUT;
  5156. xmlTextReaderSetup(reader, NULL, URL, encoding, options);
  5157. return (reader);
  5158. }
  5159. /**
  5160. * xmlReaderForIO:
  5161. * @ioread: an I/O read function
  5162. * @ioclose: an I/O close function
  5163. * @ioctx: an I/O handler
  5164. * @URL: the base URL to use for the document
  5165. * @encoding: the document encoding, or NULL
  5166. * @options: a combination of xmlParserOption
  5167. *
  5168. * Create an xmltextReader for an XML document from I/O functions and source.
  5169. * The parsing flags @options are a combination of xmlParserOption.
  5170. *
  5171. * Returns the new reader or NULL in case of error.
  5172. */
  5173. xmlTextReaderPtr
  5174. xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
  5175. void *ioctx, const char *URL, const char *encoding,
  5176. int options)
  5177. {
  5178. xmlTextReaderPtr reader;
  5179. xmlParserInputBufferPtr input;
  5180. if (ioread == NULL)
  5181. return (NULL);
  5182. input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
  5183. XML_CHAR_ENCODING_NONE);
  5184. if (input == NULL) {
  5185. if (ioclose != NULL)
  5186. ioclose(ioctx);
  5187. return (NULL);
  5188. }
  5189. reader = xmlNewTextReader(input, URL);
  5190. if (reader == NULL) {
  5191. xmlFreeParserInputBuffer(input);
  5192. return (NULL);
  5193. }
  5194. reader->allocs |= XML_TEXTREADER_INPUT;
  5195. xmlTextReaderSetup(reader, NULL, URL, encoding, options);
  5196. return (reader);
  5197. }
  5198. /**
  5199. * xmlReaderNewWalker:
  5200. * @reader: an XML reader
  5201. * @doc: a preparsed document
  5202. *
  5203. * Setup an xmltextReader to parse a preparsed XML document.
  5204. * This reuses the existing @reader xmlTextReader.
  5205. *
  5206. * Returns 0 in case of success and -1 in case of error
  5207. */
  5208. int
  5209. xmlReaderNewWalker(xmlTextReaderPtr reader, xmlDocPtr doc)
  5210. {
  5211. if (doc == NULL)
  5212. return (-1);
  5213. if (reader == NULL)
  5214. return (-1);
  5215. if (reader->input != NULL) {
  5216. xmlFreeParserInputBuffer(reader->input);
  5217. }
  5218. if (reader->ctxt != NULL) {
  5219. xmlCtxtReset(reader->ctxt);
  5220. }
  5221. reader->entNr = 0;
  5222. reader->input = NULL;
  5223. reader->mode = XML_TEXTREADER_MODE_INITIAL;
  5224. reader->node = NULL;
  5225. reader->curnode = NULL;
  5226. reader->base = 0;
  5227. reader->cur = 0;
  5228. reader->allocs = XML_TEXTREADER_CTXT;
  5229. reader->doc = doc;
  5230. reader->state = XML_TEXTREADER_START;
  5231. if (reader->dict == NULL) {
  5232. if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL))
  5233. reader->dict = reader->ctxt->dict;
  5234. else
  5235. reader->dict = xmlDictCreate();
  5236. }
  5237. return(0);
  5238. }
  5239. /**
  5240. * xmlReaderNewDoc:
  5241. * @reader: an XML reader
  5242. * @cur: a pointer to a zero terminated string
  5243. * @URL: the base URL to use for the document
  5244. * @encoding: the document encoding, or NULL
  5245. * @options: a combination of xmlParserOption
  5246. *
  5247. * Setup an xmltextReader to parse an XML in-memory document.
  5248. * The parsing flags @options are a combination of xmlParserOption.
  5249. * This reuses the existing @reader xmlTextReader.
  5250. *
  5251. * Returns 0 in case of success and -1 in case of error
  5252. */
  5253. int
  5254. xmlReaderNewDoc(xmlTextReaderPtr reader, const xmlChar * cur,
  5255. const char *URL, const char *encoding, int options)
  5256. {
  5257. int len;
  5258. if (cur == NULL)
  5259. return (-1);
  5260. if (reader == NULL)
  5261. return (-1);
  5262. len = xmlStrlen(cur);
  5263. return (xmlReaderNewMemory(reader, (const char *)cur, len,
  5264. URL, encoding, options));
  5265. }
  5266. /**
  5267. * xmlReaderNewFile:
  5268. * @reader: an XML reader
  5269. * @filename: a file or URL
  5270. * @encoding: the document encoding, or NULL
  5271. * @options: a combination of xmlParserOption
  5272. *
  5273. * parse an XML file from the filesystem or the network.
  5274. * The parsing flags @options are a combination of xmlParserOption.
  5275. * This reuses the existing @reader xmlTextReader.
  5276. *
  5277. * Returns 0 in case of success and -1 in case of error
  5278. */
  5279. int
  5280. xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename,
  5281. const char *encoding, int options)
  5282. {
  5283. xmlParserInputBufferPtr input;
  5284. if (filename == NULL)
  5285. return (-1);
  5286. if (reader == NULL)
  5287. return (-1);
  5288. input =
  5289. xmlParserInputBufferCreateFilename(filename,
  5290. XML_CHAR_ENCODING_NONE);
  5291. if (input == NULL)
  5292. return (-1);
  5293. return (xmlTextReaderSetup(reader, input, filename, encoding, options));
  5294. }
  5295. /**
  5296. * xmlReaderNewMemory:
  5297. * @reader: an XML reader
  5298. * @buffer: a pointer to a char array
  5299. * @size: the size of the array
  5300. * @URL: the base URL to use for the document
  5301. * @encoding: the document encoding, or NULL
  5302. * @options: a combination of xmlParserOption
  5303. *
  5304. * Setup an xmltextReader to parse an XML in-memory document.
  5305. * The parsing flags @options are a combination of xmlParserOption.
  5306. * This reuses the existing @reader xmlTextReader.
  5307. *
  5308. * Returns 0 in case of success and -1 in case of error
  5309. */
  5310. int
  5311. xmlReaderNewMemory(xmlTextReaderPtr reader, const char *buffer, int size,
  5312. const char *URL, const char *encoding, int options)
  5313. {
  5314. xmlParserInputBufferPtr input;
  5315. if (reader == NULL)
  5316. return (-1);
  5317. if (buffer == NULL)
  5318. return (-1);
  5319. input = xmlParserInputBufferCreateStatic(buffer, size,
  5320. XML_CHAR_ENCODING_NONE);
  5321. if (input == NULL) {
  5322. return (-1);
  5323. }
  5324. return (xmlTextReaderSetup(reader, input, URL, encoding, options));
  5325. }
  5326. /**
  5327. * xmlReaderNewFd:
  5328. * @reader: an XML reader
  5329. * @fd: an open file descriptor
  5330. * @URL: the base URL to use for the document
  5331. * @encoding: the document encoding, or NULL
  5332. * @options: a combination of xmlParserOption
  5333. *
  5334. * Setup an xmltextReader to parse an XML from a file descriptor.
  5335. * NOTE that the file descriptor will not be closed when the
  5336. * reader is closed or reset.
  5337. * The parsing flags @options are a combination of xmlParserOption.
  5338. * This reuses the existing @reader xmlTextReader.
  5339. *
  5340. * Returns 0 in case of success and -1 in case of error
  5341. */
  5342. int
  5343. xmlReaderNewFd(xmlTextReaderPtr reader, int fd,
  5344. const char *URL, const char *encoding, int options)
  5345. {
  5346. xmlParserInputBufferPtr input;
  5347. if (fd < 0)
  5348. return (-1);
  5349. if (reader == NULL)
  5350. return (-1);
  5351. input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
  5352. if (input == NULL)
  5353. return (-1);
  5354. input->closecallback = NULL;
  5355. return (xmlTextReaderSetup(reader, input, URL, encoding, options));
  5356. }
  5357. /**
  5358. * xmlReaderNewIO:
  5359. * @reader: an XML reader
  5360. * @ioread: an I/O read function
  5361. * @ioclose: an I/O close function
  5362. * @ioctx: an I/O handler
  5363. * @URL: the base URL to use for the document
  5364. * @encoding: the document encoding, or NULL
  5365. * @options: a combination of xmlParserOption
  5366. *
  5367. * Setup an xmltextReader to parse an XML document from I/O functions
  5368. * and source.
  5369. * The parsing flags @options are a combination of xmlParserOption.
  5370. * This reuses the existing @reader xmlTextReader.
  5371. *
  5372. * Returns 0 in case of success and -1 in case of error
  5373. */
  5374. int
  5375. xmlReaderNewIO(xmlTextReaderPtr reader, xmlInputReadCallback ioread,
  5376. xmlInputCloseCallback ioclose, void *ioctx,
  5377. const char *URL, const char *encoding, int options)
  5378. {
  5379. xmlParserInputBufferPtr input;
  5380. if (ioread == NULL)
  5381. return (-1);
  5382. if (reader == NULL)
  5383. return (-1);
  5384. input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
  5385. XML_CHAR_ENCODING_NONE);
  5386. if (input == NULL) {
  5387. if (ioclose != NULL)
  5388. ioclose(ioctx);
  5389. return (-1);
  5390. }
  5391. return (xmlTextReaderSetup(reader, input, URL, encoding, options));
  5392. }
  5393. /************************************************************************
  5394. * *
  5395. * Utilities *
  5396. * *
  5397. ************************************************************************/
  5398. #ifdef NOT_USED_YET
  5399. /**
  5400. * xmlBase64Decode:
  5401. * @in: the input buffer
  5402. * @inlen: the size of the input (in), the size read from it (out)
  5403. * @to: the output buffer
  5404. * @tolen: the size of the output (in), the size written to (out)
  5405. *
  5406. * Base64 decoder, reads from @in and save in @to
  5407. * TODO: tell jody when this is actually exported
  5408. *
  5409. * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
  5410. * 2 if there wasn't enough space on the output or -1 in case of error.
  5411. */
  5412. static int
  5413. xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
  5414. unsigned char *to, unsigned long *tolen)
  5415. {
  5416. unsigned long incur; /* current index in in[] */
  5417. unsigned long inblk; /* last block index in in[] */
  5418. unsigned long outcur; /* current index in out[] */
  5419. unsigned long inmax; /* size of in[] */
  5420. unsigned long outmax; /* size of out[] */
  5421. unsigned char cur; /* the current value read from in[] */
  5422. unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */
  5423. int nbintmp; /* number of byte in intmp[] */
  5424. int is_ignore; /* cur should be ignored */
  5425. int is_end = 0; /* the end of the base64 was found */
  5426. int retval = 1;
  5427. int i;
  5428. if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
  5429. return (-1);
  5430. incur = 0;
  5431. inblk = 0;
  5432. outcur = 0;
  5433. inmax = *inlen;
  5434. outmax = *tolen;
  5435. nbintmp = 0;
  5436. while (1) {
  5437. if (incur >= inmax)
  5438. break;
  5439. cur = in[incur++];
  5440. is_ignore = 0;
  5441. if ((cur >= 'A') && (cur <= 'Z'))
  5442. cur = cur - 'A';
  5443. else if ((cur >= 'a') && (cur <= 'z'))
  5444. cur = cur - 'a' + 26;
  5445. else if ((cur >= '0') && (cur <= '9'))
  5446. cur = cur - '0' + 52;
  5447. else if (cur == '+')
  5448. cur = 62;
  5449. else if (cur == '/')
  5450. cur = 63;
  5451. else if (cur == '.')
  5452. cur = 0;
  5453. else if (cur == '=') /*no op , end of the base64 stream */
  5454. is_end = 1;
  5455. else {
  5456. is_ignore = 1;
  5457. if (nbintmp == 0)
  5458. inblk = incur;
  5459. }
  5460. if (!is_ignore) {
  5461. int nbouttmp = 3;
  5462. int is_break = 0;
  5463. if (is_end) {
  5464. if (nbintmp == 0)
  5465. break;
  5466. if ((nbintmp == 1) || (nbintmp == 2))
  5467. nbouttmp = 1;
  5468. else
  5469. nbouttmp = 2;
  5470. nbintmp = 3;
  5471. is_break = 1;
  5472. }
  5473. intmp[nbintmp++] = cur;
  5474. /*
  5475. * if intmp is full, push the 4byte sequence as a 3 byte
  5476. * sequence out
  5477. */
  5478. if (nbintmp == 4) {
  5479. nbintmp = 0;
  5480. outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
  5481. outtmp[1] =
  5482. ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
  5483. outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
  5484. if (outcur + 3 >= outmax) {
  5485. retval = 2;
  5486. break;
  5487. }
  5488. for (i = 0; i < nbouttmp; i++)
  5489. to[outcur++] = outtmp[i];
  5490. inblk = incur;
  5491. }
  5492. if (is_break) {
  5493. retval = 0;
  5494. break;
  5495. }
  5496. }
  5497. }
  5498. *tolen = outcur;
  5499. *inlen = inblk;
  5500. return (retval);
  5501. }
  5502. /*
  5503. * Test routine for the xmlBase64Decode function
  5504. */
  5505. #if 0
  5506. int
  5507. main(int argc, char **argv)
  5508. {
  5509. char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
  5510. char output[100];
  5511. char output2[100];
  5512. char output3[100];
  5513. unsigned long inlen = strlen(input);
  5514. unsigned long outlen = 100;
  5515. int ret;
  5516. unsigned long cons, tmp, tmp2, prod;
  5517. /*
  5518. * Direct
  5519. */
  5520. ret = xmlBase64Decode(input, &inlen, output, &outlen);
  5521. output[outlen] = 0;
  5522. printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen,
  5523. outlen, output)indent: Standard input:179: Error:Unmatched #endif
  5524. ;
  5525. /*
  5526. * output chunking
  5527. */
  5528. cons = 0;
  5529. prod = 0;
  5530. while (cons < inlen) {
  5531. tmp = 5;
  5532. tmp2 = inlen - cons;
  5533. printf("%ld %ld\n", cons, prod);
  5534. ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
  5535. cons += tmp2;
  5536. prod += tmp;
  5537. printf("%ld %ld\n", cons, prod);
  5538. }
  5539. output2[outlen] = 0;
  5540. printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons,
  5541. prod, output2);
  5542. /*
  5543. * input chunking
  5544. */
  5545. cons = 0;
  5546. prod = 0;
  5547. while (cons < inlen) {
  5548. tmp = 100 - prod;
  5549. tmp2 = inlen - cons;
  5550. if (tmp2 > 5)
  5551. tmp2 = 5;
  5552. printf("%ld %ld\n", cons, prod);
  5553. ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
  5554. cons += tmp2;
  5555. prod += tmp;
  5556. printf("%ld %ld\n", cons, prod);
  5557. }
  5558. output3[outlen] = 0;
  5559. printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons,
  5560. prod, output3);
  5561. return (0);
  5562. }
  5563. #endif
  5564. #endif /* NOT_USED_YET */
  5565. #define bottom_xmlreader
  5566. #include "elfgcchack.h"
  5567. #endif /* LIBXML_READER_ENABLED */