xmlreader.c 162 KB

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