xmlreader.c 167 KB

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