valid.c 192 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161
  1. /*
  2. * valid.c : part of the code use to do the DTD handling and the validity
  3. * checking
  4. *
  5. * See Copyright for the status of this software.
  6. *
  7. * daniel@veillard.com
  8. */
  9. #define IN_LIBXML
  10. #include "libxml.h"
  11. #include <string.h>
  12. #ifdef HAVE_STDLIB_H
  13. #include <stdlib.h>
  14. #endif
  15. #include <libxml/xmlmemory.h>
  16. #include <libxml/hash.h>
  17. #include <libxml/uri.h>
  18. #include <libxml/valid.h>
  19. #include <libxml/parser.h>
  20. #include <libxml/parserInternals.h>
  21. #include <libxml/xmlerror.h>
  22. #include <libxml/list.h>
  23. #include <libxml/globals.h>
  24. static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
  25. int create);
  26. /* #define DEBUG_VALID_ALGO */
  27. /* #define DEBUG_REGEXP_ALGO */
  28. #define TODO \
  29. xmlGenericError(xmlGenericErrorContext, \
  30. "Unimplemented block at %s:%d\n", \
  31. __FILE__, __LINE__);
  32. #ifdef LIBXML_VALID_ENABLED
  33. static int
  34. xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
  35. const xmlChar *value);
  36. #endif
  37. /************************************************************************
  38. * *
  39. * Error handling routines *
  40. * *
  41. ************************************************************************/
  42. /**
  43. * xmlVErrMemory:
  44. * @ctxt: an XML validation parser context
  45. * @extra: extra information
  46. *
  47. * Handle an out of memory error
  48. */
  49. static void
  50. xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
  51. {
  52. xmlGenericErrorFunc channel = NULL;
  53. xmlParserCtxtPtr pctxt = NULL;
  54. void *data = NULL;
  55. if (ctxt != NULL) {
  56. channel = ctxt->error;
  57. data = ctxt->userData;
  58. /* Use the special values to detect if it is part of a parsing
  59. context */
  60. if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
  61. (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
  62. long delta = (char *) ctxt - (char *) ctxt->userData;
  63. if ((delta > 0) && (delta < 250))
  64. pctxt = ctxt->userData;
  65. }
  66. }
  67. if (extra)
  68. __xmlRaiseError(NULL, channel, data,
  69. pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
  70. XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
  71. "Memory allocation failed : %s\n", extra);
  72. else
  73. __xmlRaiseError(NULL, channel, data,
  74. pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
  75. XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
  76. "Memory allocation failed\n");
  77. }
  78. /**
  79. * xmlErrValid:
  80. * @ctxt: an XML validation parser context
  81. * @error: the error number
  82. * @extra: extra information
  83. *
  84. * Handle a validation error
  85. */
  86. static void LIBXML_ATTR_FORMAT(3,0)
  87. xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
  88. const char *msg, const char *extra)
  89. {
  90. xmlGenericErrorFunc channel = NULL;
  91. xmlParserCtxtPtr pctxt = NULL;
  92. void *data = NULL;
  93. if (ctxt != NULL) {
  94. channel = ctxt->error;
  95. data = ctxt->userData;
  96. /* Use the special values to detect if it is part of a parsing
  97. context */
  98. if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
  99. (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
  100. long delta = (char *) ctxt - (char *) ctxt->userData;
  101. if ((delta > 0) && (delta < 250))
  102. pctxt = ctxt->userData;
  103. }
  104. }
  105. if (extra)
  106. __xmlRaiseError(NULL, channel, data,
  107. pctxt, NULL, XML_FROM_VALID, error,
  108. XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
  109. msg, extra);
  110. else
  111. __xmlRaiseError(NULL, channel, data,
  112. pctxt, NULL, XML_FROM_VALID, error,
  113. XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
  114. "%s", msg);
  115. }
  116. #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  117. /**
  118. * xmlErrValidNode:
  119. * @ctxt: an XML validation parser context
  120. * @node: the node raising the error
  121. * @error: the error number
  122. * @str1: extra information
  123. * @str2: extra information
  124. * @str3: extra information
  125. *
  126. * Handle a validation error, provide contextual information
  127. */
  128. static void LIBXML_ATTR_FORMAT(4,0)
  129. xmlErrValidNode(xmlValidCtxtPtr ctxt,
  130. xmlNodePtr node, xmlParserErrors error,
  131. const char *msg, const xmlChar * str1,
  132. const xmlChar * str2, const xmlChar * str3)
  133. {
  134. xmlStructuredErrorFunc schannel = NULL;
  135. xmlGenericErrorFunc channel = NULL;
  136. xmlParserCtxtPtr pctxt = NULL;
  137. void *data = NULL;
  138. if (ctxt != NULL) {
  139. channel = ctxt->error;
  140. data = ctxt->userData;
  141. /* Use the special values to detect if it is part of a parsing
  142. context */
  143. if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
  144. (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
  145. long delta = (char *) ctxt - (char *) ctxt->userData;
  146. if ((delta > 0) && (delta < 250))
  147. pctxt = ctxt->userData;
  148. }
  149. }
  150. __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
  151. XML_ERR_ERROR, NULL, 0,
  152. (const char *) str1,
  153. (const char *) str2,
  154. (const char *) str3, 0, 0, msg, str1, str2, str3);
  155. }
  156. #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
  157. #ifdef LIBXML_VALID_ENABLED
  158. /**
  159. * xmlErrValidNodeNr:
  160. * @ctxt: an XML validation parser context
  161. * @node: the node raising the error
  162. * @error: the error number
  163. * @str1: extra information
  164. * @int2: extra information
  165. * @str3: extra information
  166. *
  167. * Handle a validation error, provide contextual information
  168. */
  169. static void LIBXML_ATTR_FORMAT(4,0)
  170. xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
  171. xmlNodePtr node, xmlParserErrors error,
  172. const char *msg, const xmlChar * str1,
  173. int int2, const xmlChar * str3)
  174. {
  175. xmlStructuredErrorFunc schannel = NULL;
  176. xmlGenericErrorFunc channel = NULL;
  177. xmlParserCtxtPtr pctxt = NULL;
  178. void *data = NULL;
  179. if (ctxt != NULL) {
  180. channel = ctxt->error;
  181. data = ctxt->userData;
  182. /* Use the special values to detect if it is part of a parsing
  183. context */
  184. if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
  185. (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
  186. long delta = (char *) ctxt - (char *) ctxt->userData;
  187. if ((delta > 0) && (delta < 250))
  188. pctxt = ctxt->userData;
  189. }
  190. }
  191. __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
  192. XML_ERR_ERROR, NULL, 0,
  193. (const char *) str1,
  194. (const char *) str3,
  195. NULL, int2, 0, msg, str1, int2, str3);
  196. }
  197. /**
  198. * xmlErrValidWarning:
  199. * @ctxt: an XML validation parser context
  200. * @node: the node raising the error
  201. * @error: the error number
  202. * @str1: extra information
  203. * @str2: extra information
  204. * @str3: extra information
  205. *
  206. * Handle a validation error, provide contextual information
  207. */
  208. static void LIBXML_ATTR_FORMAT(4,0)
  209. xmlErrValidWarning(xmlValidCtxtPtr ctxt,
  210. xmlNodePtr node, xmlParserErrors error,
  211. const char *msg, const xmlChar * str1,
  212. const xmlChar * str2, const xmlChar * str3)
  213. {
  214. xmlStructuredErrorFunc schannel = NULL;
  215. xmlGenericErrorFunc channel = NULL;
  216. xmlParserCtxtPtr pctxt = NULL;
  217. void *data = NULL;
  218. if (ctxt != NULL) {
  219. channel = ctxt->warning;
  220. data = ctxt->userData;
  221. /* Use the special values to detect if it is part of a parsing
  222. context */
  223. if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
  224. (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
  225. long delta = (char *) ctxt - (char *) ctxt->userData;
  226. if ((delta > 0) && (delta < 250))
  227. pctxt = ctxt->userData;
  228. }
  229. }
  230. __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
  231. XML_ERR_WARNING, NULL, 0,
  232. (const char *) str1,
  233. (const char *) str2,
  234. (const char *) str3, 0, 0, msg, str1, str2, str3);
  235. }
  236. #ifdef LIBXML_REGEXP_ENABLED
  237. /*
  238. * If regexp are enabled we can do continuous validation without the
  239. * need of a tree to validate the content model. this is done in each
  240. * callbacks.
  241. * Each xmlValidState represent the validation state associated to the
  242. * set of nodes currently open from the document root to the current element.
  243. */
  244. typedef struct _xmlValidState {
  245. xmlElementPtr elemDecl; /* pointer to the content model */
  246. xmlNodePtr node; /* pointer to the current node */
  247. xmlRegExecCtxtPtr exec; /* regexp runtime */
  248. } _xmlValidState;
  249. static int
  250. vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
  251. if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
  252. ctxt->vstateMax = 10;
  253. ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
  254. sizeof(ctxt->vstateTab[0]));
  255. if (ctxt->vstateTab == NULL) {
  256. xmlVErrMemory(ctxt, "malloc failed");
  257. return(-1);
  258. }
  259. }
  260. if (ctxt->vstateNr >= ctxt->vstateMax) {
  261. xmlValidState *tmp;
  262. tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
  263. 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
  264. if (tmp == NULL) {
  265. xmlVErrMemory(ctxt, "realloc failed");
  266. return(-1);
  267. }
  268. ctxt->vstateMax *= 2;
  269. ctxt->vstateTab = tmp;
  270. }
  271. ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
  272. ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
  273. ctxt->vstateTab[ctxt->vstateNr].node = node;
  274. if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
  275. if (elemDecl->contModel == NULL)
  276. xmlValidBuildContentModel(ctxt, elemDecl);
  277. if (elemDecl->contModel != NULL) {
  278. ctxt->vstateTab[ctxt->vstateNr].exec =
  279. xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
  280. } else {
  281. ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
  282. xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
  283. XML_ERR_INTERNAL_ERROR,
  284. "Failed to build content model regexp for %s\n",
  285. node->name, NULL, NULL);
  286. }
  287. }
  288. return(ctxt->vstateNr++);
  289. }
  290. static int
  291. vstateVPop(xmlValidCtxtPtr ctxt) {
  292. xmlElementPtr elemDecl;
  293. if (ctxt->vstateNr < 1) return(-1);
  294. ctxt->vstateNr--;
  295. elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
  296. ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
  297. ctxt->vstateTab[ctxt->vstateNr].node = NULL;
  298. if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
  299. xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
  300. }
  301. ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
  302. if (ctxt->vstateNr >= 1)
  303. ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
  304. else
  305. ctxt->vstate = NULL;
  306. return(ctxt->vstateNr);
  307. }
  308. #else /* not LIBXML_REGEXP_ENABLED */
  309. /*
  310. * If regexp are not enabled, it uses a home made algorithm less
  311. * complex and easier to
  312. * debug/maintain than a generic NFA -> DFA state based algo. The
  313. * only restriction is on the deepness of the tree limited by the
  314. * size of the occurs bitfield
  315. *
  316. * this is the content of a saved state for rollbacks
  317. */
  318. #define ROLLBACK_OR 0
  319. #define ROLLBACK_PARENT 1
  320. typedef struct _xmlValidState {
  321. xmlElementContentPtr cont; /* pointer to the content model subtree */
  322. xmlNodePtr node; /* pointer to the current node in the list */
  323. long occurs;/* bitfield for multiple occurrences */
  324. unsigned char depth; /* current depth in the overall tree */
  325. unsigned char state; /* ROLLBACK_XXX */
  326. } _xmlValidState;
  327. #define MAX_RECURSE 25000
  328. #define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
  329. #define CONT ctxt->vstate->cont
  330. #define NODE ctxt->vstate->node
  331. #define DEPTH ctxt->vstate->depth
  332. #define OCCURS ctxt->vstate->occurs
  333. #define STATE ctxt->vstate->state
  334. #define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
  335. #define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
  336. #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
  337. #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
  338. static int
  339. vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
  340. xmlNodePtr node, unsigned char depth, long occurs,
  341. unsigned char state) {
  342. int i = ctxt->vstateNr - 1;
  343. if (ctxt->vstateNr > MAX_RECURSE) {
  344. return(-1);
  345. }
  346. if (ctxt->vstateTab == NULL) {
  347. ctxt->vstateMax = 8;
  348. ctxt->vstateTab = (xmlValidState *) xmlMalloc(
  349. ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
  350. if (ctxt->vstateTab == NULL) {
  351. xmlVErrMemory(ctxt, "malloc failed");
  352. return(-1);
  353. }
  354. }
  355. if (ctxt->vstateNr >= ctxt->vstateMax) {
  356. xmlValidState *tmp;
  357. tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
  358. 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
  359. if (tmp == NULL) {
  360. xmlVErrMemory(ctxt, "malloc failed");
  361. return(-1);
  362. }
  363. ctxt->vstateMax *= 2;
  364. ctxt->vstateTab = tmp;
  365. ctxt->vstate = &ctxt->vstateTab[0];
  366. }
  367. /*
  368. * Don't push on the stack a state already here
  369. */
  370. if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
  371. (ctxt->vstateTab[i].node == node) &&
  372. (ctxt->vstateTab[i].depth == depth) &&
  373. (ctxt->vstateTab[i].occurs == occurs) &&
  374. (ctxt->vstateTab[i].state == state))
  375. return(ctxt->vstateNr);
  376. ctxt->vstateTab[ctxt->vstateNr].cont = cont;
  377. ctxt->vstateTab[ctxt->vstateNr].node = node;
  378. ctxt->vstateTab[ctxt->vstateNr].depth = depth;
  379. ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
  380. ctxt->vstateTab[ctxt->vstateNr].state = state;
  381. return(ctxt->vstateNr++);
  382. }
  383. static int
  384. vstateVPop(xmlValidCtxtPtr ctxt) {
  385. if (ctxt->vstateNr <= 1) return(-1);
  386. ctxt->vstateNr--;
  387. ctxt->vstate = &ctxt->vstateTab[0];
  388. ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
  389. ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
  390. ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
  391. ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
  392. ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
  393. return(ctxt->vstateNr);
  394. }
  395. #endif /* LIBXML_REGEXP_ENABLED */
  396. static int
  397. nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
  398. {
  399. if (ctxt->nodeMax <= 0) {
  400. ctxt->nodeMax = 4;
  401. ctxt->nodeTab =
  402. (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
  403. sizeof(ctxt->nodeTab[0]));
  404. if (ctxt->nodeTab == NULL) {
  405. xmlVErrMemory(ctxt, "malloc failed");
  406. ctxt->nodeMax = 0;
  407. return (0);
  408. }
  409. }
  410. if (ctxt->nodeNr >= ctxt->nodeMax) {
  411. xmlNodePtr *tmp;
  412. tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
  413. ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
  414. if (tmp == NULL) {
  415. xmlVErrMemory(ctxt, "realloc failed");
  416. return (0);
  417. }
  418. ctxt->nodeMax *= 2;
  419. ctxt->nodeTab = tmp;
  420. }
  421. ctxt->nodeTab[ctxt->nodeNr] = value;
  422. ctxt->node = value;
  423. return (ctxt->nodeNr++);
  424. }
  425. static xmlNodePtr
  426. nodeVPop(xmlValidCtxtPtr ctxt)
  427. {
  428. xmlNodePtr ret;
  429. if (ctxt->nodeNr <= 0)
  430. return (NULL);
  431. ctxt->nodeNr--;
  432. if (ctxt->nodeNr > 0)
  433. ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
  434. else
  435. ctxt->node = NULL;
  436. ret = ctxt->nodeTab[ctxt->nodeNr];
  437. ctxt->nodeTab[ctxt->nodeNr] = NULL;
  438. return (ret);
  439. }
  440. /**
  441. * xmlValidNormalizeString:
  442. * @str: a string
  443. *
  444. * Normalize a string in-place.
  445. */
  446. static void
  447. xmlValidNormalizeString(xmlChar *str) {
  448. xmlChar *dst;
  449. const xmlChar *src;
  450. if (str == NULL)
  451. return;
  452. src = str;
  453. dst = str;
  454. while (*src == 0x20) src++;
  455. while (*src != 0) {
  456. if (*src == 0x20) {
  457. while (*src == 0x20) src++;
  458. if (*src != 0)
  459. *dst++ = 0x20;
  460. } else {
  461. *dst++ = *src++;
  462. }
  463. }
  464. *dst = 0;
  465. }
  466. #ifdef DEBUG_VALID_ALGO
  467. static void
  468. xmlValidPrintNode(xmlNodePtr cur) {
  469. if (cur == NULL) {
  470. xmlGenericError(xmlGenericErrorContext, "null");
  471. return;
  472. }
  473. switch (cur->type) {
  474. case XML_ELEMENT_NODE:
  475. xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
  476. break;
  477. case XML_TEXT_NODE:
  478. xmlGenericError(xmlGenericErrorContext, "text ");
  479. break;
  480. case XML_CDATA_SECTION_NODE:
  481. xmlGenericError(xmlGenericErrorContext, "cdata ");
  482. break;
  483. case XML_ENTITY_REF_NODE:
  484. xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
  485. break;
  486. case XML_PI_NODE:
  487. xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
  488. break;
  489. case XML_COMMENT_NODE:
  490. xmlGenericError(xmlGenericErrorContext, "comment ");
  491. break;
  492. case XML_ATTRIBUTE_NODE:
  493. xmlGenericError(xmlGenericErrorContext, "?attr? ");
  494. break;
  495. case XML_ENTITY_NODE:
  496. xmlGenericError(xmlGenericErrorContext, "?ent? ");
  497. break;
  498. case XML_DOCUMENT_NODE:
  499. xmlGenericError(xmlGenericErrorContext, "?doc? ");
  500. break;
  501. case XML_DOCUMENT_TYPE_NODE:
  502. xmlGenericError(xmlGenericErrorContext, "?doctype? ");
  503. break;
  504. case XML_DOCUMENT_FRAG_NODE:
  505. xmlGenericError(xmlGenericErrorContext, "?frag? ");
  506. break;
  507. case XML_NOTATION_NODE:
  508. xmlGenericError(xmlGenericErrorContext, "?nota? ");
  509. break;
  510. case XML_HTML_DOCUMENT_NODE:
  511. xmlGenericError(xmlGenericErrorContext, "?html? ");
  512. break;
  513. #ifdef LIBXML_DOCB_ENABLED
  514. case XML_DOCB_DOCUMENT_NODE:
  515. xmlGenericError(xmlGenericErrorContext, "?docb? ");
  516. break;
  517. #endif
  518. case XML_DTD_NODE:
  519. xmlGenericError(xmlGenericErrorContext, "?dtd? ");
  520. break;
  521. case XML_ELEMENT_DECL:
  522. xmlGenericError(xmlGenericErrorContext, "?edecl? ");
  523. break;
  524. case XML_ATTRIBUTE_DECL:
  525. xmlGenericError(xmlGenericErrorContext, "?adecl? ");
  526. break;
  527. case XML_ENTITY_DECL:
  528. xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
  529. break;
  530. case XML_NAMESPACE_DECL:
  531. xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
  532. break;
  533. case XML_XINCLUDE_START:
  534. xmlGenericError(xmlGenericErrorContext, "incstart ");
  535. break;
  536. case XML_XINCLUDE_END:
  537. xmlGenericError(xmlGenericErrorContext, "incend ");
  538. break;
  539. }
  540. }
  541. static void
  542. xmlValidPrintNodeList(xmlNodePtr cur) {
  543. if (cur == NULL)
  544. xmlGenericError(xmlGenericErrorContext, "null ");
  545. while (cur != NULL) {
  546. xmlValidPrintNode(cur);
  547. cur = cur->next;
  548. }
  549. }
  550. static void
  551. xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
  552. char expr[5000];
  553. expr[0] = 0;
  554. xmlGenericError(xmlGenericErrorContext, "valid: ");
  555. xmlValidPrintNodeList(cur);
  556. xmlGenericError(xmlGenericErrorContext, "against ");
  557. xmlSnprintfElementContent(expr, 5000, cont, 1);
  558. xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
  559. }
  560. static void
  561. xmlValidDebugState(xmlValidStatePtr state) {
  562. xmlGenericError(xmlGenericErrorContext, "(");
  563. if (state->cont == NULL)
  564. xmlGenericError(xmlGenericErrorContext, "null,");
  565. else
  566. switch (state->cont->type) {
  567. case XML_ELEMENT_CONTENT_PCDATA:
  568. xmlGenericError(xmlGenericErrorContext, "pcdata,");
  569. break;
  570. case XML_ELEMENT_CONTENT_ELEMENT:
  571. xmlGenericError(xmlGenericErrorContext, "%s,",
  572. state->cont->name);
  573. break;
  574. case XML_ELEMENT_CONTENT_SEQ:
  575. xmlGenericError(xmlGenericErrorContext, "seq,");
  576. break;
  577. case XML_ELEMENT_CONTENT_OR:
  578. xmlGenericError(xmlGenericErrorContext, "or,");
  579. break;
  580. }
  581. xmlValidPrintNode(state->node);
  582. xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
  583. state->depth, state->occurs, state->state);
  584. }
  585. static void
  586. xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
  587. int i, j;
  588. xmlGenericError(xmlGenericErrorContext, "state: ");
  589. xmlValidDebugState(ctxt->vstate);
  590. xmlGenericError(xmlGenericErrorContext, " stack: %d ",
  591. ctxt->vstateNr - 1);
  592. for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
  593. xmlValidDebugState(&ctxt->vstateTab[j]);
  594. xmlGenericError(xmlGenericErrorContext, "\n");
  595. }
  596. /*****
  597. #define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
  598. *****/
  599. #define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
  600. #define DEBUG_VALID_MSG(m) \
  601. xmlGenericError(xmlGenericErrorContext, "%s\n", m);
  602. #else
  603. #define DEBUG_VALID_STATE(n,c)
  604. #define DEBUG_VALID_MSG(m)
  605. #endif
  606. /* TODO: use hash table for accesses to elem and attribute definitions */
  607. #define CHECK_DTD \
  608. if (doc == NULL) return(0); \
  609. else if ((doc->intSubset == NULL) && \
  610. (doc->extSubset == NULL)) return(0)
  611. #ifdef LIBXML_REGEXP_ENABLED
  612. /************************************************************************
  613. * *
  614. * Content model validation based on the regexps *
  615. * *
  616. ************************************************************************/
  617. /**
  618. * xmlValidBuildAContentModel:
  619. * @content: the content model
  620. * @ctxt: the schema parser context
  621. * @name: the element name whose content is being built
  622. *
  623. * Generate the automata sequence needed for that type
  624. *
  625. * Returns 1 if successful or 0 in case of error.
  626. */
  627. static int
  628. xmlValidBuildAContentModel(xmlElementContentPtr content,
  629. xmlValidCtxtPtr ctxt,
  630. const xmlChar *name) {
  631. if (content == NULL) {
  632. xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
  633. "Found NULL content in content model of %s\n",
  634. name, NULL, NULL);
  635. return(0);
  636. }
  637. switch (content->type) {
  638. case XML_ELEMENT_CONTENT_PCDATA:
  639. xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
  640. "Found PCDATA in content model of %s\n",
  641. name, NULL, NULL);
  642. return(0);
  643. break;
  644. case XML_ELEMENT_CONTENT_ELEMENT: {
  645. xmlAutomataStatePtr oldstate = ctxt->state;
  646. xmlChar fn[50];
  647. xmlChar *fullname;
  648. fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
  649. if (fullname == NULL) {
  650. xmlVErrMemory(ctxt, "Building content model");
  651. return(0);
  652. }
  653. switch (content->ocur) {
  654. case XML_ELEMENT_CONTENT_ONCE:
  655. ctxt->state = xmlAutomataNewTransition(ctxt->am,
  656. ctxt->state, NULL, fullname, NULL);
  657. break;
  658. case XML_ELEMENT_CONTENT_OPT:
  659. ctxt->state = xmlAutomataNewTransition(ctxt->am,
  660. ctxt->state, NULL, fullname, NULL);
  661. xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
  662. break;
  663. case XML_ELEMENT_CONTENT_PLUS:
  664. ctxt->state = xmlAutomataNewTransition(ctxt->am,
  665. ctxt->state, NULL, fullname, NULL);
  666. xmlAutomataNewTransition(ctxt->am, ctxt->state,
  667. ctxt->state, fullname, NULL);
  668. break;
  669. case XML_ELEMENT_CONTENT_MULT:
  670. ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
  671. ctxt->state, NULL);
  672. xmlAutomataNewTransition(ctxt->am,
  673. ctxt->state, ctxt->state, fullname, NULL);
  674. break;
  675. }
  676. if ((fullname != fn) && (fullname != content->name))
  677. xmlFree(fullname);
  678. break;
  679. }
  680. case XML_ELEMENT_CONTENT_SEQ: {
  681. xmlAutomataStatePtr oldstate, oldend;
  682. xmlElementContentOccur ocur;
  683. /*
  684. * Simply iterate over the content
  685. */
  686. oldstate = ctxt->state;
  687. ocur = content->ocur;
  688. if (ocur != XML_ELEMENT_CONTENT_ONCE) {
  689. ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
  690. oldstate = ctxt->state;
  691. }
  692. do {
  693. xmlValidBuildAContentModel(content->c1, ctxt, name);
  694. content = content->c2;
  695. } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
  696. (content->ocur == XML_ELEMENT_CONTENT_ONCE));
  697. xmlValidBuildAContentModel(content, ctxt, name);
  698. oldend = ctxt->state;
  699. ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
  700. switch (ocur) {
  701. case XML_ELEMENT_CONTENT_ONCE:
  702. break;
  703. case XML_ELEMENT_CONTENT_OPT:
  704. xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
  705. break;
  706. case XML_ELEMENT_CONTENT_MULT:
  707. xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
  708. xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
  709. break;
  710. case XML_ELEMENT_CONTENT_PLUS:
  711. xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
  712. break;
  713. }
  714. break;
  715. }
  716. case XML_ELEMENT_CONTENT_OR: {
  717. xmlAutomataStatePtr oldstate, oldend;
  718. xmlElementContentOccur ocur;
  719. ocur = content->ocur;
  720. if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
  721. (ocur == XML_ELEMENT_CONTENT_MULT)) {
  722. ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
  723. ctxt->state, NULL);
  724. }
  725. oldstate = ctxt->state;
  726. oldend = xmlAutomataNewState(ctxt->am);
  727. /*
  728. * iterate over the subtypes and remerge the end with an
  729. * epsilon transition
  730. */
  731. do {
  732. ctxt->state = oldstate;
  733. xmlValidBuildAContentModel(content->c1, ctxt, name);
  734. xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
  735. content = content->c2;
  736. } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
  737. (content->ocur == XML_ELEMENT_CONTENT_ONCE));
  738. ctxt->state = oldstate;
  739. xmlValidBuildAContentModel(content, ctxt, name);
  740. xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
  741. ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
  742. switch (ocur) {
  743. case XML_ELEMENT_CONTENT_ONCE:
  744. break;
  745. case XML_ELEMENT_CONTENT_OPT:
  746. xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
  747. break;
  748. case XML_ELEMENT_CONTENT_MULT:
  749. xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
  750. xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
  751. break;
  752. case XML_ELEMENT_CONTENT_PLUS:
  753. xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
  754. break;
  755. }
  756. break;
  757. }
  758. default:
  759. xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
  760. "ContentModel broken for element %s\n",
  761. (const char *) name);
  762. return(0);
  763. }
  764. return(1);
  765. }
  766. /**
  767. * xmlValidBuildContentModel:
  768. * @ctxt: a validation context
  769. * @elem: an element declaration node
  770. *
  771. * (Re)Build the automata associated to the content model of this
  772. * element
  773. *
  774. * Returns 1 in case of success, 0 in case of error
  775. */
  776. int
  777. xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
  778. if ((ctxt == NULL) || (elem == NULL))
  779. return(0);
  780. if (elem->type != XML_ELEMENT_DECL)
  781. return(0);
  782. if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
  783. return(1);
  784. /* TODO: should we rebuild in this case ? */
  785. if (elem->contModel != NULL) {
  786. if (!xmlRegexpIsDeterminist(elem->contModel)) {
  787. ctxt->valid = 0;
  788. return(0);
  789. }
  790. return(1);
  791. }
  792. ctxt->am = xmlNewAutomata();
  793. if (ctxt->am == NULL) {
  794. xmlErrValidNode(ctxt, (xmlNodePtr) elem,
  795. XML_ERR_INTERNAL_ERROR,
  796. "Cannot create automata for element %s\n",
  797. elem->name, NULL, NULL);
  798. return(0);
  799. }
  800. ctxt->state = xmlAutomataGetInitState(ctxt->am);
  801. xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
  802. xmlAutomataSetFinalState(ctxt->am, ctxt->state);
  803. elem->contModel = xmlAutomataCompile(ctxt->am);
  804. if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
  805. char expr[5000];
  806. expr[0] = 0;
  807. xmlSnprintfElementContent(expr, 5000, elem->content, 1);
  808. xmlErrValidNode(ctxt, (xmlNodePtr) elem,
  809. XML_DTD_CONTENT_NOT_DETERMINIST,
  810. "Content model of %s is not determinist: %s\n",
  811. elem->name, BAD_CAST expr, NULL);
  812. #ifdef DEBUG_REGEXP_ALGO
  813. xmlRegexpPrint(stderr, elem->contModel);
  814. #endif
  815. ctxt->valid = 0;
  816. ctxt->state = NULL;
  817. xmlFreeAutomata(ctxt->am);
  818. ctxt->am = NULL;
  819. return(0);
  820. }
  821. ctxt->state = NULL;
  822. xmlFreeAutomata(ctxt->am);
  823. ctxt->am = NULL;
  824. return(1);
  825. }
  826. #endif /* LIBXML_REGEXP_ENABLED */
  827. /****************************************************************
  828. * *
  829. * Util functions for data allocation/deallocation *
  830. * *
  831. ****************************************************************/
  832. /**
  833. * xmlNewValidCtxt:
  834. *
  835. * Allocate a validation context structure.
  836. *
  837. * Returns NULL if not, otherwise the new validation context structure
  838. */
  839. xmlValidCtxtPtr xmlNewValidCtxt(void) {
  840. xmlValidCtxtPtr ret;
  841. if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
  842. xmlVErrMemory(NULL, "malloc failed");
  843. return (NULL);
  844. }
  845. (void) memset(ret, 0, sizeof (xmlValidCtxt));
  846. return (ret);
  847. }
  848. /**
  849. * xmlFreeValidCtxt:
  850. * @cur: the validation context to free
  851. *
  852. * Free a validation context structure.
  853. */
  854. void
  855. xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
  856. if (cur->vstateTab != NULL)
  857. xmlFree(cur->vstateTab);
  858. if (cur->nodeTab != NULL)
  859. xmlFree(cur->nodeTab);
  860. xmlFree(cur);
  861. }
  862. #endif /* LIBXML_VALID_ENABLED */
  863. /**
  864. * xmlNewDocElementContent:
  865. * @doc: the document
  866. * @name: the subelement name or NULL
  867. * @type: the type of element content decl
  868. *
  869. * Allocate an element content structure for the document.
  870. *
  871. * Returns NULL if not, otherwise the new element content structure
  872. */
  873. xmlElementContentPtr
  874. xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name,
  875. xmlElementContentType type) {
  876. xmlElementContentPtr ret;
  877. xmlDictPtr dict = NULL;
  878. if (doc != NULL)
  879. dict = doc->dict;
  880. switch(type) {
  881. case XML_ELEMENT_CONTENT_ELEMENT:
  882. if (name == NULL) {
  883. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  884. "xmlNewElementContent : name == NULL !\n",
  885. NULL);
  886. }
  887. break;
  888. case XML_ELEMENT_CONTENT_PCDATA:
  889. case XML_ELEMENT_CONTENT_SEQ:
  890. case XML_ELEMENT_CONTENT_OR:
  891. if (name != NULL) {
  892. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  893. "xmlNewElementContent : name != NULL !\n",
  894. NULL);
  895. }
  896. break;
  897. default:
  898. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  899. "Internal: ELEMENT content corrupted invalid type\n",
  900. NULL);
  901. return(NULL);
  902. }
  903. ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
  904. if (ret == NULL) {
  905. xmlVErrMemory(NULL, "malloc failed");
  906. return(NULL);
  907. }
  908. memset(ret, 0, sizeof(xmlElementContent));
  909. ret->type = type;
  910. ret->ocur = XML_ELEMENT_CONTENT_ONCE;
  911. if (name != NULL) {
  912. int l;
  913. const xmlChar *tmp;
  914. tmp = xmlSplitQName3(name, &l);
  915. if (tmp == NULL) {
  916. if (dict == NULL)
  917. ret->name = xmlStrdup(name);
  918. else
  919. ret->name = xmlDictLookup(dict, name, -1);
  920. } else {
  921. if (dict == NULL) {
  922. ret->prefix = xmlStrndup(name, l);
  923. ret->name = xmlStrdup(tmp);
  924. } else {
  925. ret->prefix = xmlDictLookup(dict, name, l);
  926. ret->name = xmlDictLookup(dict, tmp, -1);
  927. }
  928. }
  929. }
  930. return(ret);
  931. }
  932. /**
  933. * xmlNewElementContent:
  934. * @name: the subelement name or NULL
  935. * @type: the type of element content decl
  936. *
  937. * Allocate an element content structure.
  938. * Deprecated in favor of xmlNewDocElementContent
  939. *
  940. * Returns NULL if not, otherwise the new element content structure
  941. */
  942. xmlElementContentPtr
  943. xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
  944. return(xmlNewDocElementContent(NULL, name, type));
  945. }
  946. /**
  947. * xmlCopyDocElementContent:
  948. * @doc: the document owning the element declaration
  949. * @cur: An element content pointer.
  950. *
  951. * Build a copy of an element content description.
  952. *
  953. * Returns the new xmlElementContentPtr or NULL in case of error.
  954. */
  955. xmlElementContentPtr
  956. xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
  957. xmlElementContentPtr ret = NULL, prev = NULL, tmp;
  958. xmlDictPtr dict = NULL;
  959. if (cur == NULL) return(NULL);
  960. if (doc != NULL)
  961. dict = doc->dict;
  962. ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
  963. if (ret == NULL) {
  964. xmlVErrMemory(NULL, "malloc failed");
  965. return(NULL);
  966. }
  967. memset(ret, 0, sizeof(xmlElementContent));
  968. ret->type = cur->type;
  969. ret->ocur = cur->ocur;
  970. if (cur->name != NULL) {
  971. if (dict)
  972. ret->name = xmlDictLookup(dict, cur->name, -1);
  973. else
  974. ret->name = xmlStrdup(cur->name);
  975. }
  976. if (cur->prefix != NULL) {
  977. if (dict)
  978. ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
  979. else
  980. ret->prefix = xmlStrdup(cur->prefix);
  981. }
  982. if (cur->c1 != NULL)
  983. ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
  984. if (ret->c1 != NULL)
  985. ret->c1->parent = ret;
  986. if (cur->c2 != NULL) {
  987. prev = ret;
  988. cur = cur->c2;
  989. while (cur != NULL) {
  990. tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
  991. if (tmp == NULL) {
  992. xmlVErrMemory(NULL, "malloc failed");
  993. return(ret);
  994. }
  995. memset(tmp, 0, sizeof(xmlElementContent));
  996. tmp->type = cur->type;
  997. tmp->ocur = cur->ocur;
  998. prev->c2 = tmp;
  999. if (cur->name != NULL) {
  1000. if (dict)
  1001. tmp->name = xmlDictLookup(dict, cur->name, -1);
  1002. else
  1003. tmp->name = xmlStrdup(cur->name);
  1004. }
  1005. if (cur->prefix != NULL) {
  1006. if (dict)
  1007. tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
  1008. else
  1009. tmp->prefix = xmlStrdup(cur->prefix);
  1010. }
  1011. if (cur->c1 != NULL)
  1012. tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
  1013. if (tmp->c1 != NULL)
  1014. tmp->c1->parent = ret;
  1015. prev = tmp;
  1016. cur = cur->c2;
  1017. }
  1018. }
  1019. return(ret);
  1020. }
  1021. /**
  1022. * xmlCopyElementContent:
  1023. * @cur: An element content pointer.
  1024. *
  1025. * Build a copy of an element content description.
  1026. * Deprecated, use xmlCopyDocElementContent instead
  1027. *
  1028. * Returns the new xmlElementContentPtr or NULL in case of error.
  1029. */
  1030. xmlElementContentPtr
  1031. xmlCopyElementContent(xmlElementContentPtr cur) {
  1032. return(xmlCopyDocElementContent(NULL, cur));
  1033. }
  1034. /**
  1035. * xmlFreeDocElementContent:
  1036. * @doc: the document owning the element declaration
  1037. * @cur: the element content tree to free
  1038. *
  1039. * Free an element content structure. The whole subtree is removed.
  1040. */
  1041. void
  1042. xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
  1043. xmlDictPtr dict = NULL;
  1044. size_t depth = 0;
  1045. if (cur == NULL)
  1046. return;
  1047. if (doc != NULL)
  1048. dict = doc->dict;
  1049. while (1) {
  1050. xmlElementContentPtr parent;
  1051. while ((cur->c1 != NULL) || (cur->c2 != NULL)) {
  1052. cur = (cur->c1 != NULL) ? cur->c1 : cur->c2;
  1053. depth += 1;
  1054. }
  1055. switch (cur->type) {
  1056. case XML_ELEMENT_CONTENT_PCDATA:
  1057. case XML_ELEMENT_CONTENT_ELEMENT:
  1058. case XML_ELEMENT_CONTENT_SEQ:
  1059. case XML_ELEMENT_CONTENT_OR:
  1060. break;
  1061. default:
  1062. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  1063. "Internal: ELEMENT content corrupted invalid type\n",
  1064. NULL);
  1065. return;
  1066. }
  1067. if (dict) {
  1068. if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
  1069. xmlFree((xmlChar *) cur->name);
  1070. if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
  1071. xmlFree((xmlChar *) cur->prefix);
  1072. } else {
  1073. if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
  1074. if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
  1075. }
  1076. parent = cur->parent;
  1077. if ((depth == 0) || (parent == NULL)) {
  1078. xmlFree(cur);
  1079. break;
  1080. }
  1081. if (cur == parent->c1)
  1082. parent->c1 = NULL;
  1083. else
  1084. parent->c2 = NULL;
  1085. xmlFree(cur);
  1086. if (parent->c2 != NULL) {
  1087. cur = parent->c2;
  1088. } else {
  1089. depth -= 1;
  1090. cur = parent;
  1091. }
  1092. }
  1093. }
  1094. /**
  1095. * xmlFreeElementContent:
  1096. * @cur: the element content tree to free
  1097. *
  1098. * Free an element content structure. The whole subtree is removed.
  1099. * Deprecated, use xmlFreeDocElementContent instead
  1100. */
  1101. void
  1102. xmlFreeElementContent(xmlElementContentPtr cur) {
  1103. xmlFreeDocElementContent(NULL, cur);
  1104. }
  1105. #ifdef LIBXML_OUTPUT_ENABLED
  1106. /**
  1107. * xmlDumpElementOccur:
  1108. * @buf: An XML buffer
  1109. * @cur: An element table
  1110. *
  1111. * Dump the occurrence operator of an element.
  1112. */
  1113. static void
  1114. xmlDumpElementOccur(xmlBufferPtr buf, xmlElementContentPtr cur) {
  1115. switch (cur->ocur) {
  1116. case XML_ELEMENT_CONTENT_ONCE:
  1117. break;
  1118. case XML_ELEMENT_CONTENT_OPT:
  1119. xmlBufferWriteChar(buf, "?");
  1120. break;
  1121. case XML_ELEMENT_CONTENT_MULT:
  1122. xmlBufferWriteChar(buf, "*");
  1123. break;
  1124. case XML_ELEMENT_CONTENT_PLUS:
  1125. xmlBufferWriteChar(buf, "+");
  1126. break;
  1127. }
  1128. }
  1129. /**
  1130. * xmlDumpElementContent:
  1131. * @buf: An XML buffer
  1132. * @content: An element table
  1133. *
  1134. * This will dump the content of the element table as an XML DTD definition
  1135. */
  1136. static void
  1137. xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content) {
  1138. xmlElementContentPtr cur;
  1139. if (content == NULL) return;
  1140. xmlBufferWriteChar(buf, "(");
  1141. cur = content;
  1142. do {
  1143. if (cur == NULL) return;
  1144. switch (cur->type) {
  1145. case XML_ELEMENT_CONTENT_PCDATA:
  1146. xmlBufferWriteChar(buf, "#PCDATA");
  1147. break;
  1148. case XML_ELEMENT_CONTENT_ELEMENT:
  1149. if (cur->prefix != NULL) {
  1150. xmlBufferWriteCHAR(buf, cur->prefix);
  1151. xmlBufferWriteChar(buf, ":");
  1152. }
  1153. xmlBufferWriteCHAR(buf, cur->name);
  1154. break;
  1155. case XML_ELEMENT_CONTENT_SEQ:
  1156. case XML_ELEMENT_CONTENT_OR:
  1157. if ((cur != content) &&
  1158. (cur->parent != NULL) &&
  1159. ((cur->type != cur->parent->type) ||
  1160. (cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
  1161. xmlBufferWriteChar(buf, "(");
  1162. cur = cur->c1;
  1163. continue;
  1164. default:
  1165. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  1166. "Internal: ELEMENT cur corrupted invalid type\n",
  1167. NULL);
  1168. }
  1169. while (cur != content) {
  1170. xmlElementContentPtr parent = cur->parent;
  1171. if (parent == NULL) return;
  1172. if (((cur->type == XML_ELEMENT_CONTENT_OR) ||
  1173. (cur->type == XML_ELEMENT_CONTENT_SEQ)) &&
  1174. ((cur->type != parent->type) ||
  1175. (cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
  1176. xmlBufferWriteChar(buf, ")");
  1177. xmlDumpElementOccur(buf, cur);
  1178. if (cur == parent->c1) {
  1179. if (parent->type == XML_ELEMENT_CONTENT_SEQ)
  1180. xmlBufferWriteChar(buf, " , ");
  1181. else if (parent->type == XML_ELEMENT_CONTENT_OR)
  1182. xmlBufferWriteChar(buf, " | ");
  1183. cur = parent->c2;
  1184. break;
  1185. }
  1186. cur = parent;
  1187. }
  1188. } while (cur != content);
  1189. xmlBufferWriteChar(buf, ")");
  1190. xmlDumpElementOccur(buf, content);
  1191. }
  1192. /**
  1193. * xmlSprintfElementContent:
  1194. * @buf: an output buffer
  1195. * @content: An element table
  1196. * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
  1197. *
  1198. * Deprecated, unsafe, use xmlSnprintfElementContent
  1199. */
  1200. void
  1201. xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
  1202. xmlElementContentPtr content ATTRIBUTE_UNUSED,
  1203. int englob ATTRIBUTE_UNUSED) {
  1204. }
  1205. #endif /* LIBXML_OUTPUT_ENABLED */
  1206. /**
  1207. * xmlSnprintfElementContent:
  1208. * @buf: an output buffer
  1209. * @size: the buffer size
  1210. * @content: An element table
  1211. * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
  1212. *
  1213. * This will dump the content of the element content definition
  1214. * Intended just for the debug routine
  1215. */
  1216. void
  1217. xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) {
  1218. int len;
  1219. if (content == NULL) return;
  1220. len = strlen(buf);
  1221. if (size - len < 50) {
  1222. if ((size - len > 4) && (buf[len - 1] != '.'))
  1223. strcat(buf, " ...");
  1224. return;
  1225. }
  1226. if (englob) strcat(buf, "(");
  1227. switch (content->type) {
  1228. case XML_ELEMENT_CONTENT_PCDATA:
  1229. strcat(buf, "#PCDATA");
  1230. break;
  1231. case XML_ELEMENT_CONTENT_ELEMENT: {
  1232. int qnameLen = xmlStrlen(content->name);
  1233. if (content->prefix != NULL)
  1234. qnameLen += xmlStrlen(content->prefix) + 1;
  1235. if (size - len < qnameLen + 10) {
  1236. strcat(buf, " ...");
  1237. return;
  1238. }
  1239. if (content->prefix != NULL) {
  1240. strcat(buf, (char *) content->prefix);
  1241. strcat(buf, ":");
  1242. }
  1243. if (content->name != NULL)
  1244. strcat(buf, (char *) content->name);
  1245. break;
  1246. }
  1247. case XML_ELEMENT_CONTENT_SEQ:
  1248. if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
  1249. (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
  1250. xmlSnprintfElementContent(buf, size, content->c1, 1);
  1251. else
  1252. xmlSnprintfElementContent(buf, size, content->c1, 0);
  1253. len = strlen(buf);
  1254. if (size - len < 50) {
  1255. if ((size - len > 4) && (buf[len - 1] != '.'))
  1256. strcat(buf, " ...");
  1257. return;
  1258. }
  1259. strcat(buf, " , ");
  1260. if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
  1261. (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
  1262. (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
  1263. xmlSnprintfElementContent(buf, size, content->c2, 1);
  1264. else
  1265. xmlSnprintfElementContent(buf, size, content->c2, 0);
  1266. break;
  1267. case XML_ELEMENT_CONTENT_OR:
  1268. if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
  1269. (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
  1270. xmlSnprintfElementContent(buf, size, content->c1, 1);
  1271. else
  1272. xmlSnprintfElementContent(buf, size, content->c1, 0);
  1273. len = strlen(buf);
  1274. if (size - len < 50) {
  1275. if ((size - len > 4) && (buf[len - 1] != '.'))
  1276. strcat(buf, " ...");
  1277. return;
  1278. }
  1279. strcat(buf, " | ");
  1280. if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
  1281. (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
  1282. (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
  1283. xmlSnprintfElementContent(buf, size, content->c2, 1);
  1284. else
  1285. xmlSnprintfElementContent(buf, size, content->c2, 0);
  1286. break;
  1287. }
  1288. if (size - strlen(buf) <= 2) return;
  1289. if (englob)
  1290. strcat(buf, ")");
  1291. switch (content->ocur) {
  1292. case XML_ELEMENT_CONTENT_ONCE:
  1293. break;
  1294. case XML_ELEMENT_CONTENT_OPT:
  1295. strcat(buf, "?");
  1296. break;
  1297. case XML_ELEMENT_CONTENT_MULT:
  1298. strcat(buf, "*");
  1299. break;
  1300. case XML_ELEMENT_CONTENT_PLUS:
  1301. strcat(buf, "+");
  1302. break;
  1303. }
  1304. }
  1305. /****************************************************************
  1306. * *
  1307. * Registration of DTD declarations *
  1308. * *
  1309. ****************************************************************/
  1310. /**
  1311. * xmlFreeElement:
  1312. * @elem: An element
  1313. *
  1314. * Deallocate the memory used by an element definition
  1315. */
  1316. static void
  1317. xmlFreeElement(xmlElementPtr elem) {
  1318. if (elem == NULL) return;
  1319. xmlUnlinkNode((xmlNodePtr) elem);
  1320. xmlFreeDocElementContent(elem->doc, elem->content);
  1321. if (elem->name != NULL)
  1322. xmlFree((xmlChar *) elem->name);
  1323. if (elem->prefix != NULL)
  1324. xmlFree((xmlChar *) elem->prefix);
  1325. #ifdef LIBXML_REGEXP_ENABLED
  1326. if (elem->contModel != NULL)
  1327. xmlRegFreeRegexp(elem->contModel);
  1328. #endif
  1329. xmlFree(elem);
  1330. }
  1331. /**
  1332. * xmlAddElementDecl:
  1333. * @ctxt: the validation context
  1334. * @dtd: pointer to the DTD
  1335. * @name: the entity name
  1336. * @type: the element type
  1337. * @content: the element content tree or NULL
  1338. *
  1339. * Register a new element declaration
  1340. *
  1341. * Returns NULL if not, otherwise the entity
  1342. */
  1343. xmlElementPtr
  1344. xmlAddElementDecl(xmlValidCtxtPtr ctxt,
  1345. xmlDtdPtr dtd, const xmlChar *name,
  1346. xmlElementTypeVal type,
  1347. xmlElementContentPtr content) {
  1348. xmlElementPtr ret;
  1349. xmlElementTablePtr table;
  1350. xmlAttributePtr oldAttributes = NULL;
  1351. xmlChar *ns, *uqname;
  1352. if (dtd == NULL) {
  1353. return(NULL);
  1354. }
  1355. if (name == NULL) {
  1356. return(NULL);
  1357. }
  1358. switch (type) {
  1359. case XML_ELEMENT_TYPE_EMPTY:
  1360. if (content != NULL) {
  1361. xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
  1362. "xmlAddElementDecl: content != NULL for EMPTY\n",
  1363. NULL);
  1364. return(NULL);
  1365. }
  1366. break;
  1367. case XML_ELEMENT_TYPE_ANY:
  1368. if (content != NULL) {
  1369. xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
  1370. "xmlAddElementDecl: content != NULL for ANY\n",
  1371. NULL);
  1372. return(NULL);
  1373. }
  1374. break;
  1375. case XML_ELEMENT_TYPE_MIXED:
  1376. if (content == NULL) {
  1377. xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
  1378. "xmlAddElementDecl: content == NULL for MIXED\n",
  1379. NULL);
  1380. return(NULL);
  1381. }
  1382. break;
  1383. case XML_ELEMENT_TYPE_ELEMENT:
  1384. if (content == NULL) {
  1385. xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
  1386. "xmlAddElementDecl: content == NULL for ELEMENT\n",
  1387. NULL);
  1388. return(NULL);
  1389. }
  1390. break;
  1391. default:
  1392. xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
  1393. "Internal: ELEMENT decl corrupted invalid type\n",
  1394. NULL);
  1395. return(NULL);
  1396. }
  1397. /*
  1398. * check if name is a QName
  1399. */
  1400. uqname = xmlSplitQName2(name, &ns);
  1401. if (uqname != NULL)
  1402. name = uqname;
  1403. /*
  1404. * Create the Element table if needed.
  1405. */
  1406. table = (xmlElementTablePtr) dtd->elements;
  1407. if (table == NULL) {
  1408. xmlDictPtr dict = NULL;
  1409. if (dtd->doc != NULL)
  1410. dict = dtd->doc->dict;
  1411. table = xmlHashCreateDict(0, dict);
  1412. dtd->elements = (void *) table;
  1413. }
  1414. if (table == NULL) {
  1415. xmlVErrMemory(ctxt,
  1416. "xmlAddElementDecl: Table creation failed!\n");
  1417. if (uqname != NULL)
  1418. xmlFree(uqname);
  1419. if (ns != NULL)
  1420. xmlFree(ns);
  1421. return(NULL);
  1422. }
  1423. /*
  1424. * lookup old attributes inserted on an undefined element in the
  1425. * internal subset.
  1426. */
  1427. if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
  1428. ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
  1429. if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
  1430. oldAttributes = ret->attributes;
  1431. ret->attributes = NULL;
  1432. xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
  1433. xmlFreeElement(ret);
  1434. }
  1435. }
  1436. /*
  1437. * The element may already be present if one of its attribute
  1438. * was registered first
  1439. */
  1440. ret = xmlHashLookup2(table, name, ns);
  1441. if (ret != NULL) {
  1442. if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
  1443. #ifdef LIBXML_VALID_ENABLED
  1444. /*
  1445. * The element is already defined in this DTD.
  1446. */
  1447. xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
  1448. "Redefinition of element %s\n",
  1449. name, NULL, NULL);
  1450. #endif /* LIBXML_VALID_ENABLED */
  1451. if (uqname != NULL)
  1452. xmlFree(uqname);
  1453. if (ns != NULL)
  1454. xmlFree(ns);
  1455. return(NULL);
  1456. }
  1457. if (ns != NULL) {
  1458. xmlFree(ns);
  1459. ns = NULL;
  1460. }
  1461. } else {
  1462. ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
  1463. if (ret == NULL) {
  1464. xmlVErrMemory(ctxt, "malloc failed");
  1465. if (uqname != NULL)
  1466. xmlFree(uqname);
  1467. if (ns != NULL)
  1468. xmlFree(ns);
  1469. return(NULL);
  1470. }
  1471. memset(ret, 0, sizeof(xmlElement));
  1472. ret->type = XML_ELEMENT_DECL;
  1473. /*
  1474. * fill the structure.
  1475. */
  1476. ret->name = xmlStrdup(name);
  1477. if (ret->name == NULL) {
  1478. xmlVErrMemory(ctxt, "malloc failed");
  1479. if (uqname != NULL)
  1480. xmlFree(uqname);
  1481. if (ns != NULL)
  1482. xmlFree(ns);
  1483. xmlFree(ret);
  1484. return(NULL);
  1485. }
  1486. ret->prefix = ns;
  1487. /*
  1488. * Validity Check:
  1489. * Insertion must not fail
  1490. */
  1491. if (xmlHashAddEntry2(table, name, ns, ret)) {
  1492. #ifdef LIBXML_VALID_ENABLED
  1493. /*
  1494. * The element is already defined in this DTD.
  1495. */
  1496. xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
  1497. "Redefinition of element %s\n",
  1498. name, NULL, NULL);
  1499. #endif /* LIBXML_VALID_ENABLED */
  1500. xmlFreeElement(ret);
  1501. if (uqname != NULL)
  1502. xmlFree(uqname);
  1503. return(NULL);
  1504. }
  1505. /*
  1506. * For new element, may have attributes from earlier
  1507. * definition in internal subset
  1508. */
  1509. ret->attributes = oldAttributes;
  1510. }
  1511. /*
  1512. * Finish to fill the structure.
  1513. */
  1514. ret->etype = type;
  1515. /*
  1516. * Avoid a stupid copy when called by the parser
  1517. * and flag it by setting a special parent value
  1518. * so the parser doesn't unallocate it.
  1519. */
  1520. if ((ctxt != NULL) &&
  1521. ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
  1522. (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1))) {
  1523. ret->content = content;
  1524. if (content != NULL)
  1525. content->parent = (xmlElementContentPtr) 1;
  1526. } else {
  1527. ret->content = xmlCopyDocElementContent(dtd->doc, content);
  1528. }
  1529. /*
  1530. * Link it to the DTD
  1531. */
  1532. ret->parent = dtd;
  1533. ret->doc = dtd->doc;
  1534. if (dtd->last == NULL) {
  1535. dtd->children = dtd->last = (xmlNodePtr) ret;
  1536. } else {
  1537. dtd->last->next = (xmlNodePtr) ret;
  1538. ret->prev = dtd->last;
  1539. dtd->last = (xmlNodePtr) ret;
  1540. }
  1541. if (uqname != NULL)
  1542. xmlFree(uqname);
  1543. return(ret);
  1544. }
  1545. static void
  1546. xmlFreeElementTableEntry(void *elem, const xmlChar *name ATTRIBUTE_UNUSED) {
  1547. xmlFreeElement((xmlElementPtr) elem);
  1548. }
  1549. /**
  1550. * xmlFreeElementTable:
  1551. * @table: An element table
  1552. *
  1553. * Deallocate the memory used by an element hash table.
  1554. */
  1555. void
  1556. xmlFreeElementTable(xmlElementTablePtr table) {
  1557. xmlHashFree(table, xmlFreeElementTableEntry);
  1558. }
  1559. #ifdef LIBXML_TREE_ENABLED
  1560. /**
  1561. * xmlCopyElement:
  1562. * @elem: An element
  1563. *
  1564. * Build a copy of an element.
  1565. *
  1566. * Returns the new xmlElementPtr or NULL in case of error.
  1567. */
  1568. static void *
  1569. xmlCopyElement(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
  1570. xmlElementPtr elem = (xmlElementPtr) payload;
  1571. xmlElementPtr cur;
  1572. cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
  1573. if (cur == NULL) {
  1574. xmlVErrMemory(NULL, "malloc failed");
  1575. return(NULL);
  1576. }
  1577. memset(cur, 0, sizeof(xmlElement));
  1578. cur->type = XML_ELEMENT_DECL;
  1579. cur->etype = elem->etype;
  1580. if (elem->name != NULL)
  1581. cur->name = xmlStrdup(elem->name);
  1582. else
  1583. cur->name = NULL;
  1584. if (elem->prefix != NULL)
  1585. cur->prefix = xmlStrdup(elem->prefix);
  1586. else
  1587. cur->prefix = NULL;
  1588. cur->content = xmlCopyElementContent(elem->content);
  1589. /* TODO : rebuild the attribute list on the copy */
  1590. cur->attributes = NULL;
  1591. return(cur);
  1592. }
  1593. /**
  1594. * xmlCopyElementTable:
  1595. * @table: An element table
  1596. *
  1597. * Build a copy of an element table.
  1598. *
  1599. * Returns the new xmlElementTablePtr or NULL in case of error.
  1600. */
  1601. xmlElementTablePtr
  1602. xmlCopyElementTable(xmlElementTablePtr table) {
  1603. return((xmlElementTablePtr) xmlHashCopy(table, xmlCopyElement));
  1604. }
  1605. #endif /* LIBXML_TREE_ENABLED */
  1606. #ifdef LIBXML_OUTPUT_ENABLED
  1607. /**
  1608. * xmlDumpElementDecl:
  1609. * @buf: the XML buffer output
  1610. * @elem: An element table
  1611. *
  1612. * This will dump the content of the element declaration as an XML
  1613. * DTD definition
  1614. */
  1615. void
  1616. xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
  1617. if ((buf == NULL) || (elem == NULL))
  1618. return;
  1619. switch (elem->etype) {
  1620. case XML_ELEMENT_TYPE_EMPTY:
  1621. xmlBufferWriteChar(buf, "<!ELEMENT ");
  1622. if (elem->prefix != NULL) {
  1623. xmlBufferWriteCHAR(buf, elem->prefix);
  1624. xmlBufferWriteChar(buf, ":");
  1625. }
  1626. xmlBufferWriteCHAR(buf, elem->name);
  1627. xmlBufferWriteChar(buf, " EMPTY>\n");
  1628. break;
  1629. case XML_ELEMENT_TYPE_ANY:
  1630. xmlBufferWriteChar(buf, "<!ELEMENT ");
  1631. if (elem->prefix != NULL) {
  1632. xmlBufferWriteCHAR(buf, elem->prefix);
  1633. xmlBufferWriteChar(buf, ":");
  1634. }
  1635. xmlBufferWriteCHAR(buf, elem->name);
  1636. xmlBufferWriteChar(buf, " ANY>\n");
  1637. break;
  1638. case XML_ELEMENT_TYPE_MIXED:
  1639. xmlBufferWriteChar(buf, "<!ELEMENT ");
  1640. if (elem->prefix != NULL) {
  1641. xmlBufferWriteCHAR(buf, elem->prefix);
  1642. xmlBufferWriteChar(buf, ":");
  1643. }
  1644. xmlBufferWriteCHAR(buf, elem->name);
  1645. xmlBufferWriteChar(buf, " ");
  1646. xmlDumpElementContent(buf, elem->content);
  1647. xmlBufferWriteChar(buf, ">\n");
  1648. break;
  1649. case XML_ELEMENT_TYPE_ELEMENT:
  1650. xmlBufferWriteChar(buf, "<!ELEMENT ");
  1651. if (elem->prefix != NULL) {
  1652. xmlBufferWriteCHAR(buf, elem->prefix);
  1653. xmlBufferWriteChar(buf, ":");
  1654. }
  1655. xmlBufferWriteCHAR(buf, elem->name);
  1656. xmlBufferWriteChar(buf, " ");
  1657. xmlDumpElementContent(buf, elem->content);
  1658. xmlBufferWriteChar(buf, ">\n");
  1659. break;
  1660. default:
  1661. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  1662. "Internal: ELEMENT struct corrupted invalid type\n",
  1663. NULL);
  1664. }
  1665. }
  1666. /**
  1667. * xmlDumpElementDeclScan:
  1668. * @elem: An element table
  1669. * @buf: the XML buffer output
  1670. *
  1671. * This routine is used by the hash scan function. It just reverses
  1672. * the arguments.
  1673. */
  1674. static void
  1675. xmlDumpElementDeclScan(void *elem, void *buf,
  1676. const xmlChar *name ATTRIBUTE_UNUSED) {
  1677. xmlDumpElementDecl((xmlBufferPtr) buf, (xmlElementPtr) elem);
  1678. }
  1679. /**
  1680. * xmlDumpElementTable:
  1681. * @buf: the XML buffer output
  1682. * @table: An element table
  1683. *
  1684. * This will dump the content of the element table as an XML DTD definition
  1685. */
  1686. void
  1687. xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
  1688. if ((buf == NULL) || (table == NULL))
  1689. return;
  1690. xmlHashScan(table, xmlDumpElementDeclScan, buf);
  1691. }
  1692. #endif /* LIBXML_OUTPUT_ENABLED */
  1693. /**
  1694. * xmlCreateEnumeration:
  1695. * @name: the enumeration name or NULL
  1696. *
  1697. * create and initialize an enumeration attribute node.
  1698. *
  1699. * Returns the xmlEnumerationPtr just created or NULL in case
  1700. * of error.
  1701. */
  1702. xmlEnumerationPtr
  1703. xmlCreateEnumeration(const xmlChar *name) {
  1704. xmlEnumerationPtr ret;
  1705. ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
  1706. if (ret == NULL) {
  1707. xmlVErrMemory(NULL, "malloc failed");
  1708. return(NULL);
  1709. }
  1710. memset(ret, 0, sizeof(xmlEnumeration));
  1711. if (name != NULL)
  1712. ret->name = xmlStrdup(name);
  1713. return(ret);
  1714. }
  1715. /**
  1716. * xmlFreeEnumeration:
  1717. * @cur: the tree to free.
  1718. *
  1719. * free an enumeration attribute node (recursive).
  1720. */
  1721. void
  1722. xmlFreeEnumeration(xmlEnumerationPtr cur) {
  1723. if (cur == NULL) return;
  1724. if (cur->next != NULL) xmlFreeEnumeration(cur->next);
  1725. if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
  1726. xmlFree(cur);
  1727. }
  1728. #ifdef LIBXML_TREE_ENABLED
  1729. /**
  1730. * xmlCopyEnumeration:
  1731. * @cur: the tree to copy.
  1732. *
  1733. * Copy an enumeration attribute node (recursive).
  1734. *
  1735. * Returns the xmlEnumerationPtr just created or NULL in case
  1736. * of error.
  1737. */
  1738. xmlEnumerationPtr
  1739. xmlCopyEnumeration(xmlEnumerationPtr cur) {
  1740. xmlEnumerationPtr ret;
  1741. if (cur == NULL) return(NULL);
  1742. ret = xmlCreateEnumeration((xmlChar *) cur->name);
  1743. if (ret == NULL) return(NULL);
  1744. if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
  1745. else ret->next = NULL;
  1746. return(ret);
  1747. }
  1748. #endif /* LIBXML_TREE_ENABLED */
  1749. #ifdef LIBXML_OUTPUT_ENABLED
  1750. /**
  1751. * xmlDumpEnumeration:
  1752. * @buf: the XML buffer output
  1753. * @enum: An enumeration
  1754. *
  1755. * This will dump the content of the enumeration
  1756. */
  1757. static void
  1758. xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
  1759. if ((buf == NULL) || (cur == NULL))
  1760. return;
  1761. xmlBufferWriteCHAR(buf, cur->name);
  1762. if (cur->next == NULL)
  1763. xmlBufferWriteChar(buf, ")");
  1764. else {
  1765. xmlBufferWriteChar(buf, " | ");
  1766. xmlDumpEnumeration(buf, cur->next);
  1767. }
  1768. }
  1769. #endif /* LIBXML_OUTPUT_ENABLED */
  1770. #ifdef LIBXML_VALID_ENABLED
  1771. /**
  1772. * xmlScanIDAttributeDecl:
  1773. * @ctxt: the validation context
  1774. * @elem: the element name
  1775. * @err: whether to raise errors here
  1776. *
  1777. * Verify that the element don't have too many ID attributes
  1778. * declared.
  1779. *
  1780. * Returns the number of ID attributes found.
  1781. */
  1782. static int
  1783. xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) {
  1784. xmlAttributePtr cur;
  1785. int ret = 0;
  1786. if (elem == NULL) return(0);
  1787. cur = elem->attributes;
  1788. while (cur != NULL) {
  1789. if (cur->atype == XML_ATTRIBUTE_ID) {
  1790. ret ++;
  1791. if ((ret > 1) && (err))
  1792. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
  1793. "Element %s has too many ID attributes defined : %s\n",
  1794. elem->name, cur->name, NULL);
  1795. }
  1796. cur = cur->nexth;
  1797. }
  1798. return(ret);
  1799. }
  1800. #endif /* LIBXML_VALID_ENABLED */
  1801. /**
  1802. * xmlFreeAttribute:
  1803. * @elem: An attribute
  1804. *
  1805. * Deallocate the memory used by an attribute definition
  1806. */
  1807. static void
  1808. xmlFreeAttribute(xmlAttributePtr attr) {
  1809. xmlDictPtr dict;
  1810. if (attr == NULL) return;
  1811. if (attr->doc != NULL)
  1812. dict = attr->doc->dict;
  1813. else
  1814. dict = NULL;
  1815. xmlUnlinkNode((xmlNodePtr) attr);
  1816. if (attr->tree != NULL)
  1817. xmlFreeEnumeration(attr->tree);
  1818. if (dict) {
  1819. if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
  1820. xmlFree((xmlChar *) attr->elem);
  1821. if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
  1822. xmlFree((xmlChar *) attr->name);
  1823. if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
  1824. xmlFree((xmlChar *) attr->prefix);
  1825. if ((attr->defaultValue != NULL) &&
  1826. (!xmlDictOwns(dict, attr->defaultValue)))
  1827. xmlFree((xmlChar *) attr->defaultValue);
  1828. } else {
  1829. if (attr->elem != NULL)
  1830. xmlFree((xmlChar *) attr->elem);
  1831. if (attr->name != NULL)
  1832. xmlFree((xmlChar *) attr->name);
  1833. if (attr->defaultValue != NULL)
  1834. xmlFree((xmlChar *) attr->defaultValue);
  1835. if (attr->prefix != NULL)
  1836. xmlFree((xmlChar *) attr->prefix);
  1837. }
  1838. xmlFree(attr);
  1839. }
  1840. /**
  1841. * xmlAddAttributeDecl:
  1842. * @ctxt: the validation context
  1843. * @dtd: pointer to the DTD
  1844. * @elem: the element name
  1845. * @name: the attribute name
  1846. * @ns: the attribute namespace prefix
  1847. * @type: the attribute type
  1848. * @def: the attribute default type
  1849. * @defaultValue: the attribute default value
  1850. * @tree: if it's an enumeration, the associated list
  1851. *
  1852. * Register a new attribute declaration
  1853. * Note that @tree becomes the ownership of the DTD
  1854. *
  1855. * Returns NULL if not new, otherwise the attribute decl
  1856. */
  1857. xmlAttributePtr
  1858. xmlAddAttributeDecl(xmlValidCtxtPtr ctxt,
  1859. xmlDtdPtr dtd, const xmlChar *elem,
  1860. const xmlChar *name, const xmlChar *ns,
  1861. xmlAttributeType type, xmlAttributeDefault def,
  1862. const xmlChar *defaultValue, xmlEnumerationPtr tree) {
  1863. xmlAttributePtr ret;
  1864. xmlAttributeTablePtr table;
  1865. xmlElementPtr elemDef;
  1866. xmlDictPtr dict = NULL;
  1867. if (dtd == NULL) {
  1868. xmlFreeEnumeration(tree);
  1869. return(NULL);
  1870. }
  1871. if (name == NULL) {
  1872. xmlFreeEnumeration(tree);
  1873. return(NULL);
  1874. }
  1875. if (elem == NULL) {
  1876. xmlFreeEnumeration(tree);
  1877. return(NULL);
  1878. }
  1879. if (dtd->doc != NULL)
  1880. dict = dtd->doc->dict;
  1881. #ifdef LIBXML_VALID_ENABLED
  1882. /*
  1883. * Check the type and possibly the default value.
  1884. */
  1885. switch (type) {
  1886. case XML_ATTRIBUTE_CDATA:
  1887. break;
  1888. case XML_ATTRIBUTE_ID:
  1889. break;
  1890. case XML_ATTRIBUTE_IDREF:
  1891. break;
  1892. case XML_ATTRIBUTE_IDREFS:
  1893. break;
  1894. case XML_ATTRIBUTE_ENTITY:
  1895. break;
  1896. case XML_ATTRIBUTE_ENTITIES:
  1897. break;
  1898. case XML_ATTRIBUTE_NMTOKEN:
  1899. break;
  1900. case XML_ATTRIBUTE_NMTOKENS:
  1901. break;
  1902. case XML_ATTRIBUTE_ENUMERATION:
  1903. break;
  1904. case XML_ATTRIBUTE_NOTATION:
  1905. break;
  1906. default:
  1907. xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
  1908. "Internal: ATTRIBUTE struct corrupted invalid type\n",
  1909. NULL);
  1910. xmlFreeEnumeration(tree);
  1911. return(NULL);
  1912. }
  1913. if ((defaultValue != NULL) &&
  1914. (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) {
  1915. xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
  1916. "Attribute %s of %s: invalid default value\n",
  1917. elem, name, defaultValue);
  1918. defaultValue = NULL;
  1919. if (ctxt != NULL)
  1920. ctxt->valid = 0;
  1921. }
  1922. #endif /* LIBXML_VALID_ENABLED */
  1923. /*
  1924. * Check first that an attribute defined in the external subset wasn't
  1925. * already defined in the internal subset
  1926. */
  1927. if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
  1928. (dtd->doc->intSubset != NULL) &&
  1929. (dtd->doc->intSubset->attributes != NULL)) {
  1930. ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
  1931. if (ret != NULL) {
  1932. xmlFreeEnumeration(tree);
  1933. return(NULL);
  1934. }
  1935. }
  1936. /*
  1937. * Create the Attribute table if needed.
  1938. */
  1939. table = (xmlAttributeTablePtr) dtd->attributes;
  1940. if (table == NULL) {
  1941. table = xmlHashCreateDict(0, dict);
  1942. dtd->attributes = (void *) table;
  1943. }
  1944. if (table == NULL) {
  1945. xmlVErrMemory(ctxt,
  1946. "xmlAddAttributeDecl: Table creation failed!\n");
  1947. xmlFreeEnumeration(tree);
  1948. return(NULL);
  1949. }
  1950. ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
  1951. if (ret == NULL) {
  1952. xmlVErrMemory(ctxt, "malloc failed");
  1953. xmlFreeEnumeration(tree);
  1954. return(NULL);
  1955. }
  1956. memset(ret, 0, sizeof(xmlAttribute));
  1957. ret->type = XML_ATTRIBUTE_DECL;
  1958. /*
  1959. * fill the structure.
  1960. */
  1961. ret->atype = type;
  1962. /*
  1963. * doc must be set before possible error causes call
  1964. * to xmlFreeAttribute (because it's used to check on
  1965. * dict use)
  1966. */
  1967. ret->doc = dtd->doc;
  1968. if (dict) {
  1969. ret->name = xmlDictLookup(dict, name, -1);
  1970. ret->prefix = xmlDictLookup(dict, ns, -1);
  1971. ret->elem = xmlDictLookup(dict, elem, -1);
  1972. } else {
  1973. ret->name = xmlStrdup(name);
  1974. ret->prefix = xmlStrdup(ns);
  1975. ret->elem = xmlStrdup(elem);
  1976. }
  1977. ret->def = def;
  1978. ret->tree = tree;
  1979. if (defaultValue != NULL) {
  1980. if (dict)
  1981. ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
  1982. else
  1983. ret->defaultValue = xmlStrdup(defaultValue);
  1984. }
  1985. /*
  1986. * Validity Check:
  1987. * Search the DTD for previous declarations of the ATTLIST
  1988. */
  1989. if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) {
  1990. #ifdef LIBXML_VALID_ENABLED
  1991. /*
  1992. * The attribute is already defined in this DTD.
  1993. */
  1994. xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
  1995. "Attribute %s of element %s: already defined\n",
  1996. name, elem, NULL);
  1997. #endif /* LIBXML_VALID_ENABLED */
  1998. xmlFreeAttribute(ret);
  1999. return(NULL);
  2000. }
  2001. /*
  2002. * Validity Check:
  2003. * Multiple ID per element
  2004. */
  2005. elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
  2006. if (elemDef != NULL) {
  2007. #ifdef LIBXML_VALID_ENABLED
  2008. if ((type == XML_ATTRIBUTE_ID) &&
  2009. (xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) {
  2010. xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
  2011. "Element %s has too may ID attributes defined : %s\n",
  2012. elem, name, NULL);
  2013. if (ctxt != NULL)
  2014. ctxt->valid = 0;
  2015. }
  2016. #endif /* LIBXML_VALID_ENABLED */
  2017. /*
  2018. * Insert namespace default def first they need to be
  2019. * processed first.
  2020. */
  2021. if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
  2022. ((ret->prefix != NULL &&
  2023. (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
  2024. ret->nexth = elemDef->attributes;
  2025. elemDef->attributes = ret;
  2026. } else {
  2027. xmlAttributePtr tmp = elemDef->attributes;
  2028. while ((tmp != NULL) &&
  2029. ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
  2030. ((ret->prefix != NULL &&
  2031. (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
  2032. if (tmp->nexth == NULL)
  2033. break;
  2034. tmp = tmp->nexth;
  2035. }
  2036. if (tmp != NULL) {
  2037. ret->nexth = tmp->nexth;
  2038. tmp->nexth = ret;
  2039. } else {
  2040. ret->nexth = elemDef->attributes;
  2041. elemDef->attributes = ret;
  2042. }
  2043. }
  2044. }
  2045. /*
  2046. * Link it to the DTD
  2047. */
  2048. ret->parent = dtd;
  2049. if (dtd->last == NULL) {
  2050. dtd->children = dtd->last = (xmlNodePtr) ret;
  2051. } else {
  2052. dtd->last->next = (xmlNodePtr) ret;
  2053. ret->prev = dtd->last;
  2054. dtd->last = (xmlNodePtr) ret;
  2055. }
  2056. return(ret);
  2057. }
  2058. static void
  2059. xmlFreeAttributeTableEntry(void *attr, const xmlChar *name ATTRIBUTE_UNUSED) {
  2060. xmlFreeAttribute((xmlAttributePtr) attr);
  2061. }
  2062. /**
  2063. * xmlFreeAttributeTable:
  2064. * @table: An attribute table
  2065. *
  2066. * Deallocate the memory used by an entities hash table.
  2067. */
  2068. void
  2069. xmlFreeAttributeTable(xmlAttributeTablePtr table) {
  2070. xmlHashFree(table, xmlFreeAttributeTableEntry);
  2071. }
  2072. #ifdef LIBXML_TREE_ENABLED
  2073. /**
  2074. * xmlCopyAttribute:
  2075. * @attr: An attribute
  2076. *
  2077. * Build a copy of an attribute.
  2078. *
  2079. * Returns the new xmlAttributePtr or NULL in case of error.
  2080. */
  2081. static void *
  2082. xmlCopyAttribute(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
  2083. xmlAttributePtr attr = (xmlAttributePtr) payload;
  2084. xmlAttributePtr cur;
  2085. cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
  2086. if (cur == NULL) {
  2087. xmlVErrMemory(NULL, "malloc failed");
  2088. return(NULL);
  2089. }
  2090. memset(cur, 0, sizeof(xmlAttribute));
  2091. cur->type = XML_ATTRIBUTE_DECL;
  2092. cur->atype = attr->atype;
  2093. cur->def = attr->def;
  2094. cur->tree = xmlCopyEnumeration(attr->tree);
  2095. if (attr->elem != NULL)
  2096. cur->elem = xmlStrdup(attr->elem);
  2097. if (attr->name != NULL)
  2098. cur->name = xmlStrdup(attr->name);
  2099. if (attr->prefix != NULL)
  2100. cur->prefix = xmlStrdup(attr->prefix);
  2101. if (attr->defaultValue != NULL)
  2102. cur->defaultValue = xmlStrdup(attr->defaultValue);
  2103. return(cur);
  2104. }
  2105. /**
  2106. * xmlCopyAttributeTable:
  2107. * @table: An attribute table
  2108. *
  2109. * Build a copy of an attribute table.
  2110. *
  2111. * Returns the new xmlAttributeTablePtr or NULL in case of error.
  2112. */
  2113. xmlAttributeTablePtr
  2114. xmlCopyAttributeTable(xmlAttributeTablePtr table) {
  2115. return((xmlAttributeTablePtr) xmlHashCopy(table, xmlCopyAttribute));
  2116. }
  2117. #endif /* LIBXML_TREE_ENABLED */
  2118. #ifdef LIBXML_OUTPUT_ENABLED
  2119. /**
  2120. * xmlDumpAttributeDecl:
  2121. * @buf: the XML buffer output
  2122. * @attr: An attribute declaration
  2123. *
  2124. * This will dump the content of the attribute declaration as an XML
  2125. * DTD definition
  2126. */
  2127. void
  2128. xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
  2129. if ((buf == NULL) || (attr == NULL))
  2130. return;
  2131. xmlBufferWriteChar(buf, "<!ATTLIST ");
  2132. xmlBufferWriteCHAR(buf, attr->elem);
  2133. xmlBufferWriteChar(buf, " ");
  2134. if (attr->prefix != NULL) {
  2135. xmlBufferWriteCHAR(buf, attr->prefix);
  2136. xmlBufferWriteChar(buf, ":");
  2137. }
  2138. xmlBufferWriteCHAR(buf, attr->name);
  2139. switch (attr->atype) {
  2140. case XML_ATTRIBUTE_CDATA:
  2141. xmlBufferWriteChar(buf, " CDATA");
  2142. break;
  2143. case XML_ATTRIBUTE_ID:
  2144. xmlBufferWriteChar(buf, " ID");
  2145. break;
  2146. case XML_ATTRIBUTE_IDREF:
  2147. xmlBufferWriteChar(buf, " IDREF");
  2148. break;
  2149. case XML_ATTRIBUTE_IDREFS:
  2150. xmlBufferWriteChar(buf, " IDREFS");
  2151. break;
  2152. case XML_ATTRIBUTE_ENTITY:
  2153. xmlBufferWriteChar(buf, " ENTITY");
  2154. break;
  2155. case XML_ATTRIBUTE_ENTITIES:
  2156. xmlBufferWriteChar(buf, " ENTITIES");
  2157. break;
  2158. case XML_ATTRIBUTE_NMTOKEN:
  2159. xmlBufferWriteChar(buf, " NMTOKEN");
  2160. break;
  2161. case XML_ATTRIBUTE_NMTOKENS:
  2162. xmlBufferWriteChar(buf, " NMTOKENS");
  2163. break;
  2164. case XML_ATTRIBUTE_ENUMERATION:
  2165. xmlBufferWriteChar(buf, " (");
  2166. xmlDumpEnumeration(buf, attr->tree);
  2167. break;
  2168. case XML_ATTRIBUTE_NOTATION:
  2169. xmlBufferWriteChar(buf, " NOTATION (");
  2170. xmlDumpEnumeration(buf, attr->tree);
  2171. break;
  2172. default:
  2173. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  2174. "Internal: ATTRIBUTE struct corrupted invalid type\n",
  2175. NULL);
  2176. }
  2177. switch (attr->def) {
  2178. case XML_ATTRIBUTE_NONE:
  2179. break;
  2180. case XML_ATTRIBUTE_REQUIRED:
  2181. xmlBufferWriteChar(buf, " #REQUIRED");
  2182. break;
  2183. case XML_ATTRIBUTE_IMPLIED:
  2184. xmlBufferWriteChar(buf, " #IMPLIED");
  2185. break;
  2186. case XML_ATTRIBUTE_FIXED:
  2187. xmlBufferWriteChar(buf, " #FIXED");
  2188. break;
  2189. default:
  2190. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  2191. "Internal: ATTRIBUTE struct corrupted invalid def\n",
  2192. NULL);
  2193. }
  2194. if (attr->defaultValue != NULL) {
  2195. xmlBufferWriteChar(buf, " ");
  2196. xmlBufferWriteQuotedString(buf, attr->defaultValue);
  2197. }
  2198. xmlBufferWriteChar(buf, ">\n");
  2199. }
  2200. /**
  2201. * xmlDumpAttributeDeclScan:
  2202. * @attr: An attribute declaration
  2203. * @buf: the XML buffer output
  2204. *
  2205. * This is used with the hash scan function - just reverses arguments
  2206. */
  2207. static void
  2208. xmlDumpAttributeDeclScan(void *attr, void *buf,
  2209. const xmlChar *name ATTRIBUTE_UNUSED) {
  2210. xmlDumpAttributeDecl((xmlBufferPtr) buf, (xmlAttributePtr) attr);
  2211. }
  2212. /**
  2213. * xmlDumpAttributeTable:
  2214. * @buf: the XML buffer output
  2215. * @table: An attribute table
  2216. *
  2217. * This will dump the content of the attribute table as an XML DTD definition
  2218. */
  2219. void
  2220. xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
  2221. if ((buf == NULL) || (table == NULL))
  2222. return;
  2223. xmlHashScan(table, xmlDumpAttributeDeclScan, buf);
  2224. }
  2225. #endif /* LIBXML_OUTPUT_ENABLED */
  2226. /************************************************************************
  2227. * *
  2228. * NOTATIONs *
  2229. * *
  2230. ************************************************************************/
  2231. /**
  2232. * xmlFreeNotation:
  2233. * @not: A notation
  2234. *
  2235. * Deallocate the memory used by an notation definition
  2236. */
  2237. static void
  2238. xmlFreeNotation(xmlNotationPtr nota) {
  2239. if (nota == NULL) return;
  2240. if (nota->name != NULL)
  2241. xmlFree((xmlChar *) nota->name);
  2242. if (nota->PublicID != NULL)
  2243. xmlFree((xmlChar *) nota->PublicID);
  2244. if (nota->SystemID != NULL)
  2245. xmlFree((xmlChar *) nota->SystemID);
  2246. xmlFree(nota);
  2247. }
  2248. /**
  2249. * xmlAddNotationDecl:
  2250. * @dtd: pointer to the DTD
  2251. * @ctxt: the validation context
  2252. * @name: the entity name
  2253. * @PublicID: the public identifier or NULL
  2254. * @SystemID: the system identifier or NULL
  2255. *
  2256. * Register a new notation declaration
  2257. *
  2258. * Returns NULL if not, otherwise the entity
  2259. */
  2260. xmlNotationPtr
  2261. xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
  2262. const xmlChar *name,
  2263. const xmlChar *PublicID, const xmlChar *SystemID) {
  2264. xmlNotationPtr ret;
  2265. xmlNotationTablePtr table;
  2266. if (dtd == NULL) {
  2267. return(NULL);
  2268. }
  2269. if (name == NULL) {
  2270. return(NULL);
  2271. }
  2272. if ((PublicID == NULL) && (SystemID == NULL)) {
  2273. return(NULL);
  2274. }
  2275. /*
  2276. * Create the Notation table if needed.
  2277. */
  2278. table = (xmlNotationTablePtr) dtd->notations;
  2279. if (table == NULL) {
  2280. xmlDictPtr dict = NULL;
  2281. if (dtd->doc != NULL)
  2282. dict = dtd->doc->dict;
  2283. dtd->notations = table = xmlHashCreateDict(0, dict);
  2284. }
  2285. if (table == NULL) {
  2286. xmlVErrMemory(ctxt,
  2287. "xmlAddNotationDecl: Table creation failed!\n");
  2288. return(NULL);
  2289. }
  2290. ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
  2291. if (ret == NULL) {
  2292. xmlVErrMemory(ctxt, "malloc failed");
  2293. return(NULL);
  2294. }
  2295. memset(ret, 0, sizeof(xmlNotation));
  2296. /*
  2297. * fill the structure.
  2298. */
  2299. ret->name = xmlStrdup(name);
  2300. if (SystemID != NULL)
  2301. ret->SystemID = xmlStrdup(SystemID);
  2302. if (PublicID != NULL)
  2303. ret->PublicID = xmlStrdup(PublicID);
  2304. /*
  2305. * Validity Check:
  2306. * Check the DTD for previous declarations of the ATTLIST
  2307. */
  2308. if (xmlHashAddEntry(table, name, ret)) {
  2309. #ifdef LIBXML_VALID_ENABLED
  2310. xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
  2311. "xmlAddNotationDecl: %s already defined\n",
  2312. (const char *) name);
  2313. #endif /* LIBXML_VALID_ENABLED */
  2314. xmlFreeNotation(ret);
  2315. return(NULL);
  2316. }
  2317. return(ret);
  2318. }
  2319. static void
  2320. xmlFreeNotationTableEntry(void *nota, const xmlChar *name ATTRIBUTE_UNUSED) {
  2321. xmlFreeNotation((xmlNotationPtr) nota);
  2322. }
  2323. /**
  2324. * xmlFreeNotationTable:
  2325. * @table: An notation table
  2326. *
  2327. * Deallocate the memory used by an entities hash table.
  2328. */
  2329. void
  2330. xmlFreeNotationTable(xmlNotationTablePtr table) {
  2331. xmlHashFree(table, xmlFreeNotationTableEntry);
  2332. }
  2333. #ifdef LIBXML_TREE_ENABLED
  2334. /**
  2335. * xmlCopyNotation:
  2336. * @nota: A notation
  2337. *
  2338. * Build a copy of a notation.
  2339. *
  2340. * Returns the new xmlNotationPtr or NULL in case of error.
  2341. */
  2342. static void *
  2343. xmlCopyNotation(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
  2344. xmlNotationPtr nota = (xmlNotationPtr) payload;
  2345. xmlNotationPtr cur;
  2346. cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
  2347. if (cur == NULL) {
  2348. xmlVErrMemory(NULL, "malloc failed");
  2349. return(NULL);
  2350. }
  2351. if (nota->name != NULL)
  2352. cur->name = xmlStrdup(nota->name);
  2353. else
  2354. cur->name = NULL;
  2355. if (nota->PublicID != NULL)
  2356. cur->PublicID = xmlStrdup(nota->PublicID);
  2357. else
  2358. cur->PublicID = NULL;
  2359. if (nota->SystemID != NULL)
  2360. cur->SystemID = xmlStrdup(nota->SystemID);
  2361. else
  2362. cur->SystemID = NULL;
  2363. return(cur);
  2364. }
  2365. /**
  2366. * xmlCopyNotationTable:
  2367. * @table: A notation table
  2368. *
  2369. * Build a copy of a notation table.
  2370. *
  2371. * Returns the new xmlNotationTablePtr or NULL in case of error.
  2372. */
  2373. xmlNotationTablePtr
  2374. xmlCopyNotationTable(xmlNotationTablePtr table) {
  2375. return((xmlNotationTablePtr) xmlHashCopy(table, xmlCopyNotation));
  2376. }
  2377. #endif /* LIBXML_TREE_ENABLED */
  2378. #ifdef LIBXML_OUTPUT_ENABLED
  2379. /**
  2380. * xmlDumpNotationDecl:
  2381. * @buf: the XML buffer output
  2382. * @nota: A notation declaration
  2383. *
  2384. * This will dump the content the notation declaration as an XML DTD definition
  2385. */
  2386. void
  2387. xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
  2388. if ((buf == NULL) || (nota == NULL))
  2389. return;
  2390. xmlBufferWriteChar(buf, "<!NOTATION ");
  2391. xmlBufferWriteCHAR(buf, nota->name);
  2392. if (nota->PublicID != NULL) {
  2393. xmlBufferWriteChar(buf, " PUBLIC ");
  2394. xmlBufferWriteQuotedString(buf, nota->PublicID);
  2395. if (nota->SystemID != NULL) {
  2396. xmlBufferWriteChar(buf, " ");
  2397. xmlBufferWriteQuotedString(buf, nota->SystemID);
  2398. }
  2399. } else {
  2400. xmlBufferWriteChar(buf, " SYSTEM ");
  2401. xmlBufferWriteQuotedString(buf, nota->SystemID);
  2402. }
  2403. xmlBufferWriteChar(buf, " >\n");
  2404. }
  2405. /**
  2406. * xmlDumpNotationDeclScan:
  2407. * @nota: A notation declaration
  2408. * @buf: the XML buffer output
  2409. *
  2410. * This is called with the hash scan function, and just reverses args
  2411. */
  2412. static void
  2413. xmlDumpNotationDeclScan(void *nota, void *buf,
  2414. const xmlChar *name ATTRIBUTE_UNUSED) {
  2415. xmlDumpNotationDecl((xmlBufferPtr) buf, (xmlNotationPtr) nota);
  2416. }
  2417. /**
  2418. * xmlDumpNotationTable:
  2419. * @buf: the XML buffer output
  2420. * @table: A notation table
  2421. *
  2422. * This will dump the content of the notation table as an XML DTD definition
  2423. */
  2424. void
  2425. xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
  2426. if ((buf == NULL) || (table == NULL))
  2427. return;
  2428. xmlHashScan(table, xmlDumpNotationDeclScan, buf);
  2429. }
  2430. #endif /* LIBXML_OUTPUT_ENABLED */
  2431. /************************************************************************
  2432. * *
  2433. * IDs *
  2434. * *
  2435. ************************************************************************/
  2436. /**
  2437. * DICT_FREE:
  2438. * @str: a string
  2439. *
  2440. * Free a string if it is not owned by the "dict" dictionary in the
  2441. * current scope
  2442. */
  2443. #define DICT_FREE(str) \
  2444. if ((str) && ((!dict) || \
  2445. (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
  2446. xmlFree((char *)(str));
  2447. static int
  2448. xmlIsStreaming(xmlValidCtxtPtr ctxt) {
  2449. xmlParserCtxtPtr pctxt;
  2450. if (ctxt == NULL)
  2451. return(0);
  2452. /*
  2453. * These magic values are also abused to detect whether we're validating
  2454. * while parsing a document. In this case, userData points to the parser
  2455. * context.
  2456. */
  2457. if ((ctxt->finishDtd != XML_CTXT_FINISH_DTD_0) &&
  2458. (ctxt->finishDtd != XML_CTXT_FINISH_DTD_1))
  2459. return(0);
  2460. pctxt = ctxt->userData;
  2461. return(pctxt->parseMode == XML_PARSE_READER);
  2462. }
  2463. /**
  2464. * xmlFreeID:
  2465. * @not: A id
  2466. *
  2467. * Deallocate the memory used by an id definition
  2468. */
  2469. static void
  2470. xmlFreeID(xmlIDPtr id) {
  2471. xmlDictPtr dict = NULL;
  2472. if (id == NULL) return;
  2473. if (id->doc != NULL)
  2474. dict = id->doc->dict;
  2475. if (id->value != NULL)
  2476. DICT_FREE(id->value)
  2477. if (id->name != NULL)
  2478. DICT_FREE(id->name)
  2479. xmlFree(id);
  2480. }
  2481. /**
  2482. * xmlAddID:
  2483. * @ctxt: the validation context
  2484. * @doc: pointer to the document
  2485. * @value: the value name
  2486. * @attr: the attribute holding the ID
  2487. *
  2488. * Register a new id declaration
  2489. *
  2490. * Returns NULL if not, otherwise the new xmlIDPtr
  2491. */
  2492. xmlIDPtr
  2493. xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
  2494. xmlAttrPtr attr) {
  2495. xmlIDPtr ret;
  2496. xmlIDTablePtr table;
  2497. if (doc == NULL) {
  2498. return(NULL);
  2499. }
  2500. if ((value == NULL) || (value[0] == 0)) {
  2501. return(NULL);
  2502. }
  2503. if (attr == NULL) {
  2504. return(NULL);
  2505. }
  2506. /*
  2507. * Create the ID table if needed.
  2508. */
  2509. table = (xmlIDTablePtr) doc->ids;
  2510. if (table == NULL) {
  2511. doc->ids = table = xmlHashCreateDict(0, doc->dict);
  2512. }
  2513. if (table == NULL) {
  2514. xmlVErrMemory(ctxt,
  2515. "xmlAddID: Table creation failed!\n");
  2516. return(NULL);
  2517. }
  2518. ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
  2519. if (ret == NULL) {
  2520. xmlVErrMemory(ctxt, "malloc failed");
  2521. return(NULL);
  2522. }
  2523. /*
  2524. * fill the structure.
  2525. */
  2526. ret->value = xmlStrdup(value);
  2527. ret->doc = doc;
  2528. if (xmlIsStreaming(ctxt)) {
  2529. /*
  2530. * Operating in streaming mode, attr is gonna disappear
  2531. */
  2532. if (doc->dict != NULL)
  2533. ret->name = xmlDictLookup(doc->dict, attr->name, -1);
  2534. else
  2535. ret->name = xmlStrdup(attr->name);
  2536. ret->attr = NULL;
  2537. } else {
  2538. ret->attr = attr;
  2539. ret->name = NULL;
  2540. }
  2541. ret->lineno = xmlGetLineNo(attr->parent);
  2542. if (xmlHashAddEntry(table, value, ret) < 0) {
  2543. #ifdef LIBXML_VALID_ENABLED
  2544. /*
  2545. * The id is already defined in this DTD.
  2546. */
  2547. if (ctxt != NULL) {
  2548. xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
  2549. "ID %s already defined\n", value, NULL, NULL);
  2550. }
  2551. #endif /* LIBXML_VALID_ENABLED */
  2552. xmlFreeID(ret);
  2553. return(NULL);
  2554. }
  2555. if (attr != NULL)
  2556. attr->atype = XML_ATTRIBUTE_ID;
  2557. return(ret);
  2558. }
  2559. static void
  2560. xmlFreeIDTableEntry(void *id, const xmlChar *name ATTRIBUTE_UNUSED) {
  2561. xmlFreeID((xmlIDPtr) id);
  2562. }
  2563. /**
  2564. * xmlFreeIDTable:
  2565. * @table: An id table
  2566. *
  2567. * Deallocate the memory used by an ID hash table.
  2568. */
  2569. void
  2570. xmlFreeIDTable(xmlIDTablePtr table) {
  2571. xmlHashFree(table, xmlFreeIDTableEntry);
  2572. }
  2573. /**
  2574. * xmlIsID:
  2575. * @doc: the document
  2576. * @elem: the element carrying the attribute
  2577. * @attr: the attribute
  2578. *
  2579. * Determine whether an attribute is of type ID. In case we have DTD(s)
  2580. * then this is done if DTD loading has been requested. In the case
  2581. * of HTML documents parsed with the HTML parser, then ID detection is
  2582. * done systematically.
  2583. *
  2584. * Returns 0 or 1 depending on the lookup result
  2585. */
  2586. int
  2587. xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
  2588. if ((attr == NULL) || (attr->name == NULL)) return(0);
  2589. if ((attr->ns != NULL) && (attr->ns->prefix != NULL) &&
  2590. (!strcmp((char *) attr->name, "id")) &&
  2591. (!strcmp((char *) attr->ns->prefix, "xml")))
  2592. return(1);
  2593. if (doc == NULL) return(0);
  2594. if ((doc->intSubset == NULL) && (doc->extSubset == NULL) &&
  2595. (doc->type != XML_HTML_DOCUMENT_NODE)) {
  2596. return(0);
  2597. } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
  2598. if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
  2599. ((xmlStrEqual(BAD_CAST "name", attr->name)) &&
  2600. ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a")))))
  2601. return(1);
  2602. return(0);
  2603. } else if (elem == NULL) {
  2604. return(0);
  2605. } else {
  2606. xmlAttributePtr attrDecl = NULL;
  2607. xmlChar felem[50], fattr[50];
  2608. xmlChar *fullelemname, *fullattrname;
  2609. fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ?
  2610. xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) :
  2611. (xmlChar *)elem->name;
  2612. fullattrname = (attr->ns != NULL && attr->ns->prefix != NULL) ?
  2613. xmlBuildQName(attr->name, attr->ns->prefix, fattr, 50) :
  2614. (xmlChar *)attr->name;
  2615. if (fullelemname != NULL && fullattrname != NULL) {
  2616. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullelemname,
  2617. fullattrname);
  2618. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  2619. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullelemname,
  2620. fullattrname);
  2621. }
  2622. if ((fullattrname != fattr) && (fullattrname != attr->name))
  2623. xmlFree(fullattrname);
  2624. if ((fullelemname != felem) && (fullelemname != elem->name))
  2625. xmlFree(fullelemname);
  2626. if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
  2627. return(1);
  2628. }
  2629. return(0);
  2630. }
  2631. /**
  2632. * xmlRemoveID:
  2633. * @doc: the document
  2634. * @attr: the attribute
  2635. *
  2636. * Remove the given attribute from the ID table maintained internally.
  2637. *
  2638. * Returns -1 if the lookup failed and 0 otherwise
  2639. */
  2640. int
  2641. xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
  2642. xmlIDTablePtr table;
  2643. xmlIDPtr id;
  2644. xmlChar *ID;
  2645. if (doc == NULL) return(-1);
  2646. if (attr == NULL) return(-1);
  2647. table = (xmlIDTablePtr) doc->ids;
  2648. if (table == NULL)
  2649. return(-1);
  2650. ID = xmlNodeListGetString(doc, attr->children, 1);
  2651. if (ID == NULL)
  2652. return(-1);
  2653. xmlValidNormalizeString(ID);
  2654. id = xmlHashLookup(table, ID);
  2655. if (id == NULL || id->attr != attr) {
  2656. xmlFree(ID);
  2657. return(-1);
  2658. }
  2659. xmlHashRemoveEntry(table, ID, xmlFreeIDTableEntry);
  2660. xmlFree(ID);
  2661. attr->atype = 0;
  2662. return(0);
  2663. }
  2664. /**
  2665. * xmlGetID:
  2666. * @doc: pointer to the document
  2667. * @ID: the ID value
  2668. *
  2669. * Search the attribute declaring the given ID
  2670. *
  2671. * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
  2672. */
  2673. xmlAttrPtr
  2674. xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
  2675. xmlIDTablePtr table;
  2676. xmlIDPtr id;
  2677. if (doc == NULL) {
  2678. return(NULL);
  2679. }
  2680. if (ID == NULL) {
  2681. return(NULL);
  2682. }
  2683. table = (xmlIDTablePtr) doc->ids;
  2684. if (table == NULL)
  2685. return(NULL);
  2686. id = xmlHashLookup(table, ID);
  2687. if (id == NULL)
  2688. return(NULL);
  2689. if (id->attr == NULL) {
  2690. /*
  2691. * We are operating on a stream, return a well known reference
  2692. * since the attribute node doesn't exist anymore
  2693. */
  2694. return((xmlAttrPtr) doc);
  2695. }
  2696. return(id->attr);
  2697. }
  2698. /************************************************************************
  2699. * *
  2700. * Refs *
  2701. * *
  2702. ************************************************************************/
  2703. typedef struct xmlRemoveMemo_t
  2704. {
  2705. xmlListPtr l;
  2706. xmlAttrPtr ap;
  2707. } xmlRemoveMemo;
  2708. typedef xmlRemoveMemo *xmlRemoveMemoPtr;
  2709. typedef struct xmlValidateMemo_t
  2710. {
  2711. xmlValidCtxtPtr ctxt;
  2712. const xmlChar *name;
  2713. } xmlValidateMemo;
  2714. typedef xmlValidateMemo *xmlValidateMemoPtr;
  2715. /**
  2716. * xmlFreeRef:
  2717. * @lk: A list link
  2718. *
  2719. * Deallocate the memory used by a ref definition
  2720. */
  2721. static void
  2722. xmlFreeRef(xmlLinkPtr lk) {
  2723. xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
  2724. if (ref == NULL) return;
  2725. if (ref->value != NULL)
  2726. xmlFree((xmlChar *)ref->value);
  2727. if (ref->name != NULL)
  2728. xmlFree((xmlChar *)ref->name);
  2729. xmlFree(ref);
  2730. }
  2731. /**
  2732. * xmlFreeRefTableEntry:
  2733. * @list_ref: A list of references.
  2734. *
  2735. * Deallocate the memory used by a list of references
  2736. */
  2737. static void
  2738. xmlFreeRefTableEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
  2739. xmlListPtr list_ref = (xmlListPtr) payload;
  2740. if (list_ref == NULL) return;
  2741. xmlListDelete(list_ref);
  2742. }
  2743. /**
  2744. * xmlWalkRemoveRef:
  2745. * @data: Contents of current link
  2746. * @user: Value supplied by the user
  2747. *
  2748. * Returns 0 to abort the walk or 1 to continue
  2749. */
  2750. static int
  2751. xmlWalkRemoveRef(const void *data, void *user)
  2752. {
  2753. xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
  2754. xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
  2755. xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
  2756. if (attr0 == attr1) { /* Matched: remove and terminate walk */
  2757. xmlListRemoveFirst(ref_list, (void *)data);
  2758. return 0;
  2759. }
  2760. return 1;
  2761. }
  2762. /**
  2763. * xmlDummyCompare
  2764. * @data0: Value supplied by the user
  2765. * @data1: Value supplied by the user
  2766. *
  2767. * Do nothing, return 0. Used to create unordered lists.
  2768. */
  2769. static int
  2770. xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
  2771. const void *data1 ATTRIBUTE_UNUSED)
  2772. {
  2773. return (0);
  2774. }
  2775. /**
  2776. * xmlAddRef:
  2777. * @ctxt: the validation context
  2778. * @doc: pointer to the document
  2779. * @value: the value name
  2780. * @attr: the attribute holding the Ref
  2781. *
  2782. * Register a new ref declaration
  2783. *
  2784. * Returns NULL if not, otherwise the new xmlRefPtr
  2785. */
  2786. xmlRefPtr
  2787. xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
  2788. xmlAttrPtr attr) {
  2789. xmlRefPtr ret;
  2790. xmlRefTablePtr table;
  2791. xmlListPtr ref_list;
  2792. if (doc == NULL) {
  2793. return(NULL);
  2794. }
  2795. if (value == NULL) {
  2796. return(NULL);
  2797. }
  2798. if (attr == NULL) {
  2799. return(NULL);
  2800. }
  2801. /*
  2802. * Create the Ref table if needed.
  2803. */
  2804. table = (xmlRefTablePtr) doc->refs;
  2805. if (table == NULL) {
  2806. doc->refs = table = xmlHashCreateDict(0, doc->dict);
  2807. }
  2808. if (table == NULL) {
  2809. xmlVErrMemory(ctxt,
  2810. "xmlAddRef: Table creation failed!\n");
  2811. return(NULL);
  2812. }
  2813. ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
  2814. if (ret == NULL) {
  2815. xmlVErrMemory(ctxt, "malloc failed");
  2816. return(NULL);
  2817. }
  2818. /*
  2819. * fill the structure.
  2820. */
  2821. ret->value = xmlStrdup(value);
  2822. if (xmlIsStreaming(ctxt)) {
  2823. /*
  2824. * Operating in streaming mode, attr is gonna disappear
  2825. */
  2826. ret->name = xmlStrdup(attr->name);
  2827. ret->attr = NULL;
  2828. } else {
  2829. ret->name = NULL;
  2830. ret->attr = attr;
  2831. }
  2832. ret->lineno = xmlGetLineNo(attr->parent);
  2833. /* To add a reference :-
  2834. * References are maintained as a list of references,
  2835. * Lookup the entry, if no entry create new nodelist
  2836. * Add the owning node to the NodeList
  2837. * Return the ref
  2838. */
  2839. if (NULL == (ref_list = xmlHashLookup(table, value))) {
  2840. if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
  2841. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  2842. "xmlAddRef: Reference list creation failed!\n",
  2843. NULL);
  2844. goto failed;
  2845. }
  2846. if (xmlHashAddEntry(table, value, ref_list) < 0) {
  2847. xmlListDelete(ref_list);
  2848. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  2849. "xmlAddRef: Reference list insertion failed!\n",
  2850. NULL);
  2851. goto failed;
  2852. }
  2853. }
  2854. if (xmlListAppend(ref_list, ret) != 0) {
  2855. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  2856. "xmlAddRef: Reference list insertion failed!\n",
  2857. NULL);
  2858. goto failed;
  2859. }
  2860. return(ret);
  2861. failed:
  2862. if (ret != NULL) {
  2863. if (ret->value != NULL)
  2864. xmlFree((char *)ret->value);
  2865. if (ret->name != NULL)
  2866. xmlFree((char *)ret->name);
  2867. xmlFree(ret);
  2868. }
  2869. return(NULL);
  2870. }
  2871. /**
  2872. * xmlFreeRefTable:
  2873. * @table: An ref table
  2874. *
  2875. * Deallocate the memory used by an Ref hash table.
  2876. */
  2877. void
  2878. xmlFreeRefTable(xmlRefTablePtr table) {
  2879. xmlHashFree(table, xmlFreeRefTableEntry);
  2880. }
  2881. /**
  2882. * xmlIsRef:
  2883. * @doc: the document
  2884. * @elem: the element carrying the attribute
  2885. * @attr: the attribute
  2886. *
  2887. * Determine whether an attribute is of type Ref. In case we have DTD(s)
  2888. * then this is simple, otherwise we use an heuristic: name Ref (upper
  2889. * or lowercase).
  2890. *
  2891. * Returns 0 or 1 depending on the lookup result
  2892. */
  2893. int
  2894. xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
  2895. if (attr == NULL)
  2896. return(0);
  2897. if (doc == NULL) {
  2898. doc = attr->doc;
  2899. if (doc == NULL) return(0);
  2900. }
  2901. if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
  2902. return(0);
  2903. } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
  2904. /* TODO @@@ */
  2905. return(0);
  2906. } else {
  2907. xmlAttributePtr attrDecl;
  2908. if (elem == NULL) return(0);
  2909. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
  2910. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  2911. attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
  2912. elem->name, attr->name);
  2913. if ((attrDecl != NULL) &&
  2914. (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
  2915. attrDecl->atype == XML_ATTRIBUTE_IDREFS))
  2916. return(1);
  2917. }
  2918. return(0);
  2919. }
  2920. /**
  2921. * xmlRemoveRef:
  2922. * @doc: the document
  2923. * @attr: the attribute
  2924. *
  2925. * Remove the given attribute from the Ref table maintained internally.
  2926. *
  2927. * Returns -1 if the lookup failed and 0 otherwise
  2928. */
  2929. int
  2930. xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
  2931. xmlListPtr ref_list;
  2932. xmlRefTablePtr table;
  2933. xmlChar *ID;
  2934. xmlRemoveMemo target;
  2935. if (doc == NULL) return(-1);
  2936. if (attr == NULL) return(-1);
  2937. table = (xmlRefTablePtr) doc->refs;
  2938. if (table == NULL)
  2939. return(-1);
  2940. ID = xmlNodeListGetString(doc, attr->children, 1);
  2941. if (ID == NULL)
  2942. return(-1);
  2943. ref_list = xmlHashLookup(table, ID);
  2944. if(ref_list == NULL) {
  2945. xmlFree(ID);
  2946. return (-1);
  2947. }
  2948. /* At this point, ref_list refers to a list of references which
  2949. * have the same key as the supplied attr. Our list of references
  2950. * is ordered by reference address and we don't have that information
  2951. * here to use when removing. We'll have to walk the list and
  2952. * check for a matching attribute, when we find one stop the walk
  2953. * and remove the entry.
  2954. * The list is ordered by reference, so that means we don't have the
  2955. * key. Passing the list and the reference to the walker means we
  2956. * will have enough data to be able to remove the entry.
  2957. */
  2958. target.l = ref_list;
  2959. target.ap = attr;
  2960. /* Remove the supplied attr from our list */
  2961. xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
  2962. /*If the list is empty then remove the list entry in the hash */
  2963. if (xmlListEmpty(ref_list))
  2964. xmlHashUpdateEntry(table, ID, NULL, xmlFreeRefTableEntry);
  2965. xmlFree(ID);
  2966. return(0);
  2967. }
  2968. /**
  2969. * xmlGetRefs:
  2970. * @doc: pointer to the document
  2971. * @ID: the ID value
  2972. *
  2973. * Find the set of references for the supplied ID.
  2974. *
  2975. * Returns NULL if not found, otherwise node set for the ID.
  2976. */
  2977. xmlListPtr
  2978. xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
  2979. xmlRefTablePtr table;
  2980. if (doc == NULL) {
  2981. return(NULL);
  2982. }
  2983. if (ID == NULL) {
  2984. return(NULL);
  2985. }
  2986. table = (xmlRefTablePtr) doc->refs;
  2987. if (table == NULL)
  2988. return(NULL);
  2989. return (xmlHashLookup(table, ID));
  2990. }
  2991. /************************************************************************
  2992. * *
  2993. * Routines for validity checking *
  2994. * *
  2995. ************************************************************************/
  2996. /**
  2997. * xmlGetDtdElementDesc:
  2998. * @dtd: a pointer to the DtD to search
  2999. * @name: the element name
  3000. *
  3001. * Search the DTD for the description of this element
  3002. *
  3003. * returns the xmlElementPtr if found or NULL
  3004. */
  3005. xmlElementPtr
  3006. xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
  3007. xmlElementTablePtr table;
  3008. xmlElementPtr cur;
  3009. xmlChar *uqname = NULL, *prefix = NULL;
  3010. if ((dtd == NULL) || (name == NULL)) return(NULL);
  3011. if (dtd->elements == NULL)
  3012. return(NULL);
  3013. table = (xmlElementTablePtr) dtd->elements;
  3014. uqname = xmlSplitQName2(name, &prefix);
  3015. if (uqname != NULL)
  3016. name = uqname;
  3017. cur = xmlHashLookup2(table, name, prefix);
  3018. if (prefix != NULL) xmlFree(prefix);
  3019. if (uqname != NULL) xmlFree(uqname);
  3020. return(cur);
  3021. }
  3022. /**
  3023. * xmlGetDtdElementDesc2:
  3024. * @dtd: a pointer to the DtD to search
  3025. * @name: the element name
  3026. * @create: create an empty description if not found
  3027. *
  3028. * Search the DTD for the description of this element
  3029. *
  3030. * returns the xmlElementPtr if found or NULL
  3031. */
  3032. static xmlElementPtr
  3033. xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
  3034. xmlElementTablePtr table;
  3035. xmlElementPtr cur;
  3036. xmlChar *uqname = NULL, *prefix = NULL;
  3037. if (dtd == NULL) return(NULL);
  3038. if (dtd->elements == NULL) {
  3039. xmlDictPtr dict = NULL;
  3040. if (dtd->doc != NULL)
  3041. dict = dtd->doc->dict;
  3042. if (!create)
  3043. return(NULL);
  3044. /*
  3045. * Create the Element table if needed.
  3046. */
  3047. table = (xmlElementTablePtr) dtd->elements;
  3048. if (table == NULL) {
  3049. table = xmlHashCreateDict(0, dict);
  3050. dtd->elements = (void *) table;
  3051. }
  3052. if (table == NULL) {
  3053. xmlVErrMemory(NULL, "element table allocation failed");
  3054. return(NULL);
  3055. }
  3056. }
  3057. table = (xmlElementTablePtr) dtd->elements;
  3058. uqname = xmlSplitQName2(name, &prefix);
  3059. if (uqname != NULL)
  3060. name = uqname;
  3061. cur = xmlHashLookup2(table, name, prefix);
  3062. if ((cur == NULL) && (create)) {
  3063. cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
  3064. if (cur == NULL) {
  3065. xmlVErrMemory(NULL, "malloc failed");
  3066. return(NULL);
  3067. }
  3068. memset(cur, 0, sizeof(xmlElement));
  3069. cur->type = XML_ELEMENT_DECL;
  3070. /*
  3071. * fill the structure.
  3072. */
  3073. cur->name = xmlStrdup(name);
  3074. cur->prefix = xmlStrdup(prefix);
  3075. cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
  3076. xmlHashAddEntry2(table, name, prefix, cur);
  3077. }
  3078. if (prefix != NULL) xmlFree(prefix);
  3079. if (uqname != NULL) xmlFree(uqname);
  3080. return(cur);
  3081. }
  3082. /**
  3083. * xmlGetDtdQElementDesc:
  3084. * @dtd: a pointer to the DtD to search
  3085. * @name: the element name
  3086. * @prefix: the element namespace prefix
  3087. *
  3088. * Search the DTD for the description of this element
  3089. *
  3090. * returns the xmlElementPtr if found or NULL
  3091. */
  3092. xmlElementPtr
  3093. xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
  3094. const xmlChar *prefix) {
  3095. xmlElementTablePtr table;
  3096. if (dtd == NULL) return(NULL);
  3097. if (dtd->elements == NULL) return(NULL);
  3098. table = (xmlElementTablePtr) dtd->elements;
  3099. return(xmlHashLookup2(table, name, prefix));
  3100. }
  3101. /**
  3102. * xmlGetDtdAttrDesc:
  3103. * @dtd: a pointer to the DtD to search
  3104. * @elem: the element name
  3105. * @name: the attribute name
  3106. *
  3107. * Search the DTD for the description of this attribute on
  3108. * this element.
  3109. *
  3110. * returns the xmlAttributePtr if found or NULL
  3111. */
  3112. xmlAttributePtr
  3113. xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
  3114. xmlAttributeTablePtr table;
  3115. xmlAttributePtr cur;
  3116. xmlChar *uqname = NULL, *prefix = NULL;
  3117. if (dtd == NULL) return(NULL);
  3118. if (dtd->attributes == NULL) return(NULL);
  3119. table = (xmlAttributeTablePtr) dtd->attributes;
  3120. if (table == NULL)
  3121. return(NULL);
  3122. uqname = xmlSplitQName2(name, &prefix);
  3123. if (uqname != NULL) {
  3124. cur = xmlHashLookup3(table, uqname, prefix, elem);
  3125. if (prefix != NULL) xmlFree(prefix);
  3126. if (uqname != NULL) xmlFree(uqname);
  3127. } else
  3128. cur = xmlHashLookup3(table, name, NULL, elem);
  3129. return(cur);
  3130. }
  3131. /**
  3132. * xmlGetDtdQAttrDesc:
  3133. * @dtd: a pointer to the DtD to search
  3134. * @elem: the element name
  3135. * @name: the attribute name
  3136. * @prefix: the attribute namespace prefix
  3137. *
  3138. * Search the DTD for the description of this qualified attribute on
  3139. * this element.
  3140. *
  3141. * returns the xmlAttributePtr if found or NULL
  3142. */
  3143. xmlAttributePtr
  3144. xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
  3145. const xmlChar *prefix) {
  3146. xmlAttributeTablePtr table;
  3147. if (dtd == NULL) return(NULL);
  3148. if (dtd->attributes == NULL) return(NULL);
  3149. table = (xmlAttributeTablePtr) dtd->attributes;
  3150. return(xmlHashLookup3(table, name, prefix, elem));
  3151. }
  3152. /**
  3153. * xmlGetDtdNotationDesc:
  3154. * @dtd: a pointer to the DtD to search
  3155. * @name: the notation name
  3156. *
  3157. * Search the DTD for the description of this notation
  3158. *
  3159. * returns the xmlNotationPtr if found or NULL
  3160. */
  3161. xmlNotationPtr
  3162. xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
  3163. xmlNotationTablePtr table;
  3164. if (dtd == NULL) return(NULL);
  3165. if (dtd->notations == NULL) return(NULL);
  3166. table = (xmlNotationTablePtr) dtd->notations;
  3167. return(xmlHashLookup(table, name));
  3168. }
  3169. #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  3170. /**
  3171. * xmlValidateNotationUse:
  3172. * @ctxt: the validation context
  3173. * @doc: the document
  3174. * @notationName: the notation name to check
  3175. *
  3176. * Validate that the given name match a notation declaration.
  3177. * - [ VC: Notation Declared ]
  3178. *
  3179. * returns 1 if valid or 0 otherwise
  3180. */
  3181. int
  3182. xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3183. const xmlChar *notationName) {
  3184. xmlNotationPtr notaDecl;
  3185. if ((doc == NULL) || (doc->intSubset == NULL) ||
  3186. (notationName == NULL)) return(-1);
  3187. notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
  3188. if ((notaDecl == NULL) && (doc->extSubset != NULL))
  3189. notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
  3190. if ((notaDecl == NULL) && (ctxt != NULL)) {
  3191. xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
  3192. "NOTATION %s is not declared\n",
  3193. notationName, NULL, NULL);
  3194. return(0);
  3195. }
  3196. return(1);
  3197. }
  3198. #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
  3199. /**
  3200. * xmlIsMixedElement:
  3201. * @doc: the document
  3202. * @name: the element name
  3203. *
  3204. * Search in the DtDs whether an element accept Mixed content (or ANY)
  3205. * basically if it is supposed to accept text childs
  3206. *
  3207. * returns 0 if no, 1 if yes, and -1 if no element description is available
  3208. */
  3209. int
  3210. xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
  3211. xmlElementPtr elemDecl;
  3212. if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
  3213. elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
  3214. if ((elemDecl == NULL) && (doc->extSubset != NULL))
  3215. elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
  3216. if (elemDecl == NULL) return(-1);
  3217. switch (elemDecl->etype) {
  3218. case XML_ELEMENT_TYPE_UNDEFINED:
  3219. return(-1);
  3220. case XML_ELEMENT_TYPE_ELEMENT:
  3221. return(0);
  3222. case XML_ELEMENT_TYPE_EMPTY:
  3223. /*
  3224. * return 1 for EMPTY since we want VC error to pop up
  3225. * on <empty> </empty> for example
  3226. */
  3227. case XML_ELEMENT_TYPE_ANY:
  3228. case XML_ELEMENT_TYPE_MIXED:
  3229. return(1);
  3230. }
  3231. return(1);
  3232. }
  3233. #ifdef LIBXML_VALID_ENABLED
  3234. static int
  3235. xmlIsDocNameStartChar(xmlDocPtr doc, int c) {
  3236. if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
  3237. /*
  3238. * Use the new checks of production [4] [4a] amd [5] of the
  3239. * Update 5 of XML-1.0
  3240. */
  3241. if (((c >= 'a') && (c <= 'z')) ||
  3242. ((c >= 'A') && (c <= 'Z')) ||
  3243. (c == '_') || (c == ':') ||
  3244. ((c >= 0xC0) && (c <= 0xD6)) ||
  3245. ((c >= 0xD8) && (c <= 0xF6)) ||
  3246. ((c >= 0xF8) && (c <= 0x2FF)) ||
  3247. ((c >= 0x370) && (c <= 0x37D)) ||
  3248. ((c >= 0x37F) && (c <= 0x1FFF)) ||
  3249. ((c >= 0x200C) && (c <= 0x200D)) ||
  3250. ((c >= 0x2070) && (c <= 0x218F)) ||
  3251. ((c >= 0x2C00) && (c <= 0x2FEF)) ||
  3252. ((c >= 0x3001) && (c <= 0xD7FF)) ||
  3253. ((c >= 0xF900) && (c <= 0xFDCF)) ||
  3254. ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
  3255. ((c >= 0x10000) && (c <= 0xEFFFF)))
  3256. return(1);
  3257. } else {
  3258. if (IS_LETTER(c) || (c == '_') || (c == ':'))
  3259. return(1);
  3260. }
  3261. return(0);
  3262. }
  3263. static int
  3264. xmlIsDocNameChar(xmlDocPtr doc, int c) {
  3265. if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
  3266. /*
  3267. * Use the new checks of production [4] [4a] amd [5] of the
  3268. * Update 5 of XML-1.0
  3269. */
  3270. if (((c >= 'a') && (c <= 'z')) ||
  3271. ((c >= 'A') && (c <= 'Z')) ||
  3272. ((c >= '0') && (c <= '9')) || /* !start */
  3273. (c == '_') || (c == ':') ||
  3274. (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
  3275. ((c >= 0xC0) && (c <= 0xD6)) ||
  3276. ((c >= 0xD8) && (c <= 0xF6)) ||
  3277. ((c >= 0xF8) && (c <= 0x2FF)) ||
  3278. ((c >= 0x300) && (c <= 0x36F)) || /* !start */
  3279. ((c >= 0x370) && (c <= 0x37D)) ||
  3280. ((c >= 0x37F) && (c <= 0x1FFF)) ||
  3281. ((c >= 0x200C) && (c <= 0x200D)) ||
  3282. ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
  3283. ((c >= 0x2070) && (c <= 0x218F)) ||
  3284. ((c >= 0x2C00) && (c <= 0x2FEF)) ||
  3285. ((c >= 0x3001) && (c <= 0xD7FF)) ||
  3286. ((c >= 0xF900) && (c <= 0xFDCF)) ||
  3287. ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
  3288. ((c >= 0x10000) && (c <= 0xEFFFF)))
  3289. return(1);
  3290. } else {
  3291. if ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
  3292. (c == '.') || (c == '-') ||
  3293. (c == '_') || (c == ':') ||
  3294. (IS_COMBINING(c)) ||
  3295. (IS_EXTENDER(c)))
  3296. return(1);
  3297. }
  3298. return(0);
  3299. }
  3300. /**
  3301. * xmlValidateNameValue:
  3302. * @doc: pointer to the document or NULL
  3303. * @value: an Name value
  3304. *
  3305. * Validate that the given value match Name production
  3306. *
  3307. * returns 1 if valid or 0 otherwise
  3308. */
  3309. static int
  3310. xmlValidateNameValueInternal(xmlDocPtr doc, const xmlChar *value) {
  3311. const xmlChar *cur;
  3312. int val, len;
  3313. if (value == NULL) return(0);
  3314. cur = value;
  3315. val = xmlStringCurrentChar(NULL, cur, &len);
  3316. cur += len;
  3317. if (!xmlIsDocNameStartChar(doc, val))
  3318. return(0);
  3319. val = xmlStringCurrentChar(NULL, cur, &len);
  3320. cur += len;
  3321. while (xmlIsDocNameChar(doc, val)) {
  3322. val = xmlStringCurrentChar(NULL, cur, &len);
  3323. cur += len;
  3324. }
  3325. if (val != 0) return(0);
  3326. return(1);
  3327. }
  3328. /**
  3329. * xmlValidateNameValue:
  3330. * @value: an Name value
  3331. *
  3332. * Validate that the given value match Name production
  3333. *
  3334. * returns 1 if valid or 0 otherwise
  3335. */
  3336. int
  3337. xmlValidateNameValue(const xmlChar *value) {
  3338. return(xmlValidateNameValueInternal(NULL, value));
  3339. }
  3340. /**
  3341. * xmlValidateNamesValueInternal:
  3342. * @doc: pointer to the document or NULL
  3343. * @value: an Names value
  3344. *
  3345. * Validate that the given value match Names production
  3346. *
  3347. * returns 1 if valid or 0 otherwise
  3348. */
  3349. static int
  3350. xmlValidateNamesValueInternal(xmlDocPtr doc, const xmlChar *value) {
  3351. const xmlChar *cur;
  3352. int val, len;
  3353. if (value == NULL) return(0);
  3354. cur = value;
  3355. val = xmlStringCurrentChar(NULL, cur, &len);
  3356. cur += len;
  3357. if (!xmlIsDocNameStartChar(doc, val))
  3358. return(0);
  3359. val = xmlStringCurrentChar(NULL, cur, &len);
  3360. cur += len;
  3361. while (xmlIsDocNameChar(doc, val)) {
  3362. val = xmlStringCurrentChar(NULL, cur, &len);
  3363. cur += len;
  3364. }
  3365. /* Should not test IS_BLANK(val) here -- see erratum E20*/
  3366. while (val == 0x20) {
  3367. while (val == 0x20) {
  3368. val = xmlStringCurrentChar(NULL, cur, &len);
  3369. cur += len;
  3370. }
  3371. if (!xmlIsDocNameStartChar(doc, val))
  3372. return(0);
  3373. val = xmlStringCurrentChar(NULL, cur, &len);
  3374. cur += len;
  3375. while (xmlIsDocNameChar(doc, val)) {
  3376. val = xmlStringCurrentChar(NULL, cur, &len);
  3377. cur += len;
  3378. }
  3379. }
  3380. if (val != 0) return(0);
  3381. return(1);
  3382. }
  3383. /**
  3384. * xmlValidateNamesValue:
  3385. * @value: an Names value
  3386. *
  3387. * Validate that the given value match Names production
  3388. *
  3389. * returns 1 if valid or 0 otherwise
  3390. */
  3391. int
  3392. xmlValidateNamesValue(const xmlChar *value) {
  3393. return(xmlValidateNamesValueInternal(NULL, value));
  3394. }
  3395. /**
  3396. * xmlValidateNmtokenValueInternal:
  3397. * @doc: pointer to the document or NULL
  3398. * @value: an Nmtoken value
  3399. *
  3400. * Validate that the given value match Nmtoken production
  3401. *
  3402. * [ VC: Name Token ]
  3403. *
  3404. * returns 1 if valid or 0 otherwise
  3405. */
  3406. static int
  3407. xmlValidateNmtokenValueInternal(xmlDocPtr doc, const xmlChar *value) {
  3408. const xmlChar *cur;
  3409. int val, len;
  3410. if (value == NULL) return(0);
  3411. cur = value;
  3412. val = xmlStringCurrentChar(NULL, cur, &len);
  3413. cur += len;
  3414. if (!xmlIsDocNameChar(doc, val))
  3415. return(0);
  3416. val = xmlStringCurrentChar(NULL, cur, &len);
  3417. cur += len;
  3418. while (xmlIsDocNameChar(doc, val)) {
  3419. val = xmlStringCurrentChar(NULL, cur, &len);
  3420. cur += len;
  3421. }
  3422. if (val != 0) return(0);
  3423. return(1);
  3424. }
  3425. /**
  3426. * xmlValidateNmtokenValue:
  3427. * @value: an Nmtoken value
  3428. *
  3429. * Validate that the given value match Nmtoken production
  3430. *
  3431. * [ VC: Name Token ]
  3432. *
  3433. * returns 1 if valid or 0 otherwise
  3434. */
  3435. int
  3436. xmlValidateNmtokenValue(const xmlChar *value) {
  3437. return(xmlValidateNmtokenValueInternal(NULL, value));
  3438. }
  3439. /**
  3440. * xmlValidateNmtokensValueInternal:
  3441. * @doc: pointer to the document or NULL
  3442. * @value: an Nmtokens value
  3443. *
  3444. * Validate that the given value match Nmtokens production
  3445. *
  3446. * [ VC: Name Token ]
  3447. *
  3448. * returns 1 if valid or 0 otherwise
  3449. */
  3450. static int
  3451. xmlValidateNmtokensValueInternal(xmlDocPtr doc, const xmlChar *value) {
  3452. const xmlChar *cur;
  3453. int val, len;
  3454. if (value == NULL) return(0);
  3455. cur = value;
  3456. val = xmlStringCurrentChar(NULL, cur, &len);
  3457. cur += len;
  3458. while (IS_BLANK(val)) {
  3459. val = xmlStringCurrentChar(NULL, cur, &len);
  3460. cur += len;
  3461. }
  3462. if (!xmlIsDocNameChar(doc, val))
  3463. return(0);
  3464. while (xmlIsDocNameChar(doc, val)) {
  3465. val = xmlStringCurrentChar(NULL, cur, &len);
  3466. cur += len;
  3467. }
  3468. /* Should not test IS_BLANK(val) here -- see erratum E20*/
  3469. while (val == 0x20) {
  3470. while (val == 0x20) {
  3471. val = xmlStringCurrentChar(NULL, cur, &len);
  3472. cur += len;
  3473. }
  3474. if (val == 0) return(1);
  3475. if (!xmlIsDocNameChar(doc, val))
  3476. return(0);
  3477. val = xmlStringCurrentChar(NULL, cur, &len);
  3478. cur += len;
  3479. while (xmlIsDocNameChar(doc, val)) {
  3480. val = xmlStringCurrentChar(NULL, cur, &len);
  3481. cur += len;
  3482. }
  3483. }
  3484. if (val != 0) return(0);
  3485. return(1);
  3486. }
  3487. /**
  3488. * xmlValidateNmtokensValue:
  3489. * @value: an Nmtokens value
  3490. *
  3491. * Validate that the given value match Nmtokens production
  3492. *
  3493. * [ VC: Name Token ]
  3494. *
  3495. * returns 1 if valid or 0 otherwise
  3496. */
  3497. int
  3498. xmlValidateNmtokensValue(const xmlChar *value) {
  3499. return(xmlValidateNmtokensValueInternal(NULL, value));
  3500. }
  3501. /**
  3502. * xmlValidateNotationDecl:
  3503. * @ctxt: the validation context
  3504. * @doc: a document instance
  3505. * @nota: a notation definition
  3506. *
  3507. * Try to validate a single notation definition
  3508. * basically it does the following checks as described by the
  3509. * XML-1.0 recommendation:
  3510. * - it seems that no validity constraint exists on notation declarations
  3511. * But this function get called anyway ...
  3512. *
  3513. * returns 1 if valid or 0 otherwise
  3514. */
  3515. int
  3516. xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
  3517. xmlNotationPtr nota ATTRIBUTE_UNUSED) {
  3518. int ret = 1;
  3519. return(ret);
  3520. }
  3521. /**
  3522. * xmlValidateAttributeValueInternal:
  3523. * @doc: the document
  3524. * @type: an attribute type
  3525. * @value: an attribute value
  3526. *
  3527. * Validate that the given attribute value match the proper production
  3528. *
  3529. * returns 1 if valid or 0 otherwise
  3530. */
  3531. static int
  3532. xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
  3533. const xmlChar *value) {
  3534. switch (type) {
  3535. case XML_ATTRIBUTE_ENTITIES:
  3536. case XML_ATTRIBUTE_IDREFS:
  3537. return(xmlValidateNamesValueInternal(doc, value));
  3538. case XML_ATTRIBUTE_ENTITY:
  3539. case XML_ATTRIBUTE_IDREF:
  3540. case XML_ATTRIBUTE_ID:
  3541. case XML_ATTRIBUTE_NOTATION:
  3542. return(xmlValidateNameValueInternal(doc, value));
  3543. case XML_ATTRIBUTE_NMTOKENS:
  3544. case XML_ATTRIBUTE_ENUMERATION:
  3545. return(xmlValidateNmtokensValueInternal(doc, value));
  3546. case XML_ATTRIBUTE_NMTOKEN:
  3547. return(xmlValidateNmtokenValueInternal(doc, value));
  3548. case XML_ATTRIBUTE_CDATA:
  3549. break;
  3550. }
  3551. return(1);
  3552. }
  3553. /**
  3554. * xmlValidateAttributeValue:
  3555. * @type: an attribute type
  3556. * @value: an attribute value
  3557. *
  3558. * Validate that the given attribute value match the proper production
  3559. *
  3560. * [ VC: ID ]
  3561. * Values of type ID must match the Name production....
  3562. *
  3563. * [ VC: IDREF ]
  3564. * Values of type IDREF must match the Name production, and values
  3565. * of type IDREFS must match Names ...
  3566. *
  3567. * [ VC: Entity Name ]
  3568. * Values of type ENTITY must match the Name production, values
  3569. * of type ENTITIES must match Names ...
  3570. *
  3571. * [ VC: Name Token ]
  3572. * Values of type NMTOKEN must match the Nmtoken production; values
  3573. * of type NMTOKENS must match Nmtokens.
  3574. *
  3575. * returns 1 if valid or 0 otherwise
  3576. */
  3577. int
  3578. xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
  3579. return(xmlValidateAttributeValueInternal(NULL, type, value));
  3580. }
  3581. /**
  3582. * xmlValidateAttributeValue2:
  3583. * @ctxt: the validation context
  3584. * @doc: the document
  3585. * @name: the attribute name (used for error reporting only)
  3586. * @type: the attribute type
  3587. * @value: the attribute value
  3588. *
  3589. * Validate that the given attribute value match a given type.
  3590. * This typically cannot be done before having finished parsing
  3591. * the subsets.
  3592. *
  3593. * [ VC: IDREF ]
  3594. * Values of type IDREF must match one of the declared IDs
  3595. * Values of type IDREFS must match a sequence of the declared IDs
  3596. * each Name must match the value of an ID attribute on some element
  3597. * in the XML document; i.e. IDREF values must match the value of
  3598. * some ID attribute
  3599. *
  3600. * [ VC: Entity Name ]
  3601. * Values of type ENTITY must match one declared entity
  3602. * Values of type ENTITIES must match a sequence of declared entities
  3603. *
  3604. * [ VC: Notation Attributes ]
  3605. * all notation names in the declaration must be declared.
  3606. *
  3607. * returns 1 if valid or 0 otherwise
  3608. */
  3609. static int
  3610. xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3611. const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
  3612. int ret = 1;
  3613. switch (type) {
  3614. case XML_ATTRIBUTE_IDREFS:
  3615. case XML_ATTRIBUTE_IDREF:
  3616. case XML_ATTRIBUTE_ID:
  3617. case XML_ATTRIBUTE_NMTOKENS:
  3618. case XML_ATTRIBUTE_ENUMERATION:
  3619. case XML_ATTRIBUTE_NMTOKEN:
  3620. case XML_ATTRIBUTE_CDATA:
  3621. break;
  3622. case XML_ATTRIBUTE_ENTITY: {
  3623. xmlEntityPtr ent;
  3624. ent = xmlGetDocEntity(doc, value);
  3625. /* yeah it's a bit messy... */
  3626. if ((ent == NULL) && (doc->standalone == 1)) {
  3627. doc->standalone = 0;
  3628. ent = xmlGetDocEntity(doc, value);
  3629. }
  3630. if (ent == NULL) {
  3631. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3632. XML_DTD_UNKNOWN_ENTITY,
  3633. "ENTITY attribute %s reference an unknown entity \"%s\"\n",
  3634. name, value, NULL);
  3635. ret = 0;
  3636. } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
  3637. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3638. XML_DTD_ENTITY_TYPE,
  3639. "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
  3640. name, value, NULL);
  3641. ret = 0;
  3642. }
  3643. break;
  3644. }
  3645. case XML_ATTRIBUTE_ENTITIES: {
  3646. xmlChar *dup, *nam = NULL, *cur, save;
  3647. xmlEntityPtr ent;
  3648. dup = xmlStrdup(value);
  3649. if (dup == NULL)
  3650. return(0);
  3651. cur = dup;
  3652. while (*cur != 0) {
  3653. nam = cur;
  3654. while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
  3655. save = *cur;
  3656. *cur = 0;
  3657. ent = xmlGetDocEntity(doc, nam);
  3658. if (ent == NULL) {
  3659. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3660. XML_DTD_UNKNOWN_ENTITY,
  3661. "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
  3662. name, nam, NULL);
  3663. ret = 0;
  3664. } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
  3665. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3666. XML_DTD_ENTITY_TYPE,
  3667. "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
  3668. name, nam, NULL);
  3669. ret = 0;
  3670. }
  3671. if (save == 0)
  3672. break;
  3673. *cur = save;
  3674. while (IS_BLANK_CH(*cur)) cur++;
  3675. }
  3676. xmlFree(dup);
  3677. break;
  3678. }
  3679. case XML_ATTRIBUTE_NOTATION: {
  3680. xmlNotationPtr nota;
  3681. nota = xmlGetDtdNotationDesc(doc->intSubset, value);
  3682. if ((nota == NULL) && (doc->extSubset != NULL))
  3683. nota = xmlGetDtdNotationDesc(doc->extSubset, value);
  3684. if (nota == NULL) {
  3685. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3686. XML_DTD_UNKNOWN_NOTATION,
  3687. "NOTATION attribute %s reference an unknown notation \"%s\"\n",
  3688. name, value, NULL);
  3689. ret = 0;
  3690. }
  3691. break;
  3692. }
  3693. }
  3694. return(ret);
  3695. }
  3696. /**
  3697. * xmlValidCtxtNormalizeAttributeValue:
  3698. * @ctxt: the validation context
  3699. * @doc: the document
  3700. * @elem: the parent
  3701. * @name: the attribute name
  3702. * @value: the attribute value
  3703. * @ctxt: the validation context or NULL
  3704. *
  3705. * Does the validation related extra step of the normalization of attribute
  3706. * values:
  3707. *
  3708. * If the declared value is not CDATA, then the XML processor must further
  3709. * process the normalized attribute value by discarding any leading and
  3710. * trailing space (#x20) characters, and by replacing sequences of space
  3711. * (#x20) characters by single space (#x20) character.
  3712. *
  3713. * Also check VC: Standalone Document Declaration in P32, and update
  3714. * ctxt->valid accordingly
  3715. *
  3716. * returns a new normalized string if normalization is needed, NULL otherwise
  3717. * the caller must free the returned value.
  3718. */
  3719. xmlChar *
  3720. xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3721. xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
  3722. xmlChar *ret;
  3723. xmlAttributePtr attrDecl = NULL;
  3724. int extsubset = 0;
  3725. if (doc == NULL) return(NULL);
  3726. if (elem == NULL) return(NULL);
  3727. if (name == NULL) return(NULL);
  3728. if (value == NULL) return(NULL);
  3729. if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
  3730. xmlChar fn[50];
  3731. xmlChar *fullname;
  3732. fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
  3733. if (fullname == NULL)
  3734. return(NULL);
  3735. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
  3736. if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
  3737. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
  3738. if (attrDecl != NULL)
  3739. extsubset = 1;
  3740. }
  3741. if ((fullname != fn) && (fullname != elem->name))
  3742. xmlFree(fullname);
  3743. }
  3744. if ((attrDecl == NULL) && (doc->intSubset != NULL))
  3745. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
  3746. if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
  3747. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
  3748. if (attrDecl != NULL)
  3749. extsubset = 1;
  3750. }
  3751. if (attrDecl == NULL)
  3752. return(NULL);
  3753. if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
  3754. return(NULL);
  3755. ret = xmlStrdup(value);
  3756. if (ret == NULL)
  3757. return(NULL);
  3758. xmlValidNormalizeString(ret);
  3759. if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
  3760. xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
  3761. "standalone: %s on %s value had to be normalized based on external subset declaration\n",
  3762. name, elem->name, NULL);
  3763. ctxt->valid = 0;
  3764. }
  3765. return(ret);
  3766. }
  3767. /**
  3768. * xmlValidNormalizeAttributeValue:
  3769. * @doc: the document
  3770. * @elem: the parent
  3771. * @name: the attribute name
  3772. * @value: the attribute value
  3773. *
  3774. * Does the validation related extra step of the normalization of attribute
  3775. * values:
  3776. *
  3777. * If the declared value is not CDATA, then the XML processor must further
  3778. * process the normalized attribute value by discarding any leading and
  3779. * trailing space (#x20) characters, and by replacing sequences of space
  3780. * (#x20) characters by single space (#x20) character.
  3781. *
  3782. * Returns a new normalized string if normalization is needed, NULL otherwise
  3783. * the caller must free the returned value.
  3784. */
  3785. xmlChar *
  3786. xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
  3787. const xmlChar *name, const xmlChar *value) {
  3788. xmlChar *ret;
  3789. xmlAttributePtr attrDecl = NULL;
  3790. if (doc == NULL) return(NULL);
  3791. if (elem == NULL) return(NULL);
  3792. if (name == NULL) return(NULL);
  3793. if (value == NULL) return(NULL);
  3794. if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
  3795. xmlChar fn[50];
  3796. xmlChar *fullname;
  3797. fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
  3798. if (fullname == NULL)
  3799. return(NULL);
  3800. if ((fullname != fn) && (fullname != elem->name))
  3801. xmlFree(fullname);
  3802. }
  3803. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
  3804. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  3805. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
  3806. if (attrDecl == NULL)
  3807. return(NULL);
  3808. if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
  3809. return(NULL);
  3810. ret = xmlStrdup(value);
  3811. if (ret == NULL)
  3812. return(NULL);
  3813. xmlValidNormalizeString(ret);
  3814. return(ret);
  3815. }
  3816. static void
  3817. xmlValidateAttributeIdCallback(void *payload, void *data,
  3818. const xmlChar *name ATTRIBUTE_UNUSED) {
  3819. xmlAttributePtr attr = (xmlAttributePtr) payload;
  3820. int *count = (int *) data;
  3821. if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
  3822. }
  3823. /**
  3824. * xmlValidateAttributeDecl:
  3825. * @ctxt: the validation context
  3826. * @doc: a document instance
  3827. * @attr: an attribute definition
  3828. *
  3829. * Try to validate a single attribute definition
  3830. * basically it does the following checks as described by the
  3831. * XML-1.0 recommendation:
  3832. * - [ VC: Attribute Default Legal ]
  3833. * - [ VC: Enumeration ]
  3834. * - [ VC: ID Attribute Default ]
  3835. *
  3836. * The ID/IDREF uniqueness and matching are done separately
  3837. *
  3838. * returns 1 if valid or 0 otherwise
  3839. */
  3840. int
  3841. xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3842. xmlAttributePtr attr) {
  3843. int ret = 1;
  3844. int val;
  3845. CHECK_DTD;
  3846. if(attr == NULL) return(1);
  3847. /* Attribute Default Legal */
  3848. /* Enumeration */
  3849. if (attr->defaultValue != NULL) {
  3850. val = xmlValidateAttributeValueInternal(doc, attr->atype,
  3851. attr->defaultValue);
  3852. if (val == 0) {
  3853. xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
  3854. "Syntax of default value for attribute %s of %s is not valid\n",
  3855. attr->name, attr->elem, NULL);
  3856. }
  3857. ret &= val;
  3858. }
  3859. /* ID Attribute Default */
  3860. if ((attr->atype == XML_ATTRIBUTE_ID)&&
  3861. (attr->def != XML_ATTRIBUTE_IMPLIED) &&
  3862. (attr->def != XML_ATTRIBUTE_REQUIRED)) {
  3863. xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
  3864. "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
  3865. attr->name, attr->elem, NULL);
  3866. ret = 0;
  3867. }
  3868. /* One ID per Element Type */
  3869. if (attr->atype == XML_ATTRIBUTE_ID) {
  3870. int nbId;
  3871. /* the trick is that we parse DtD as their own internal subset */
  3872. xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
  3873. attr->elem);
  3874. if (elem != NULL) {
  3875. nbId = xmlScanIDAttributeDecl(NULL, elem, 0);
  3876. } else {
  3877. xmlAttributeTablePtr table;
  3878. /*
  3879. * The attribute may be declared in the internal subset and the
  3880. * element in the external subset.
  3881. */
  3882. nbId = 0;
  3883. if (doc->intSubset != NULL) {
  3884. table = (xmlAttributeTablePtr) doc->intSubset->attributes;
  3885. xmlHashScan3(table, NULL, NULL, attr->elem,
  3886. xmlValidateAttributeIdCallback, &nbId);
  3887. }
  3888. }
  3889. if (nbId > 1) {
  3890. xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
  3891. "Element %s has %d ID attribute defined in the internal subset : %s\n",
  3892. attr->elem, nbId, attr->name);
  3893. } else if (doc->extSubset != NULL) {
  3894. int extId = 0;
  3895. elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
  3896. if (elem != NULL) {
  3897. extId = xmlScanIDAttributeDecl(NULL, elem, 0);
  3898. }
  3899. if (extId > 1) {
  3900. xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
  3901. "Element %s has %d ID attribute defined in the external subset : %s\n",
  3902. attr->elem, extId, attr->name);
  3903. } else if (extId + nbId > 1) {
  3904. xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
  3905. "Element %s has ID attributes defined in the internal and external subset : %s\n",
  3906. attr->elem, attr->name, NULL);
  3907. }
  3908. }
  3909. }
  3910. /* Validity Constraint: Enumeration */
  3911. if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
  3912. xmlEnumerationPtr tree = attr->tree;
  3913. while (tree != NULL) {
  3914. if (xmlStrEqual(tree->name, attr->defaultValue)) break;
  3915. tree = tree->next;
  3916. }
  3917. if (tree == NULL) {
  3918. xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
  3919. "Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
  3920. attr->defaultValue, attr->name, attr->elem);
  3921. ret = 0;
  3922. }
  3923. }
  3924. return(ret);
  3925. }
  3926. /**
  3927. * xmlValidateElementDecl:
  3928. * @ctxt: the validation context
  3929. * @doc: a document instance
  3930. * @elem: an element definition
  3931. *
  3932. * Try to validate a single element definition
  3933. * basically it does the following checks as described by the
  3934. * XML-1.0 recommendation:
  3935. * - [ VC: One ID per Element Type ]
  3936. * - [ VC: No Duplicate Types ]
  3937. * - [ VC: Unique Element Type Declaration ]
  3938. *
  3939. * returns 1 if valid or 0 otherwise
  3940. */
  3941. int
  3942. xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3943. xmlElementPtr elem) {
  3944. int ret = 1;
  3945. xmlElementPtr tst;
  3946. CHECK_DTD;
  3947. if (elem == NULL) return(1);
  3948. #if 0
  3949. #ifdef LIBXML_REGEXP_ENABLED
  3950. /* Build the regexp associated to the content model */
  3951. ret = xmlValidBuildContentModel(ctxt, elem);
  3952. #endif
  3953. #endif
  3954. /* No Duplicate Types */
  3955. if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
  3956. xmlElementContentPtr cur, next;
  3957. const xmlChar *name;
  3958. cur = elem->content;
  3959. while (cur != NULL) {
  3960. if (cur->type != XML_ELEMENT_CONTENT_OR) break;
  3961. if (cur->c1 == NULL) break;
  3962. if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
  3963. name = cur->c1->name;
  3964. next = cur->c2;
  3965. while (next != NULL) {
  3966. if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
  3967. if ((xmlStrEqual(next->name, name)) &&
  3968. (xmlStrEqual(next->prefix, cur->c1->prefix))) {
  3969. if (cur->c1->prefix == NULL) {
  3970. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
  3971. "Definition of %s has duplicate references of %s\n",
  3972. elem->name, name, NULL);
  3973. } else {
  3974. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
  3975. "Definition of %s has duplicate references of %s:%s\n",
  3976. elem->name, cur->c1->prefix, name);
  3977. }
  3978. ret = 0;
  3979. }
  3980. break;
  3981. }
  3982. if (next->c1 == NULL) break;
  3983. if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
  3984. if ((xmlStrEqual(next->c1->name, name)) &&
  3985. (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) {
  3986. if (cur->c1->prefix == NULL) {
  3987. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
  3988. "Definition of %s has duplicate references to %s\n",
  3989. elem->name, name, NULL);
  3990. } else {
  3991. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
  3992. "Definition of %s has duplicate references to %s:%s\n",
  3993. elem->name, cur->c1->prefix, name);
  3994. }
  3995. ret = 0;
  3996. }
  3997. next = next->c2;
  3998. }
  3999. }
  4000. cur = cur->c2;
  4001. }
  4002. }
  4003. /* VC: Unique Element Type Declaration */
  4004. tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
  4005. if ((tst != NULL ) && (tst != elem) &&
  4006. ((tst->prefix == elem->prefix) ||
  4007. (xmlStrEqual(tst->prefix, elem->prefix))) &&
  4008. (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
  4009. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
  4010. "Redefinition of element %s\n",
  4011. elem->name, NULL, NULL);
  4012. ret = 0;
  4013. }
  4014. tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
  4015. if ((tst != NULL ) && (tst != elem) &&
  4016. ((tst->prefix == elem->prefix) ||
  4017. (xmlStrEqual(tst->prefix, elem->prefix))) &&
  4018. (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
  4019. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
  4020. "Redefinition of element %s\n",
  4021. elem->name, NULL, NULL);
  4022. ret = 0;
  4023. }
  4024. /* One ID per Element Type
  4025. * already done when registering the attribute
  4026. if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
  4027. ret = 0;
  4028. } */
  4029. return(ret);
  4030. }
  4031. /**
  4032. * xmlValidateOneAttribute:
  4033. * @ctxt: the validation context
  4034. * @doc: a document instance
  4035. * @elem: an element instance
  4036. * @attr: an attribute instance
  4037. * @value: the attribute value (without entities processing)
  4038. *
  4039. * Try to validate a single attribute for an element
  4040. * basically it does the following checks as described by the
  4041. * XML-1.0 recommendation:
  4042. * - [ VC: Attribute Value Type ]
  4043. * - [ VC: Fixed Attribute Default ]
  4044. * - [ VC: Entity Name ]
  4045. * - [ VC: Name Token ]
  4046. * - [ VC: ID ]
  4047. * - [ VC: IDREF ]
  4048. * - [ VC: Entity Name ]
  4049. * - [ VC: Notation Attributes ]
  4050. *
  4051. * The ID/IDREF uniqueness and matching are done separately
  4052. *
  4053. * returns 1 if valid or 0 otherwise
  4054. */
  4055. int
  4056. xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  4057. xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
  4058. {
  4059. xmlAttributePtr attrDecl = NULL;
  4060. int val;
  4061. int ret = 1;
  4062. CHECK_DTD;
  4063. if ((elem == NULL) || (elem->name == NULL)) return(0);
  4064. if ((attr == NULL) || (attr->name == NULL)) return(0);
  4065. if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
  4066. xmlChar fn[50];
  4067. xmlChar *fullname;
  4068. fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
  4069. if (fullname == NULL)
  4070. return(0);
  4071. if (attr->ns != NULL) {
  4072. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
  4073. attr->name, attr->ns->prefix);
  4074. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4075. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
  4076. attr->name, attr->ns->prefix);
  4077. } else {
  4078. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
  4079. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4080. attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
  4081. fullname, attr->name);
  4082. }
  4083. if ((fullname != fn) && (fullname != elem->name))
  4084. xmlFree(fullname);
  4085. }
  4086. if (attrDecl == NULL) {
  4087. if (attr->ns != NULL) {
  4088. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
  4089. attr->name, attr->ns->prefix);
  4090. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4091. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
  4092. attr->name, attr->ns->prefix);
  4093. } else {
  4094. attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
  4095. elem->name, attr->name);
  4096. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4097. attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
  4098. elem->name, attr->name);
  4099. }
  4100. }
  4101. /* Validity Constraint: Attribute Value Type */
  4102. if (attrDecl == NULL) {
  4103. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
  4104. "No declaration for attribute %s of element %s\n",
  4105. attr->name, elem->name, NULL);
  4106. return(0);
  4107. }
  4108. attr->atype = attrDecl->atype;
  4109. val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
  4110. if (val == 0) {
  4111. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4112. "Syntax of value for attribute %s of %s is not valid\n",
  4113. attr->name, elem->name, NULL);
  4114. ret = 0;
  4115. }
  4116. /* Validity constraint: Fixed Attribute Default */
  4117. if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
  4118. if (!xmlStrEqual(value, attrDecl->defaultValue)) {
  4119. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
  4120. "Value for attribute %s of %s is different from default \"%s\"\n",
  4121. attr->name, elem->name, attrDecl->defaultValue);
  4122. ret = 0;
  4123. }
  4124. }
  4125. /* Validity Constraint: ID uniqueness */
  4126. if (attrDecl->atype == XML_ATTRIBUTE_ID) {
  4127. if (xmlAddID(ctxt, doc, value, attr) == NULL)
  4128. ret = 0;
  4129. }
  4130. if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
  4131. (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
  4132. if (xmlAddRef(ctxt, doc, value, attr) == NULL)
  4133. ret = 0;
  4134. }
  4135. /* Validity Constraint: Notation Attributes */
  4136. if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
  4137. xmlEnumerationPtr tree = attrDecl->tree;
  4138. xmlNotationPtr nota;
  4139. /* First check that the given NOTATION was declared */
  4140. nota = xmlGetDtdNotationDesc(doc->intSubset, value);
  4141. if (nota == NULL)
  4142. nota = xmlGetDtdNotationDesc(doc->extSubset, value);
  4143. if (nota == NULL) {
  4144. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
  4145. "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
  4146. value, attr->name, elem->name);
  4147. ret = 0;
  4148. }
  4149. /* Second, verify that it's among the list */
  4150. while (tree != NULL) {
  4151. if (xmlStrEqual(tree->name, value)) break;
  4152. tree = tree->next;
  4153. }
  4154. if (tree == NULL) {
  4155. xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
  4156. "Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
  4157. value, attr->name, elem->name);
  4158. ret = 0;
  4159. }
  4160. }
  4161. /* Validity Constraint: Enumeration */
  4162. if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
  4163. xmlEnumerationPtr tree = attrDecl->tree;
  4164. while (tree != NULL) {
  4165. if (xmlStrEqual(tree->name, value)) break;
  4166. tree = tree->next;
  4167. }
  4168. if (tree == NULL) {
  4169. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4170. "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
  4171. value, attr->name, elem->name);
  4172. ret = 0;
  4173. }
  4174. }
  4175. /* Fixed Attribute Default */
  4176. if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
  4177. (!xmlStrEqual(attrDecl->defaultValue, value))) {
  4178. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4179. "Value for attribute %s of %s must be \"%s\"\n",
  4180. attr->name, elem->name, attrDecl->defaultValue);
  4181. ret = 0;
  4182. }
  4183. /* Extra check for the attribute value */
  4184. ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
  4185. attrDecl->atype, value);
  4186. return(ret);
  4187. }
  4188. /**
  4189. * xmlValidateOneNamespace:
  4190. * @ctxt: the validation context
  4191. * @doc: a document instance
  4192. * @elem: an element instance
  4193. * @prefix: the namespace prefix
  4194. * @ns: an namespace declaration instance
  4195. * @value: the attribute value (without entities processing)
  4196. *
  4197. * Try to validate a single namespace declaration for an element
  4198. * basically it does the following checks as described by the
  4199. * XML-1.0 recommendation:
  4200. * - [ VC: Attribute Value Type ]
  4201. * - [ VC: Fixed Attribute Default ]
  4202. * - [ VC: Entity Name ]
  4203. * - [ VC: Name Token ]
  4204. * - [ VC: ID ]
  4205. * - [ VC: IDREF ]
  4206. * - [ VC: Entity Name ]
  4207. * - [ VC: Notation Attributes ]
  4208. *
  4209. * The ID/IDREF uniqueness and matching are done separately
  4210. *
  4211. * returns 1 if valid or 0 otherwise
  4212. */
  4213. int
  4214. xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  4215. xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
  4216. /* xmlElementPtr elemDecl; */
  4217. xmlAttributePtr attrDecl = NULL;
  4218. int val;
  4219. int ret = 1;
  4220. CHECK_DTD;
  4221. if ((elem == NULL) || (elem->name == NULL)) return(0);
  4222. if ((ns == NULL) || (ns->href == NULL)) return(0);
  4223. if (prefix != NULL) {
  4224. xmlChar fn[50];
  4225. xmlChar *fullname;
  4226. fullname = xmlBuildQName(elem->name, prefix, fn, 50);
  4227. if (fullname == NULL) {
  4228. xmlVErrMemory(ctxt, "Validating namespace");
  4229. return(0);
  4230. }
  4231. if (ns->prefix != NULL) {
  4232. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
  4233. ns->prefix, BAD_CAST "xmlns");
  4234. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4235. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
  4236. ns->prefix, BAD_CAST "xmlns");
  4237. } else {
  4238. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
  4239. BAD_CAST "xmlns");
  4240. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4241. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
  4242. BAD_CAST "xmlns");
  4243. }
  4244. if ((fullname != fn) && (fullname != elem->name))
  4245. xmlFree(fullname);
  4246. }
  4247. if (attrDecl == NULL) {
  4248. if (ns->prefix != NULL) {
  4249. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
  4250. ns->prefix, BAD_CAST "xmlns");
  4251. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4252. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
  4253. ns->prefix, BAD_CAST "xmlns");
  4254. } else {
  4255. attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
  4256. elem->name, BAD_CAST "xmlns");
  4257. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4258. attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
  4259. elem->name, BAD_CAST "xmlns");
  4260. }
  4261. }
  4262. /* Validity Constraint: Attribute Value Type */
  4263. if (attrDecl == NULL) {
  4264. if (ns->prefix != NULL) {
  4265. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
  4266. "No declaration for attribute xmlns:%s of element %s\n",
  4267. ns->prefix, elem->name, NULL);
  4268. } else {
  4269. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
  4270. "No declaration for attribute xmlns of element %s\n",
  4271. elem->name, NULL, NULL);
  4272. }
  4273. return(0);
  4274. }
  4275. val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
  4276. if (val == 0) {
  4277. if (ns->prefix != NULL) {
  4278. xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
  4279. "Syntax of value for attribute xmlns:%s of %s is not valid\n",
  4280. ns->prefix, elem->name, NULL);
  4281. } else {
  4282. xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
  4283. "Syntax of value for attribute xmlns of %s is not valid\n",
  4284. elem->name, NULL, NULL);
  4285. }
  4286. ret = 0;
  4287. }
  4288. /* Validity constraint: Fixed Attribute Default */
  4289. if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
  4290. if (!xmlStrEqual(value, attrDecl->defaultValue)) {
  4291. if (ns->prefix != NULL) {
  4292. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
  4293. "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
  4294. ns->prefix, elem->name, attrDecl->defaultValue);
  4295. } else {
  4296. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
  4297. "Value for attribute xmlns of %s is different from default \"%s\"\n",
  4298. elem->name, attrDecl->defaultValue, NULL);
  4299. }
  4300. ret = 0;
  4301. }
  4302. }
  4303. /*
  4304. * Casting ns to xmlAttrPtr is wrong. We'd need separate functions
  4305. * xmlAddID and xmlAddRef for namespace declarations, but it makes
  4306. * no practical sense to use ID types anyway.
  4307. */
  4308. #if 0
  4309. /* Validity Constraint: ID uniqueness */
  4310. if (attrDecl->atype == XML_ATTRIBUTE_ID) {
  4311. if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
  4312. ret = 0;
  4313. }
  4314. if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
  4315. (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
  4316. if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
  4317. ret = 0;
  4318. }
  4319. #endif
  4320. /* Validity Constraint: Notation Attributes */
  4321. if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
  4322. xmlEnumerationPtr tree = attrDecl->tree;
  4323. xmlNotationPtr nota;
  4324. /* First check that the given NOTATION was declared */
  4325. nota = xmlGetDtdNotationDesc(doc->intSubset, value);
  4326. if (nota == NULL)
  4327. nota = xmlGetDtdNotationDesc(doc->extSubset, value);
  4328. if (nota == NULL) {
  4329. if (ns->prefix != NULL) {
  4330. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
  4331. "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
  4332. value, ns->prefix, elem->name);
  4333. } else {
  4334. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
  4335. "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
  4336. value, elem->name, NULL);
  4337. }
  4338. ret = 0;
  4339. }
  4340. /* Second, verify that it's among the list */
  4341. while (tree != NULL) {
  4342. if (xmlStrEqual(tree->name, value)) break;
  4343. tree = tree->next;
  4344. }
  4345. if (tree == NULL) {
  4346. if (ns->prefix != NULL) {
  4347. xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
  4348. "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
  4349. value, ns->prefix, elem->name);
  4350. } else {
  4351. xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
  4352. "Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
  4353. value, elem->name, NULL);
  4354. }
  4355. ret = 0;
  4356. }
  4357. }
  4358. /* Validity Constraint: Enumeration */
  4359. if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
  4360. xmlEnumerationPtr tree = attrDecl->tree;
  4361. while (tree != NULL) {
  4362. if (xmlStrEqual(tree->name, value)) break;
  4363. tree = tree->next;
  4364. }
  4365. if (tree == NULL) {
  4366. if (ns->prefix != NULL) {
  4367. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4368. "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
  4369. value, ns->prefix, elem->name);
  4370. } else {
  4371. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4372. "Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
  4373. value, elem->name, NULL);
  4374. }
  4375. ret = 0;
  4376. }
  4377. }
  4378. /* Fixed Attribute Default */
  4379. if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
  4380. (!xmlStrEqual(attrDecl->defaultValue, value))) {
  4381. if (ns->prefix != NULL) {
  4382. xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
  4383. "Value for attribute xmlns:%s of %s must be \"%s\"\n",
  4384. ns->prefix, elem->name, attrDecl->defaultValue);
  4385. } else {
  4386. xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
  4387. "Value for attribute xmlns of %s must be \"%s\"\n",
  4388. elem->name, attrDecl->defaultValue, NULL);
  4389. }
  4390. ret = 0;
  4391. }
  4392. /* Extra check for the attribute value */
  4393. if (ns->prefix != NULL) {
  4394. ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
  4395. attrDecl->atype, value);
  4396. } else {
  4397. ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
  4398. attrDecl->atype, value);
  4399. }
  4400. return(ret);
  4401. }
  4402. #ifndef LIBXML_REGEXP_ENABLED
  4403. /**
  4404. * xmlValidateSkipIgnorable:
  4405. * @ctxt: the validation context
  4406. * @child: the child list
  4407. *
  4408. * Skip ignorable elements w.r.t. the validation process
  4409. *
  4410. * returns the first element to consider for validation of the content model
  4411. */
  4412. static xmlNodePtr
  4413. xmlValidateSkipIgnorable(xmlNodePtr child) {
  4414. while (child != NULL) {
  4415. switch (child->type) {
  4416. /* These things are ignored (skipped) during validation. */
  4417. case XML_PI_NODE:
  4418. case XML_COMMENT_NODE:
  4419. case XML_XINCLUDE_START:
  4420. case XML_XINCLUDE_END:
  4421. child = child->next;
  4422. break;
  4423. case XML_TEXT_NODE:
  4424. if (xmlIsBlankNode(child))
  4425. child = child->next;
  4426. else
  4427. return(child);
  4428. break;
  4429. /* keep current node */
  4430. default:
  4431. return(child);
  4432. }
  4433. }
  4434. return(child);
  4435. }
  4436. /**
  4437. * xmlValidateElementType:
  4438. * @ctxt: the validation context
  4439. *
  4440. * Try to validate the content model of an element internal function
  4441. *
  4442. * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
  4443. * reference is found and -3 if the validation succeeded but
  4444. * the content model is not determinist.
  4445. */
  4446. static int
  4447. xmlValidateElementType(xmlValidCtxtPtr ctxt) {
  4448. int ret = -1;
  4449. int determinist = 1;
  4450. NODE = xmlValidateSkipIgnorable(NODE);
  4451. if ((NODE == NULL) && (CONT == NULL))
  4452. return(1);
  4453. if ((NODE == NULL) &&
  4454. ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
  4455. (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
  4456. return(1);
  4457. }
  4458. if (CONT == NULL) return(-1);
  4459. if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
  4460. return(-2);
  4461. /*
  4462. * We arrive here when more states need to be examined
  4463. */
  4464. cont:
  4465. /*
  4466. * We just recovered from a rollback generated by a possible
  4467. * epsilon transition, go directly to the analysis phase
  4468. */
  4469. if (STATE == ROLLBACK_PARENT) {
  4470. DEBUG_VALID_MSG("restored parent branch");
  4471. DEBUG_VALID_STATE(NODE, CONT)
  4472. ret = 1;
  4473. goto analyze;
  4474. }
  4475. DEBUG_VALID_STATE(NODE, CONT)
  4476. /*
  4477. * we may have to save a backup state here. This is the equivalent
  4478. * of handling epsilon transition in NFAs.
  4479. */
  4480. if ((CONT != NULL) &&
  4481. ((CONT->parent == NULL) ||
  4482. (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
  4483. ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
  4484. (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
  4485. ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
  4486. DEBUG_VALID_MSG("saving parent branch");
  4487. if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
  4488. return(0);
  4489. }
  4490. /*
  4491. * Check first if the content matches
  4492. */
  4493. switch (CONT->type) {
  4494. case XML_ELEMENT_CONTENT_PCDATA:
  4495. if (NODE == NULL) {
  4496. DEBUG_VALID_MSG("pcdata failed no node");
  4497. ret = 0;
  4498. break;
  4499. }
  4500. if (NODE->type == XML_TEXT_NODE) {
  4501. DEBUG_VALID_MSG("pcdata found, skip to next");
  4502. /*
  4503. * go to next element in the content model
  4504. * skipping ignorable elems
  4505. */
  4506. do {
  4507. NODE = NODE->next;
  4508. NODE = xmlValidateSkipIgnorable(NODE);
  4509. if ((NODE != NULL) &&
  4510. (NODE->type == XML_ENTITY_REF_NODE))
  4511. return(-2);
  4512. } while ((NODE != NULL) &&
  4513. ((NODE->type != XML_ELEMENT_NODE) &&
  4514. (NODE->type != XML_TEXT_NODE) &&
  4515. (NODE->type != XML_CDATA_SECTION_NODE)));
  4516. ret = 1;
  4517. break;
  4518. } else {
  4519. DEBUG_VALID_MSG("pcdata failed");
  4520. ret = 0;
  4521. break;
  4522. }
  4523. break;
  4524. case XML_ELEMENT_CONTENT_ELEMENT:
  4525. if (NODE == NULL) {
  4526. DEBUG_VALID_MSG("element failed no node");
  4527. ret = 0;
  4528. break;
  4529. }
  4530. ret = ((NODE->type == XML_ELEMENT_NODE) &&
  4531. (xmlStrEqual(NODE->name, CONT->name)));
  4532. if (ret == 1) {
  4533. if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
  4534. ret = (CONT->prefix == NULL);
  4535. } else if (CONT->prefix == NULL) {
  4536. ret = 0;
  4537. } else {
  4538. ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
  4539. }
  4540. }
  4541. if (ret == 1) {
  4542. DEBUG_VALID_MSG("element found, skip to next");
  4543. /*
  4544. * go to next element in the content model
  4545. * skipping ignorable elems
  4546. */
  4547. do {
  4548. NODE = NODE->next;
  4549. NODE = xmlValidateSkipIgnorable(NODE);
  4550. if ((NODE != NULL) &&
  4551. (NODE->type == XML_ENTITY_REF_NODE))
  4552. return(-2);
  4553. } while ((NODE != NULL) &&
  4554. ((NODE->type != XML_ELEMENT_NODE) &&
  4555. (NODE->type != XML_TEXT_NODE) &&
  4556. (NODE->type != XML_CDATA_SECTION_NODE)));
  4557. } else {
  4558. DEBUG_VALID_MSG("element failed");
  4559. ret = 0;
  4560. break;
  4561. }
  4562. break;
  4563. case XML_ELEMENT_CONTENT_OR:
  4564. /*
  4565. * Small optimization.
  4566. */
  4567. if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
  4568. if ((NODE == NULL) ||
  4569. (!xmlStrEqual(NODE->name, CONT->c1->name))) {
  4570. DEPTH++;
  4571. CONT = CONT->c2;
  4572. goto cont;
  4573. }
  4574. if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
  4575. ret = (CONT->c1->prefix == NULL);
  4576. } else if (CONT->c1->prefix == NULL) {
  4577. ret = 0;
  4578. } else {
  4579. ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
  4580. }
  4581. if (ret == 0) {
  4582. DEPTH++;
  4583. CONT = CONT->c2;
  4584. goto cont;
  4585. }
  4586. }
  4587. /*
  4588. * save the second branch 'or' branch
  4589. */
  4590. DEBUG_VALID_MSG("saving 'or' branch");
  4591. if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
  4592. OCCURS, ROLLBACK_OR) < 0)
  4593. return(-1);
  4594. DEPTH++;
  4595. CONT = CONT->c1;
  4596. goto cont;
  4597. case XML_ELEMENT_CONTENT_SEQ:
  4598. /*
  4599. * Small optimization.
  4600. */
  4601. if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
  4602. ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
  4603. (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
  4604. if ((NODE == NULL) ||
  4605. (!xmlStrEqual(NODE->name, CONT->c1->name))) {
  4606. DEPTH++;
  4607. CONT = CONT->c2;
  4608. goto cont;
  4609. }
  4610. if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
  4611. ret = (CONT->c1->prefix == NULL);
  4612. } else if (CONT->c1->prefix == NULL) {
  4613. ret = 0;
  4614. } else {
  4615. ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
  4616. }
  4617. if (ret == 0) {
  4618. DEPTH++;
  4619. CONT = CONT->c2;
  4620. goto cont;
  4621. }
  4622. }
  4623. DEPTH++;
  4624. CONT = CONT->c1;
  4625. goto cont;
  4626. }
  4627. /*
  4628. * At this point handle going up in the tree
  4629. */
  4630. if (ret == -1) {
  4631. DEBUG_VALID_MSG("error found returning");
  4632. return(ret);
  4633. }
  4634. analyze:
  4635. while (CONT != NULL) {
  4636. /*
  4637. * First do the analysis depending on the occurrence model at
  4638. * this level.
  4639. */
  4640. if (ret == 0) {
  4641. switch (CONT->ocur) {
  4642. xmlNodePtr cur;
  4643. case XML_ELEMENT_CONTENT_ONCE:
  4644. cur = ctxt->vstate->node;
  4645. DEBUG_VALID_MSG("Once branch failed, rollback");
  4646. if (vstateVPop(ctxt) < 0 ) {
  4647. DEBUG_VALID_MSG("exhaustion, failed");
  4648. return(0);
  4649. }
  4650. if (cur != ctxt->vstate->node)
  4651. determinist = -3;
  4652. goto cont;
  4653. case XML_ELEMENT_CONTENT_PLUS:
  4654. if (OCCURRENCE == 0) {
  4655. cur = ctxt->vstate->node;
  4656. DEBUG_VALID_MSG("Plus branch failed, rollback");
  4657. if (vstateVPop(ctxt) < 0 ) {
  4658. DEBUG_VALID_MSG("exhaustion, failed");
  4659. return(0);
  4660. }
  4661. if (cur != ctxt->vstate->node)
  4662. determinist = -3;
  4663. goto cont;
  4664. }
  4665. DEBUG_VALID_MSG("Plus branch found");
  4666. ret = 1;
  4667. break;
  4668. case XML_ELEMENT_CONTENT_MULT:
  4669. #ifdef DEBUG_VALID_ALGO
  4670. if (OCCURRENCE == 0) {
  4671. DEBUG_VALID_MSG("Mult branch failed");
  4672. } else {
  4673. DEBUG_VALID_MSG("Mult branch found");
  4674. }
  4675. #endif
  4676. ret = 1;
  4677. break;
  4678. case XML_ELEMENT_CONTENT_OPT:
  4679. DEBUG_VALID_MSG("Option branch failed");
  4680. ret = 1;
  4681. break;
  4682. }
  4683. } else {
  4684. switch (CONT->ocur) {
  4685. case XML_ELEMENT_CONTENT_OPT:
  4686. DEBUG_VALID_MSG("Option branch succeeded");
  4687. ret = 1;
  4688. break;
  4689. case XML_ELEMENT_CONTENT_ONCE:
  4690. DEBUG_VALID_MSG("Once branch succeeded");
  4691. ret = 1;
  4692. break;
  4693. case XML_ELEMENT_CONTENT_PLUS:
  4694. if (STATE == ROLLBACK_PARENT) {
  4695. DEBUG_VALID_MSG("Plus branch rollback");
  4696. ret = 1;
  4697. break;
  4698. }
  4699. if (NODE == NULL) {
  4700. DEBUG_VALID_MSG("Plus branch exhausted");
  4701. ret = 1;
  4702. break;
  4703. }
  4704. DEBUG_VALID_MSG("Plus branch succeeded, continuing");
  4705. SET_OCCURRENCE;
  4706. goto cont;
  4707. case XML_ELEMENT_CONTENT_MULT:
  4708. if (STATE == ROLLBACK_PARENT) {
  4709. DEBUG_VALID_MSG("Mult branch rollback");
  4710. ret = 1;
  4711. break;
  4712. }
  4713. if (NODE == NULL) {
  4714. DEBUG_VALID_MSG("Mult branch exhausted");
  4715. ret = 1;
  4716. break;
  4717. }
  4718. DEBUG_VALID_MSG("Mult branch succeeded, continuing");
  4719. /* SET_OCCURRENCE; */
  4720. goto cont;
  4721. }
  4722. }
  4723. STATE = 0;
  4724. /*
  4725. * Then act accordingly at the parent level
  4726. */
  4727. RESET_OCCURRENCE;
  4728. if (CONT->parent == NULL)
  4729. break;
  4730. switch (CONT->parent->type) {
  4731. case XML_ELEMENT_CONTENT_PCDATA:
  4732. DEBUG_VALID_MSG("Error: parent pcdata");
  4733. return(-1);
  4734. case XML_ELEMENT_CONTENT_ELEMENT:
  4735. DEBUG_VALID_MSG("Error: parent element");
  4736. return(-1);
  4737. case XML_ELEMENT_CONTENT_OR:
  4738. if (ret == 1) {
  4739. DEBUG_VALID_MSG("Or succeeded");
  4740. CONT = CONT->parent;
  4741. DEPTH--;
  4742. } else {
  4743. DEBUG_VALID_MSG("Or failed");
  4744. CONT = CONT->parent;
  4745. DEPTH--;
  4746. }
  4747. break;
  4748. case XML_ELEMENT_CONTENT_SEQ:
  4749. if (ret == 0) {
  4750. DEBUG_VALID_MSG("Sequence failed");
  4751. CONT = CONT->parent;
  4752. DEPTH--;
  4753. } else if (CONT == CONT->parent->c1) {
  4754. DEBUG_VALID_MSG("Sequence testing 2nd branch");
  4755. CONT = CONT->parent->c2;
  4756. goto cont;
  4757. } else {
  4758. DEBUG_VALID_MSG("Sequence succeeded");
  4759. CONT = CONT->parent;
  4760. DEPTH--;
  4761. }
  4762. }
  4763. }
  4764. if (NODE != NULL) {
  4765. xmlNodePtr cur;
  4766. cur = ctxt->vstate->node;
  4767. DEBUG_VALID_MSG("Failed, remaining input, rollback");
  4768. if (vstateVPop(ctxt) < 0 ) {
  4769. DEBUG_VALID_MSG("exhaustion, failed");
  4770. return(0);
  4771. }
  4772. if (cur != ctxt->vstate->node)
  4773. determinist = -3;
  4774. goto cont;
  4775. }
  4776. if (ret == 0) {
  4777. xmlNodePtr cur;
  4778. cur = ctxt->vstate->node;
  4779. DEBUG_VALID_MSG("Failure, rollback");
  4780. if (vstateVPop(ctxt) < 0 ) {
  4781. DEBUG_VALID_MSG("exhaustion, failed");
  4782. return(0);
  4783. }
  4784. if (cur != ctxt->vstate->node)
  4785. determinist = -3;
  4786. goto cont;
  4787. }
  4788. return(determinist);
  4789. }
  4790. #endif
  4791. /**
  4792. * xmlSnprintfElements:
  4793. * @buf: an output buffer
  4794. * @size: the size of the buffer
  4795. * @content: An element
  4796. * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
  4797. *
  4798. * This will dump the list of elements to the buffer
  4799. * Intended just for the debug routine
  4800. */
  4801. static void
  4802. xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
  4803. xmlNodePtr cur;
  4804. int len;
  4805. if (node == NULL) return;
  4806. if (glob) strcat(buf, "(");
  4807. cur = node;
  4808. while (cur != NULL) {
  4809. len = strlen(buf);
  4810. if (size - len < 50) {
  4811. if ((size - len > 4) && (buf[len - 1] != '.'))
  4812. strcat(buf, " ...");
  4813. return;
  4814. }
  4815. switch (cur->type) {
  4816. case XML_ELEMENT_NODE:
  4817. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  4818. if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
  4819. if ((size - len > 4) && (buf[len - 1] != '.'))
  4820. strcat(buf, " ...");
  4821. return;
  4822. }
  4823. strcat(buf, (char *) cur->ns->prefix);
  4824. strcat(buf, ":");
  4825. }
  4826. if (size - len < xmlStrlen(cur->name) + 10) {
  4827. if ((size - len > 4) && (buf[len - 1] != '.'))
  4828. strcat(buf, " ...");
  4829. return;
  4830. }
  4831. strcat(buf, (char *) cur->name);
  4832. if (cur->next != NULL)
  4833. strcat(buf, " ");
  4834. break;
  4835. case XML_TEXT_NODE:
  4836. if (xmlIsBlankNode(cur))
  4837. break;
  4838. /* Falls through. */
  4839. case XML_CDATA_SECTION_NODE:
  4840. case XML_ENTITY_REF_NODE:
  4841. strcat(buf, "CDATA");
  4842. if (cur->next != NULL)
  4843. strcat(buf, " ");
  4844. break;
  4845. case XML_ATTRIBUTE_NODE:
  4846. case XML_DOCUMENT_NODE:
  4847. #ifdef LIBXML_DOCB_ENABLED
  4848. case XML_DOCB_DOCUMENT_NODE:
  4849. #endif
  4850. case XML_HTML_DOCUMENT_NODE:
  4851. case XML_DOCUMENT_TYPE_NODE:
  4852. case XML_DOCUMENT_FRAG_NODE:
  4853. case XML_NOTATION_NODE:
  4854. case XML_NAMESPACE_DECL:
  4855. strcat(buf, "???");
  4856. if (cur->next != NULL)
  4857. strcat(buf, " ");
  4858. break;
  4859. case XML_ENTITY_NODE:
  4860. case XML_PI_NODE:
  4861. case XML_DTD_NODE:
  4862. case XML_COMMENT_NODE:
  4863. case XML_ELEMENT_DECL:
  4864. case XML_ATTRIBUTE_DECL:
  4865. case XML_ENTITY_DECL:
  4866. case XML_XINCLUDE_START:
  4867. case XML_XINCLUDE_END:
  4868. break;
  4869. }
  4870. cur = cur->next;
  4871. }
  4872. if (glob) strcat(buf, ")");
  4873. }
  4874. /**
  4875. * xmlValidateElementContent:
  4876. * @ctxt: the validation context
  4877. * @child: the child list
  4878. * @elemDecl: pointer to the element declaration
  4879. * @warn: emit the error message
  4880. * @parent: the parent element (for error reporting)
  4881. *
  4882. * Try to validate the content model of an element
  4883. *
  4884. * returns 1 if valid or 0 if not and -1 in case of error
  4885. */
  4886. static int
  4887. xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
  4888. xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
  4889. int ret = 1;
  4890. #ifndef LIBXML_REGEXP_ENABLED
  4891. xmlNodePtr repl = NULL, last = NULL, tmp;
  4892. #endif
  4893. xmlNodePtr cur;
  4894. xmlElementContentPtr cont;
  4895. const xmlChar *name;
  4896. if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL))
  4897. return(-1);
  4898. cont = elemDecl->content;
  4899. name = elemDecl->name;
  4900. #ifdef LIBXML_REGEXP_ENABLED
  4901. /* Build the regexp associated to the content model */
  4902. if (elemDecl->contModel == NULL)
  4903. ret = xmlValidBuildContentModel(ctxt, elemDecl);
  4904. if (elemDecl->contModel == NULL) {
  4905. return(-1);
  4906. } else {
  4907. xmlRegExecCtxtPtr exec;
  4908. if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
  4909. return(-1);
  4910. }
  4911. ctxt->nodeMax = 0;
  4912. ctxt->nodeNr = 0;
  4913. ctxt->nodeTab = NULL;
  4914. exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
  4915. if (exec != NULL) {
  4916. cur = child;
  4917. while (cur != NULL) {
  4918. switch (cur->type) {
  4919. case XML_ENTITY_REF_NODE:
  4920. /*
  4921. * Push the current node to be able to roll back
  4922. * and process within the entity
  4923. */
  4924. if ((cur->children != NULL) &&
  4925. (cur->children->children != NULL)) {
  4926. nodeVPush(ctxt, cur);
  4927. cur = cur->children->children;
  4928. continue;
  4929. }
  4930. break;
  4931. case XML_TEXT_NODE:
  4932. if (xmlIsBlankNode(cur))
  4933. break;
  4934. ret = 0;
  4935. goto fail;
  4936. case XML_CDATA_SECTION_NODE:
  4937. /* TODO */
  4938. ret = 0;
  4939. goto fail;
  4940. case XML_ELEMENT_NODE:
  4941. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  4942. xmlChar fn[50];
  4943. xmlChar *fullname;
  4944. fullname = xmlBuildQName(cur->name,
  4945. cur->ns->prefix, fn, 50);
  4946. if (fullname == NULL) {
  4947. ret = -1;
  4948. goto fail;
  4949. }
  4950. ret = xmlRegExecPushString(exec, fullname, NULL);
  4951. if ((fullname != fn) && (fullname != cur->name))
  4952. xmlFree(fullname);
  4953. } else {
  4954. ret = xmlRegExecPushString(exec, cur->name, NULL);
  4955. }
  4956. break;
  4957. default:
  4958. break;
  4959. }
  4960. /*
  4961. * Switch to next element
  4962. */
  4963. cur = cur->next;
  4964. while (cur == NULL) {
  4965. cur = nodeVPop(ctxt);
  4966. if (cur == NULL)
  4967. break;
  4968. cur = cur->next;
  4969. }
  4970. }
  4971. ret = xmlRegExecPushString(exec, NULL, NULL);
  4972. fail:
  4973. xmlRegFreeExecCtxt(exec);
  4974. }
  4975. }
  4976. #else /* LIBXML_REGEXP_ENABLED */
  4977. /*
  4978. * Allocate the stack
  4979. */
  4980. ctxt->vstateMax = 8;
  4981. ctxt->vstateTab = (xmlValidState *) xmlMalloc(
  4982. ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
  4983. if (ctxt->vstateTab == NULL) {
  4984. xmlVErrMemory(ctxt, "malloc failed");
  4985. return(-1);
  4986. }
  4987. /*
  4988. * The first entry in the stack is reserved to the current state
  4989. */
  4990. ctxt->nodeMax = 0;
  4991. ctxt->nodeNr = 0;
  4992. ctxt->nodeTab = NULL;
  4993. ctxt->vstate = &ctxt->vstateTab[0];
  4994. ctxt->vstateNr = 1;
  4995. CONT = cont;
  4996. NODE = child;
  4997. DEPTH = 0;
  4998. OCCURS = 0;
  4999. STATE = 0;
  5000. ret = xmlValidateElementType(ctxt);
  5001. if ((ret == -3) && (warn)) {
  5002. xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
  5003. "Content model for Element %s is ambiguous\n",
  5004. name, NULL, NULL);
  5005. } else if (ret == -2) {
  5006. /*
  5007. * An entities reference appeared at this level.
  5008. * Build a minimal representation of this node content
  5009. * sufficient to run the validation process on it
  5010. */
  5011. DEBUG_VALID_MSG("Found an entity reference, linearizing");
  5012. cur = child;
  5013. while (cur != NULL) {
  5014. switch (cur->type) {
  5015. case XML_ENTITY_REF_NODE:
  5016. /*
  5017. * Push the current node to be able to roll back
  5018. * and process within the entity
  5019. */
  5020. if ((cur->children != NULL) &&
  5021. (cur->children->children != NULL)) {
  5022. nodeVPush(ctxt, cur);
  5023. cur = cur->children->children;
  5024. continue;
  5025. }
  5026. break;
  5027. case XML_TEXT_NODE:
  5028. if (xmlIsBlankNode(cur))
  5029. break;
  5030. /* no break on purpose */
  5031. case XML_CDATA_SECTION_NODE:
  5032. /* no break on purpose */
  5033. case XML_ELEMENT_NODE:
  5034. /*
  5035. * Allocate a new node and minimally fills in
  5036. * what's required
  5037. */
  5038. tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  5039. if (tmp == NULL) {
  5040. xmlVErrMemory(ctxt, "malloc failed");
  5041. xmlFreeNodeList(repl);
  5042. ret = -1;
  5043. goto done;
  5044. }
  5045. tmp->type = cur->type;
  5046. tmp->name = cur->name;
  5047. tmp->ns = cur->ns;
  5048. tmp->next = NULL;
  5049. tmp->content = NULL;
  5050. if (repl == NULL)
  5051. repl = last = tmp;
  5052. else {
  5053. last->next = tmp;
  5054. last = tmp;
  5055. }
  5056. if (cur->type == XML_CDATA_SECTION_NODE) {
  5057. /*
  5058. * E59 spaces in CDATA does not match the
  5059. * nonterminal S
  5060. */
  5061. tmp->content = xmlStrdup(BAD_CAST "CDATA");
  5062. }
  5063. break;
  5064. default:
  5065. break;
  5066. }
  5067. /*
  5068. * Switch to next element
  5069. */
  5070. cur = cur->next;
  5071. while (cur == NULL) {
  5072. cur = nodeVPop(ctxt);
  5073. if (cur == NULL)
  5074. break;
  5075. cur = cur->next;
  5076. }
  5077. }
  5078. /*
  5079. * Relaunch the validation
  5080. */
  5081. ctxt->vstate = &ctxt->vstateTab[0];
  5082. ctxt->vstateNr = 1;
  5083. CONT = cont;
  5084. NODE = repl;
  5085. DEPTH = 0;
  5086. OCCURS = 0;
  5087. STATE = 0;
  5088. ret = xmlValidateElementType(ctxt);
  5089. }
  5090. #endif /* LIBXML_REGEXP_ENABLED */
  5091. if ((warn) && ((ret != 1) && (ret != -3))) {
  5092. if (ctxt != NULL) {
  5093. char expr[5000];
  5094. char list[5000];
  5095. expr[0] = 0;
  5096. xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
  5097. list[0] = 0;
  5098. #ifndef LIBXML_REGEXP_ENABLED
  5099. if (repl != NULL)
  5100. xmlSnprintfElements(&list[0], 5000, repl, 1);
  5101. else
  5102. #endif /* LIBXML_REGEXP_ENABLED */
  5103. xmlSnprintfElements(&list[0], 5000, child, 1);
  5104. if (name != NULL) {
  5105. xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
  5106. "Element %s content does not follow the DTD, expecting %s, got %s\n",
  5107. name, BAD_CAST expr, BAD_CAST list);
  5108. } else {
  5109. xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
  5110. "Element content does not follow the DTD, expecting %s, got %s\n",
  5111. BAD_CAST expr, BAD_CAST list, NULL);
  5112. }
  5113. } else {
  5114. if (name != NULL) {
  5115. xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
  5116. "Element %s content does not follow the DTD\n",
  5117. name, NULL, NULL);
  5118. } else {
  5119. xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
  5120. "Element content does not follow the DTD\n",
  5121. NULL, NULL, NULL);
  5122. }
  5123. }
  5124. ret = 0;
  5125. }
  5126. if (ret == -3)
  5127. ret = 1;
  5128. #ifndef LIBXML_REGEXP_ENABLED
  5129. done:
  5130. /*
  5131. * Deallocate the copy if done, and free up the validation stack
  5132. */
  5133. while (repl != NULL) {
  5134. tmp = repl->next;
  5135. xmlFree(repl);
  5136. repl = tmp;
  5137. }
  5138. ctxt->vstateMax = 0;
  5139. if (ctxt->vstateTab != NULL) {
  5140. xmlFree(ctxt->vstateTab);
  5141. ctxt->vstateTab = NULL;
  5142. }
  5143. #endif
  5144. ctxt->nodeMax = 0;
  5145. ctxt->nodeNr = 0;
  5146. if (ctxt->nodeTab != NULL) {
  5147. xmlFree(ctxt->nodeTab);
  5148. ctxt->nodeTab = NULL;
  5149. }
  5150. return(ret);
  5151. }
  5152. /**
  5153. * xmlValidateCdataElement:
  5154. * @ctxt: the validation context
  5155. * @doc: a document instance
  5156. * @elem: an element instance
  5157. *
  5158. * Check that an element follows #CDATA
  5159. *
  5160. * returns 1 if valid or 0 otherwise
  5161. */
  5162. static int
  5163. xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  5164. xmlNodePtr elem) {
  5165. int ret = 1;
  5166. xmlNodePtr cur, child;
  5167. if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) ||
  5168. (elem->type != XML_ELEMENT_NODE))
  5169. return(0);
  5170. child = elem->children;
  5171. cur = child;
  5172. while (cur != NULL) {
  5173. switch (cur->type) {
  5174. case XML_ENTITY_REF_NODE:
  5175. /*
  5176. * Push the current node to be able to roll back
  5177. * and process within the entity
  5178. */
  5179. if ((cur->children != NULL) &&
  5180. (cur->children->children != NULL)) {
  5181. nodeVPush(ctxt, cur);
  5182. cur = cur->children->children;
  5183. continue;
  5184. }
  5185. break;
  5186. case XML_COMMENT_NODE:
  5187. case XML_PI_NODE:
  5188. case XML_TEXT_NODE:
  5189. case XML_CDATA_SECTION_NODE:
  5190. break;
  5191. default:
  5192. ret = 0;
  5193. goto done;
  5194. }
  5195. /*
  5196. * Switch to next element
  5197. */
  5198. cur = cur->next;
  5199. while (cur == NULL) {
  5200. cur = nodeVPop(ctxt);
  5201. if (cur == NULL)
  5202. break;
  5203. cur = cur->next;
  5204. }
  5205. }
  5206. done:
  5207. ctxt->nodeMax = 0;
  5208. ctxt->nodeNr = 0;
  5209. if (ctxt->nodeTab != NULL) {
  5210. xmlFree(ctxt->nodeTab);
  5211. ctxt->nodeTab = NULL;
  5212. }
  5213. return(ret);
  5214. }
  5215. /**
  5216. * xmlValidateCheckMixed:
  5217. * @ctxt: the validation context
  5218. * @cont: the mixed content model
  5219. * @qname: the qualified name as appearing in the serialization
  5220. *
  5221. * Check if the given node is part of the content model.
  5222. *
  5223. * Returns 1 if yes, 0 if no, -1 in case of error
  5224. */
  5225. static int
  5226. xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
  5227. xmlElementContentPtr cont, const xmlChar *qname) {
  5228. const xmlChar *name;
  5229. int plen;
  5230. name = xmlSplitQName3(qname, &plen);
  5231. if (name == NULL) {
  5232. while (cont != NULL) {
  5233. if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
  5234. if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
  5235. return(1);
  5236. } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
  5237. (cont->c1 != NULL) &&
  5238. (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
  5239. if ((cont->c1->prefix == NULL) &&
  5240. (xmlStrEqual(cont->c1->name, qname)))
  5241. return(1);
  5242. } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
  5243. (cont->c1 == NULL) ||
  5244. (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
  5245. xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
  5246. "Internal: MIXED struct corrupted\n",
  5247. NULL);
  5248. break;
  5249. }
  5250. cont = cont->c2;
  5251. }
  5252. } else {
  5253. while (cont != NULL) {
  5254. if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
  5255. if ((cont->prefix != NULL) &&
  5256. (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
  5257. (xmlStrEqual(cont->name, name)))
  5258. return(1);
  5259. } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
  5260. (cont->c1 != NULL) &&
  5261. (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
  5262. if ((cont->c1->prefix != NULL) &&
  5263. (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
  5264. (xmlStrEqual(cont->c1->name, name)))
  5265. return(1);
  5266. } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
  5267. (cont->c1 == NULL) ||
  5268. (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
  5269. xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
  5270. "Internal: MIXED struct corrupted\n",
  5271. NULL);
  5272. break;
  5273. }
  5274. cont = cont->c2;
  5275. }
  5276. }
  5277. return(0);
  5278. }
  5279. /**
  5280. * xmlValidGetElemDecl:
  5281. * @ctxt: the validation context
  5282. * @doc: a document instance
  5283. * @elem: an element instance
  5284. * @extsubset: pointer, (out) indicate if the declaration was found
  5285. * in the external subset.
  5286. *
  5287. * Finds a declaration associated to an element in the document.
  5288. *
  5289. * returns the pointer to the declaration or NULL if not found.
  5290. */
  5291. static xmlElementPtr
  5292. xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  5293. xmlNodePtr elem, int *extsubset) {
  5294. xmlElementPtr elemDecl = NULL;
  5295. const xmlChar *prefix = NULL;
  5296. if ((ctxt == NULL) || (doc == NULL) ||
  5297. (elem == NULL) || (elem->name == NULL))
  5298. return(NULL);
  5299. if (extsubset != NULL)
  5300. *extsubset = 0;
  5301. /*
  5302. * Fetch the declaration for the qualified name
  5303. */
  5304. if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
  5305. prefix = elem->ns->prefix;
  5306. if (prefix != NULL) {
  5307. elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
  5308. elem->name, prefix);
  5309. if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
  5310. elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
  5311. elem->name, prefix);
  5312. if ((elemDecl != NULL) && (extsubset != NULL))
  5313. *extsubset = 1;
  5314. }
  5315. }
  5316. /*
  5317. * Fetch the declaration for the non qualified name
  5318. * This is "non-strict" validation should be done on the
  5319. * full QName but in that case being flexible makes sense.
  5320. */
  5321. if (elemDecl == NULL) {
  5322. elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
  5323. if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
  5324. elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
  5325. if ((elemDecl != NULL) && (extsubset != NULL))
  5326. *extsubset = 1;
  5327. }
  5328. }
  5329. if (elemDecl == NULL) {
  5330. xmlErrValidNode(ctxt, elem,
  5331. XML_DTD_UNKNOWN_ELEM,
  5332. "No declaration for element %s\n",
  5333. elem->name, NULL, NULL);
  5334. }
  5335. return(elemDecl);
  5336. }
  5337. #ifdef LIBXML_REGEXP_ENABLED
  5338. /**
  5339. * xmlValidatePushElement:
  5340. * @ctxt: the validation context
  5341. * @doc: a document instance
  5342. * @elem: an element instance
  5343. * @qname: the qualified name as appearing in the serialization
  5344. *
  5345. * Push a new element start on the validation stack.
  5346. *
  5347. * returns 1 if no validation problem was found or 0 otherwise
  5348. */
  5349. int
  5350. xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  5351. xmlNodePtr elem, const xmlChar *qname) {
  5352. int ret = 1;
  5353. xmlElementPtr eDecl;
  5354. int extsubset = 0;
  5355. if (ctxt == NULL)
  5356. return(0);
  5357. /* printf("PushElem %s\n", qname); */
  5358. if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
  5359. xmlValidStatePtr state = ctxt->vstate;
  5360. xmlElementPtr elemDecl;
  5361. /*
  5362. * Check the new element against the content model of the new elem.
  5363. */
  5364. if (state->elemDecl != NULL) {
  5365. elemDecl = state->elemDecl;
  5366. switch(elemDecl->etype) {
  5367. case XML_ELEMENT_TYPE_UNDEFINED:
  5368. ret = 0;
  5369. break;
  5370. case XML_ELEMENT_TYPE_EMPTY:
  5371. xmlErrValidNode(ctxt, state->node,
  5372. XML_DTD_NOT_EMPTY,
  5373. "Element %s was declared EMPTY this one has content\n",
  5374. state->node->name, NULL, NULL);
  5375. ret = 0;
  5376. break;
  5377. case XML_ELEMENT_TYPE_ANY:
  5378. /* I don't think anything is required then */
  5379. break;
  5380. case XML_ELEMENT_TYPE_MIXED:
  5381. /* simple case of declared as #PCDATA */
  5382. if ((elemDecl->content != NULL) &&
  5383. (elemDecl->content->type ==
  5384. XML_ELEMENT_CONTENT_PCDATA)) {
  5385. xmlErrValidNode(ctxt, state->node,
  5386. XML_DTD_NOT_PCDATA,
  5387. "Element %s was declared #PCDATA but contains non text nodes\n",
  5388. state->node->name, NULL, NULL);
  5389. ret = 0;
  5390. } else {
  5391. ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
  5392. qname);
  5393. if (ret != 1) {
  5394. xmlErrValidNode(ctxt, state->node,
  5395. XML_DTD_INVALID_CHILD,
  5396. "Element %s is not declared in %s list of possible children\n",
  5397. qname, state->node->name, NULL);
  5398. }
  5399. }
  5400. break;
  5401. case XML_ELEMENT_TYPE_ELEMENT:
  5402. /*
  5403. * TODO:
  5404. * VC: Standalone Document Declaration
  5405. * - element types with element content, if white space
  5406. * occurs directly within any instance of those types.
  5407. */
  5408. if (state->exec != NULL) {
  5409. ret = xmlRegExecPushString(state->exec, qname, NULL);
  5410. if (ret < 0) {
  5411. xmlErrValidNode(ctxt, state->node,
  5412. XML_DTD_CONTENT_MODEL,
  5413. "Element %s content does not follow the DTD, Misplaced %s\n",
  5414. state->node->name, qname, NULL);
  5415. ret = 0;
  5416. } else {
  5417. ret = 1;
  5418. }
  5419. }
  5420. break;
  5421. }
  5422. }
  5423. }
  5424. eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
  5425. vstateVPush(ctxt, eDecl, elem);
  5426. return(ret);
  5427. }
  5428. /**
  5429. * xmlValidatePushCData:
  5430. * @ctxt: the validation context
  5431. * @data: some character data read
  5432. * @len: the length of the data
  5433. *
  5434. * check the CData parsed for validation in the current stack
  5435. *
  5436. * returns 1 if no validation problem was found or 0 otherwise
  5437. */
  5438. int
  5439. xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
  5440. int ret = 1;
  5441. /* printf("CDATA %s %d\n", data, len); */
  5442. if (ctxt == NULL)
  5443. return(0);
  5444. if (len <= 0)
  5445. return(ret);
  5446. if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
  5447. xmlValidStatePtr state = ctxt->vstate;
  5448. xmlElementPtr elemDecl;
  5449. /*
  5450. * Check the new element against the content model of the new elem.
  5451. */
  5452. if (state->elemDecl != NULL) {
  5453. elemDecl = state->elemDecl;
  5454. switch(elemDecl->etype) {
  5455. case XML_ELEMENT_TYPE_UNDEFINED:
  5456. ret = 0;
  5457. break;
  5458. case XML_ELEMENT_TYPE_EMPTY:
  5459. xmlErrValidNode(ctxt, state->node,
  5460. XML_DTD_NOT_EMPTY,
  5461. "Element %s was declared EMPTY this one has content\n",
  5462. state->node->name, NULL, NULL);
  5463. ret = 0;
  5464. break;
  5465. case XML_ELEMENT_TYPE_ANY:
  5466. break;
  5467. case XML_ELEMENT_TYPE_MIXED:
  5468. break;
  5469. case XML_ELEMENT_TYPE_ELEMENT: {
  5470. int i;
  5471. for (i = 0;i < len;i++) {
  5472. if (!IS_BLANK_CH(data[i])) {
  5473. xmlErrValidNode(ctxt, state->node,
  5474. XML_DTD_CONTENT_MODEL,
  5475. "Element %s content does not follow the DTD, Text not allowed\n",
  5476. state->node->name, NULL, NULL);
  5477. ret = 0;
  5478. goto done;
  5479. }
  5480. }
  5481. /*
  5482. * TODO:
  5483. * VC: Standalone Document Declaration
  5484. * element types with element content, if white space
  5485. * occurs directly within any instance of those types.
  5486. */
  5487. break;
  5488. }
  5489. }
  5490. }
  5491. }
  5492. done:
  5493. return(ret);
  5494. }
  5495. /**
  5496. * xmlValidatePopElement:
  5497. * @ctxt: the validation context
  5498. * @doc: a document instance
  5499. * @elem: an element instance
  5500. * @qname: the qualified name as appearing in the serialization
  5501. *
  5502. * Pop the element end from the validation stack.
  5503. *
  5504. * returns 1 if no validation problem was found or 0 otherwise
  5505. */
  5506. int
  5507. xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
  5508. xmlNodePtr elem ATTRIBUTE_UNUSED,
  5509. const xmlChar *qname ATTRIBUTE_UNUSED) {
  5510. int ret = 1;
  5511. if (ctxt == NULL)
  5512. return(0);
  5513. /* printf("PopElem %s\n", qname); */
  5514. if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
  5515. xmlValidStatePtr state = ctxt->vstate;
  5516. xmlElementPtr elemDecl;
  5517. /*
  5518. * Check the new element against the content model of the new elem.
  5519. */
  5520. if (state->elemDecl != NULL) {
  5521. elemDecl = state->elemDecl;
  5522. if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
  5523. if (state->exec != NULL) {
  5524. ret = xmlRegExecPushString(state->exec, NULL, NULL);
  5525. if (ret == 0) {
  5526. xmlErrValidNode(ctxt, state->node,
  5527. XML_DTD_CONTENT_MODEL,
  5528. "Element %s content does not follow the DTD, Expecting more child\n",
  5529. state->node->name, NULL,NULL);
  5530. } else {
  5531. /*
  5532. * previous validation errors should not generate
  5533. * a new one here
  5534. */
  5535. ret = 1;
  5536. }
  5537. }
  5538. }
  5539. }
  5540. vstateVPop(ctxt);
  5541. }
  5542. return(ret);
  5543. }
  5544. #endif /* LIBXML_REGEXP_ENABLED */
  5545. /**
  5546. * xmlValidateOneElement:
  5547. * @ctxt: the validation context
  5548. * @doc: a document instance
  5549. * @elem: an element instance
  5550. *
  5551. * Try to validate a single element and it's attributes,
  5552. * basically it does the following checks as described by the
  5553. * XML-1.0 recommendation:
  5554. * - [ VC: Element Valid ]
  5555. * - [ VC: Required Attribute ]
  5556. * Then call xmlValidateOneAttribute() for each attribute present.
  5557. *
  5558. * The ID/IDREF checkings are done separately
  5559. *
  5560. * returns 1 if valid or 0 otherwise
  5561. */
  5562. int
  5563. xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  5564. xmlNodePtr elem) {
  5565. xmlElementPtr elemDecl = NULL;
  5566. xmlElementContentPtr cont;
  5567. xmlAttributePtr attr;
  5568. xmlNodePtr child;
  5569. int ret = 1, tmp;
  5570. const xmlChar *name;
  5571. int extsubset = 0;
  5572. CHECK_DTD;
  5573. if (elem == NULL) return(0);
  5574. switch (elem->type) {
  5575. case XML_ATTRIBUTE_NODE:
  5576. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5577. "Attribute element not expected\n", NULL, NULL ,NULL);
  5578. return(0);
  5579. case XML_TEXT_NODE:
  5580. if (elem->children != NULL) {
  5581. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5582. "Text element has children !\n",
  5583. NULL,NULL,NULL);
  5584. return(0);
  5585. }
  5586. if (elem->ns != NULL) {
  5587. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5588. "Text element has namespace !\n",
  5589. NULL,NULL,NULL);
  5590. return(0);
  5591. }
  5592. if (elem->content == NULL) {
  5593. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5594. "Text element has no content !\n",
  5595. NULL,NULL,NULL);
  5596. return(0);
  5597. }
  5598. return(1);
  5599. case XML_XINCLUDE_START:
  5600. case XML_XINCLUDE_END:
  5601. return(1);
  5602. case XML_CDATA_SECTION_NODE:
  5603. case XML_ENTITY_REF_NODE:
  5604. case XML_PI_NODE:
  5605. case XML_COMMENT_NODE:
  5606. return(1);
  5607. case XML_ENTITY_NODE:
  5608. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5609. "Entity element not expected\n", NULL, NULL ,NULL);
  5610. return(0);
  5611. case XML_NOTATION_NODE:
  5612. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5613. "Notation element not expected\n", NULL, NULL ,NULL);
  5614. return(0);
  5615. case XML_DOCUMENT_NODE:
  5616. case XML_DOCUMENT_TYPE_NODE:
  5617. case XML_DOCUMENT_FRAG_NODE:
  5618. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5619. "Document element not expected\n", NULL, NULL ,NULL);
  5620. return(0);
  5621. case XML_HTML_DOCUMENT_NODE:
  5622. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5623. "HTML Document not expected\n", NULL, NULL ,NULL);
  5624. return(0);
  5625. case XML_ELEMENT_NODE:
  5626. break;
  5627. default:
  5628. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5629. "unknown element type\n", NULL, NULL ,NULL);
  5630. return(0);
  5631. }
  5632. /*
  5633. * Fetch the declaration
  5634. */
  5635. elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
  5636. if (elemDecl == NULL)
  5637. return(0);
  5638. /*
  5639. * If vstateNr is not zero that means continuous validation is
  5640. * activated, do not try to check the content model at that level.
  5641. */
  5642. if (ctxt->vstateNr == 0) {
  5643. /* Check that the element content matches the definition */
  5644. switch (elemDecl->etype) {
  5645. case XML_ELEMENT_TYPE_UNDEFINED:
  5646. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
  5647. "No declaration for element %s\n",
  5648. elem->name, NULL, NULL);
  5649. return(0);
  5650. case XML_ELEMENT_TYPE_EMPTY:
  5651. if (elem->children != NULL) {
  5652. xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
  5653. "Element %s was declared EMPTY this one has content\n",
  5654. elem->name, NULL, NULL);
  5655. ret = 0;
  5656. }
  5657. break;
  5658. case XML_ELEMENT_TYPE_ANY:
  5659. /* I don't think anything is required then */
  5660. break;
  5661. case XML_ELEMENT_TYPE_MIXED:
  5662. /* simple case of declared as #PCDATA */
  5663. if ((elemDecl->content != NULL) &&
  5664. (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
  5665. ret = xmlValidateOneCdataElement(ctxt, doc, elem);
  5666. if (!ret) {
  5667. xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
  5668. "Element %s was declared #PCDATA but contains non text nodes\n",
  5669. elem->name, NULL, NULL);
  5670. }
  5671. break;
  5672. }
  5673. child = elem->children;
  5674. /* Hum, this start to get messy */
  5675. while (child != NULL) {
  5676. if (child->type == XML_ELEMENT_NODE) {
  5677. name = child->name;
  5678. if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
  5679. xmlChar fn[50];
  5680. xmlChar *fullname;
  5681. fullname = xmlBuildQName(child->name, child->ns->prefix,
  5682. fn, 50);
  5683. if (fullname == NULL)
  5684. return(0);
  5685. cont = elemDecl->content;
  5686. while (cont != NULL) {
  5687. if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
  5688. if (xmlStrEqual(cont->name, fullname))
  5689. break;
  5690. } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
  5691. (cont->c1 != NULL) &&
  5692. (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
  5693. if (xmlStrEqual(cont->c1->name, fullname))
  5694. break;
  5695. } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
  5696. (cont->c1 == NULL) ||
  5697. (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
  5698. xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
  5699. "Internal: MIXED struct corrupted\n",
  5700. NULL);
  5701. break;
  5702. }
  5703. cont = cont->c2;
  5704. }
  5705. if ((fullname != fn) && (fullname != child->name))
  5706. xmlFree(fullname);
  5707. if (cont != NULL)
  5708. goto child_ok;
  5709. }
  5710. cont = elemDecl->content;
  5711. while (cont != NULL) {
  5712. if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
  5713. if (xmlStrEqual(cont->name, name)) break;
  5714. } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
  5715. (cont->c1 != NULL) &&
  5716. (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
  5717. if (xmlStrEqual(cont->c1->name, name)) break;
  5718. } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
  5719. (cont->c1 == NULL) ||
  5720. (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
  5721. xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
  5722. "Internal: MIXED struct corrupted\n",
  5723. NULL);
  5724. break;
  5725. }
  5726. cont = cont->c2;
  5727. }
  5728. if (cont == NULL) {
  5729. xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
  5730. "Element %s is not declared in %s list of possible children\n",
  5731. name, elem->name, NULL);
  5732. ret = 0;
  5733. }
  5734. }
  5735. child_ok:
  5736. child = child->next;
  5737. }
  5738. break;
  5739. case XML_ELEMENT_TYPE_ELEMENT:
  5740. if ((doc->standalone == 1) && (extsubset == 1)) {
  5741. /*
  5742. * VC: Standalone Document Declaration
  5743. * - element types with element content, if white space
  5744. * occurs directly within any instance of those types.
  5745. */
  5746. child = elem->children;
  5747. while (child != NULL) {
  5748. if (child->type == XML_TEXT_NODE) {
  5749. const xmlChar *content = child->content;
  5750. while (IS_BLANK_CH(*content))
  5751. content++;
  5752. if (*content == 0) {
  5753. xmlErrValidNode(ctxt, elem,
  5754. XML_DTD_STANDALONE_WHITE_SPACE,
  5755. "standalone: %s declared in the external subset contains white spaces nodes\n",
  5756. elem->name, NULL, NULL);
  5757. ret = 0;
  5758. break;
  5759. }
  5760. }
  5761. child =child->next;
  5762. }
  5763. }
  5764. child = elem->children;
  5765. cont = elemDecl->content;
  5766. tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
  5767. if (tmp <= 0)
  5768. ret = tmp;
  5769. break;
  5770. }
  5771. } /* not continuous */
  5772. /* [ VC: Required Attribute ] */
  5773. attr = elemDecl->attributes;
  5774. while (attr != NULL) {
  5775. if (attr->def == XML_ATTRIBUTE_REQUIRED) {
  5776. int qualified = -1;
  5777. if ((attr->prefix == NULL) &&
  5778. (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
  5779. xmlNsPtr ns;
  5780. ns = elem->nsDef;
  5781. while (ns != NULL) {
  5782. if (ns->prefix == NULL)
  5783. goto found;
  5784. ns = ns->next;
  5785. }
  5786. } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
  5787. xmlNsPtr ns;
  5788. ns = elem->nsDef;
  5789. while (ns != NULL) {
  5790. if (xmlStrEqual(attr->name, ns->prefix))
  5791. goto found;
  5792. ns = ns->next;
  5793. }
  5794. } else {
  5795. xmlAttrPtr attrib;
  5796. attrib = elem->properties;
  5797. while (attrib != NULL) {
  5798. if (xmlStrEqual(attrib->name, attr->name)) {
  5799. if (attr->prefix != NULL) {
  5800. xmlNsPtr nameSpace = attrib->ns;
  5801. if (nameSpace == NULL)
  5802. nameSpace = elem->ns;
  5803. /*
  5804. * qualified names handling is problematic, having a
  5805. * different prefix should be possible but DTDs don't
  5806. * allow to define the URI instead of the prefix :-(
  5807. */
  5808. if (nameSpace == NULL) {
  5809. if (qualified < 0)
  5810. qualified = 0;
  5811. } else if (!xmlStrEqual(nameSpace->prefix,
  5812. attr->prefix)) {
  5813. if (qualified < 1)
  5814. qualified = 1;
  5815. } else
  5816. goto found;
  5817. } else {
  5818. /*
  5819. * We should allow applications to define namespaces
  5820. * for their application even if the DTD doesn't
  5821. * carry one, otherwise, basically we would always
  5822. * break.
  5823. */
  5824. goto found;
  5825. }
  5826. }
  5827. attrib = attrib->next;
  5828. }
  5829. }
  5830. if (qualified == -1) {
  5831. if (attr->prefix == NULL) {
  5832. xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
  5833. "Element %s does not carry attribute %s\n",
  5834. elem->name, attr->name, NULL);
  5835. ret = 0;
  5836. } else {
  5837. xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
  5838. "Element %s does not carry attribute %s:%s\n",
  5839. elem->name, attr->prefix,attr->name);
  5840. ret = 0;
  5841. }
  5842. } else if (qualified == 0) {
  5843. xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
  5844. "Element %s required attribute %s:%s has no prefix\n",
  5845. elem->name, attr->prefix, attr->name);
  5846. } else if (qualified == 1) {
  5847. xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
  5848. "Element %s required attribute %s:%s has different prefix\n",
  5849. elem->name, attr->prefix, attr->name);
  5850. }
  5851. } else if (attr->def == XML_ATTRIBUTE_FIXED) {
  5852. /*
  5853. * Special tests checking #FIXED namespace declarations
  5854. * have the right value since this is not done as an
  5855. * attribute checking
  5856. */
  5857. if ((attr->prefix == NULL) &&
  5858. (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
  5859. xmlNsPtr ns;
  5860. ns = elem->nsDef;
  5861. while (ns != NULL) {
  5862. if (ns->prefix == NULL) {
  5863. if (!xmlStrEqual(attr->defaultValue, ns->href)) {
  5864. xmlErrValidNode(ctxt, elem,
  5865. XML_DTD_ELEM_DEFAULT_NAMESPACE,
  5866. "Element %s namespace name for default namespace does not match the DTD\n",
  5867. elem->name, NULL, NULL);
  5868. ret = 0;
  5869. }
  5870. goto found;
  5871. }
  5872. ns = ns->next;
  5873. }
  5874. } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
  5875. xmlNsPtr ns;
  5876. ns = elem->nsDef;
  5877. while (ns != NULL) {
  5878. if (xmlStrEqual(attr->name, ns->prefix)) {
  5879. if (!xmlStrEqual(attr->defaultValue, ns->href)) {
  5880. xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
  5881. "Element %s namespace name for %s does not match the DTD\n",
  5882. elem->name, ns->prefix, NULL);
  5883. ret = 0;
  5884. }
  5885. goto found;
  5886. }
  5887. ns = ns->next;
  5888. }
  5889. }
  5890. }
  5891. found:
  5892. attr = attr->nexth;
  5893. }
  5894. return(ret);
  5895. }
  5896. /**
  5897. * xmlValidateRoot:
  5898. * @ctxt: the validation context
  5899. * @doc: a document instance
  5900. *
  5901. * Try to validate a the root element
  5902. * basically it does the following check as described by the
  5903. * XML-1.0 recommendation:
  5904. * - [ VC: Root Element Type ]
  5905. * it doesn't try to recurse or apply other check to the element
  5906. *
  5907. * returns 1 if valid or 0 otherwise
  5908. */
  5909. int
  5910. xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
  5911. xmlNodePtr root;
  5912. int ret;
  5913. if (doc == NULL) return(0);
  5914. root = xmlDocGetRootElement(doc);
  5915. if ((root == NULL) || (root->name == NULL)) {
  5916. xmlErrValid(ctxt, XML_DTD_NO_ROOT,
  5917. "no root element\n", NULL);
  5918. return(0);
  5919. }
  5920. /*
  5921. * When doing post validation against a separate DTD, those may
  5922. * no internal subset has been generated
  5923. */
  5924. if ((doc->intSubset != NULL) &&
  5925. (doc->intSubset->name != NULL)) {
  5926. /*
  5927. * Check first the document root against the NQName
  5928. */
  5929. if (!xmlStrEqual(doc->intSubset->name, root->name)) {
  5930. if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
  5931. xmlChar fn[50];
  5932. xmlChar *fullname;
  5933. fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
  5934. if (fullname == NULL) {
  5935. xmlVErrMemory(ctxt, NULL);
  5936. return(0);
  5937. }
  5938. ret = xmlStrEqual(doc->intSubset->name, fullname);
  5939. if ((fullname != fn) && (fullname != root->name))
  5940. xmlFree(fullname);
  5941. if (ret == 1)
  5942. goto name_ok;
  5943. }
  5944. if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
  5945. (xmlStrEqual(root->name, BAD_CAST "html")))
  5946. goto name_ok;
  5947. xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
  5948. "root and DTD name do not match '%s' and '%s'\n",
  5949. root->name, doc->intSubset->name, NULL);
  5950. return(0);
  5951. }
  5952. }
  5953. name_ok:
  5954. return(1);
  5955. }
  5956. /**
  5957. * xmlValidateElement:
  5958. * @ctxt: the validation context
  5959. * @doc: a document instance
  5960. * @elem: an element instance
  5961. *
  5962. * Try to validate the subtree under an element
  5963. *
  5964. * returns 1 if valid or 0 otherwise
  5965. */
  5966. int
  5967. xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
  5968. xmlNodePtr child;
  5969. xmlAttrPtr attr;
  5970. xmlNsPtr ns;
  5971. const xmlChar *value;
  5972. int ret = 1;
  5973. if (elem == NULL) return(0);
  5974. /*
  5975. * XInclude elements were added after parsing in the infoset,
  5976. * they don't really mean anything validation wise.
  5977. */
  5978. if ((elem->type == XML_XINCLUDE_START) ||
  5979. (elem->type == XML_XINCLUDE_END) ||
  5980. (elem->type == XML_NAMESPACE_DECL))
  5981. return(1);
  5982. CHECK_DTD;
  5983. /*
  5984. * Entities references have to be handled separately
  5985. */
  5986. if (elem->type == XML_ENTITY_REF_NODE) {
  5987. return(1);
  5988. }
  5989. ret &= xmlValidateOneElement(ctxt, doc, elem);
  5990. if (elem->type == XML_ELEMENT_NODE) {
  5991. attr = elem->properties;
  5992. while (attr != NULL) {
  5993. value = xmlNodeListGetString(doc, attr->children, 0);
  5994. ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
  5995. if (value != NULL)
  5996. xmlFree((char *)value);
  5997. attr= attr->next;
  5998. }
  5999. ns = elem->nsDef;
  6000. while (ns != NULL) {
  6001. if (elem->ns == NULL)
  6002. ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
  6003. ns, ns->href);
  6004. else
  6005. ret &= xmlValidateOneNamespace(ctxt, doc, elem,
  6006. elem->ns->prefix, ns, ns->href);
  6007. ns = ns->next;
  6008. }
  6009. }
  6010. child = elem->children;
  6011. while (child != NULL) {
  6012. ret &= xmlValidateElement(ctxt, doc, child);
  6013. child = child->next;
  6014. }
  6015. return(ret);
  6016. }
  6017. /**
  6018. * xmlValidateRef:
  6019. * @ref: A reference to be validated
  6020. * @ctxt: Validation context
  6021. * @name: Name of ID we are searching for
  6022. *
  6023. */
  6024. static void
  6025. xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
  6026. const xmlChar *name) {
  6027. xmlAttrPtr id;
  6028. xmlAttrPtr attr;
  6029. if (ref == NULL)
  6030. return;
  6031. if ((ref->attr == NULL) && (ref->name == NULL))
  6032. return;
  6033. attr = ref->attr;
  6034. if (attr == NULL) {
  6035. xmlChar *dup, *str = NULL, *cur, save;
  6036. dup = xmlStrdup(name);
  6037. if (dup == NULL) {
  6038. ctxt->valid = 0;
  6039. return;
  6040. }
  6041. cur = dup;
  6042. while (*cur != 0) {
  6043. str = cur;
  6044. while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
  6045. save = *cur;
  6046. *cur = 0;
  6047. id = xmlGetID(ctxt->doc, str);
  6048. if (id == NULL) {
  6049. xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
  6050. "attribute %s line %d references an unknown ID \"%s\"\n",
  6051. ref->name, ref->lineno, str);
  6052. ctxt->valid = 0;
  6053. }
  6054. if (save == 0)
  6055. break;
  6056. *cur = save;
  6057. while (IS_BLANK_CH(*cur)) cur++;
  6058. }
  6059. xmlFree(dup);
  6060. } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
  6061. id = xmlGetID(ctxt->doc, name);
  6062. if (id == NULL) {
  6063. xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
  6064. "IDREF attribute %s references an unknown ID \"%s\"\n",
  6065. attr->name, name, NULL);
  6066. ctxt->valid = 0;
  6067. }
  6068. } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
  6069. xmlChar *dup, *str = NULL, *cur, save;
  6070. dup = xmlStrdup(name);
  6071. if (dup == NULL) {
  6072. xmlVErrMemory(ctxt, "IDREFS split");
  6073. ctxt->valid = 0;
  6074. return;
  6075. }
  6076. cur = dup;
  6077. while (*cur != 0) {
  6078. str = cur;
  6079. while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
  6080. save = *cur;
  6081. *cur = 0;
  6082. id = xmlGetID(ctxt->doc, str);
  6083. if (id == NULL) {
  6084. xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
  6085. "IDREFS attribute %s references an unknown ID \"%s\"\n",
  6086. attr->name, str, NULL);
  6087. ctxt->valid = 0;
  6088. }
  6089. if (save == 0)
  6090. break;
  6091. *cur = save;
  6092. while (IS_BLANK_CH(*cur)) cur++;
  6093. }
  6094. xmlFree(dup);
  6095. }
  6096. }
  6097. /**
  6098. * xmlWalkValidateList:
  6099. * @data: Contents of current link
  6100. * @user: Value supplied by the user
  6101. *
  6102. * Returns 0 to abort the walk or 1 to continue
  6103. */
  6104. static int
  6105. xmlWalkValidateList(const void *data, void *user)
  6106. {
  6107. xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
  6108. xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
  6109. return 1;
  6110. }
  6111. /**
  6112. * xmlValidateCheckRefCallback:
  6113. * @ref_list: List of references
  6114. * @ctxt: Validation context
  6115. * @name: Name of ID we are searching for
  6116. *
  6117. */
  6118. static void
  6119. xmlValidateCheckRefCallback(void *payload, void *data, const xmlChar *name) {
  6120. xmlListPtr ref_list = (xmlListPtr) payload;
  6121. xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data;
  6122. xmlValidateMemo memo;
  6123. if (ref_list == NULL)
  6124. return;
  6125. memo.ctxt = ctxt;
  6126. memo.name = name;
  6127. xmlListWalk(ref_list, xmlWalkValidateList, &memo);
  6128. }
  6129. /**
  6130. * xmlValidateDocumentFinal:
  6131. * @ctxt: the validation context
  6132. * @doc: a document instance
  6133. *
  6134. * Does the final step for the document validation once all the
  6135. * incremental validation steps have been completed
  6136. *
  6137. * basically it does the following checks described by the XML Rec
  6138. *
  6139. * Check all the IDREF/IDREFS attributes definition for validity
  6140. *
  6141. * returns 1 if valid or 0 otherwise
  6142. */
  6143. int
  6144. xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
  6145. xmlRefTablePtr table;
  6146. unsigned int save;
  6147. if (ctxt == NULL)
  6148. return(0);
  6149. if (doc == NULL) {
  6150. xmlErrValid(ctxt, XML_DTD_NO_DOC,
  6151. "xmlValidateDocumentFinal: doc == NULL\n", NULL);
  6152. return(0);
  6153. }
  6154. /* trick to get correct line id report */
  6155. save = ctxt->finishDtd;
  6156. ctxt->finishDtd = 0;
  6157. /*
  6158. * Check all the NOTATION/NOTATIONS attributes
  6159. */
  6160. /*
  6161. * Check all the ENTITY/ENTITIES attributes definition for validity
  6162. */
  6163. /*
  6164. * Check all the IDREF/IDREFS attributes definition for validity
  6165. */
  6166. table = (xmlRefTablePtr) doc->refs;
  6167. ctxt->doc = doc;
  6168. ctxt->valid = 1;
  6169. xmlHashScan(table, xmlValidateCheckRefCallback, ctxt);
  6170. ctxt->finishDtd = save;
  6171. return(ctxt->valid);
  6172. }
  6173. /**
  6174. * xmlValidateDtd:
  6175. * @ctxt: the validation context
  6176. * @doc: a document instance
  6177. * @dtd: a dtd instance
  6178. *
  6179. * Try to validate the document against the dtd instance
  6180. *
  6181. * Basically it does check all the definitions in the DtD.
  6182. * Note the the internal subset (if present) is de-coupled
  6183. * (i.e. not used), which could give problems if ID or IDREF
  6184. * is present.
  6185. *
  6186. * returns 1 if valid or 0 otherwise
  6187. */
  6188. int
  6189. xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
  6190. int ret;
  6191. xmlDtdPtr oldExt, oldInt;
  6192. xmlNodePtr root;
  6193. if (dtd == NULL) return(0);
  6194. if (doc == NULL) return(0);
  6195. oldExt = doc->extSubset;
  6196. oldInt = doc->intSubset;
  6197. doc->extSubset = dtd;
  6198. doc->intSubset = NULL;
  6199. ret = xmlValidateRoot(ctxt, doc);
  6200. if (ret == 0) {
  6201. doc->extSubset = oldExt;
  6202. doc->intSubset = oldInt;
  6203. return(ret);
  6204. }
  6205. if (doc->ids != NULL) {
  6206. xmlFreeIDTable(doc->ids);
  6207. doc->ids = NULL;
  6208. }
  6209. if (doc->refs != NULL) {
  6210. xmlFreeRefTable(doc->refs);
  6211. doc->refs = NULL;
  6212. }
  6213. root = xmlDocGetRootElement(doc);
  6214. ret = xmlValidateElement(ctxt, doc, root);
  6215. ret &= xmlValidateDocumentFinal(ctxt, doc);
  6216. doc->extSubset = oldExt;
  6217. doc->intSubset = oldInt;
  6218. return(ret);
  6219. }
  6220. static void
  6221. xmlValidateNotationCallback(void *payload, void *data,
  6222. const xmlChar *name ATTRIBUTE_UNUSED) {
  6223. xmlEntityPtr cur = (xmlEntityPtr) payload;
  6224. xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data;
  6225. if (cur == NULL)
  6226. return;
  6227. if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
  6228. xmlChar *notation = cur->content;
  6229. if (notation != NULL) {
  6230. int ret;
  6231. ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
  6232. if (ret != 1) {
  6233. ctxt->valid = 0;
  6234. }
  6235. }
  6236. }
  6237. }
  6238. static void
  6239. xmlValidateAttributeCallback(void *payload, void *data,
  6240. const xmlChar *name ATTRIBUTE_UNUSED) {
  6241. xmlAttributePtr cur = (xmlAttributePtr) payload;
  6242. xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data;
  6243. int ret;
  6244. xmlDocPtr doc;
  6245. xmlElementPtr elem = NULL;
  6246. if (cur == NULL)
  6247. return;
  6248. switch (cur->atype) {
  6249. case XML_ATTRIBUTE_CDATA:
  6250. case XML_ATTRIBUTE_ID:
  6251. case XML_ATTRIBUTE_IDREF :
  6252. case XML_ATTRIBUTE_IDREFS:
  6253. case XML_ATTRIBUTE_NMTOKEN:
  6254. case XML_ATTRIBUTE_NMTOKENS:
  6255. case XML_ATTRIBUTE_ENUMERATION:
  6256. break;
  6257. case XML_ATTRIBUTE_ENTITY:
  6258. case XML_ATTRIBUTE_ENTITIES:
  6259. case XML_ATTRIBUTE_NOTATION:
  6260. if (cur->defaultValue != NULL) {
  6261. ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
  6262. cur->atype, cur->defaultValue);
  6263. if ((ret == 0) && (ctxt->valid == 1))
  6264. ctxt->valid = 0;
  6265. }
  6266. if (cur->tree != NULL) {
  6267. xmlEnumerationPtr tree = cur->tree;
  6268. while (tree != NULL) {
  6269. ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
  6270. cur->name, cur->atype, tree->name);
  6271. if ((ret == 0) && (ctxt->valid == 1))
  6272. ctxt->valid = 0;
  6273. tree = tree->next;
  6274. }
  6275. }
  6276. }
  6277. if (cur->atype == XML_ATTRIBUTE_NOTATION) {
  6278. doc = cur->doc;
  6279. if (cur->elem == NULL) {
  6280. xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
  6281. "xmlValidateAttributeCallback(%s): internal error\n",
  6282. (const char *) cur->name);
  6283. return;
  6284. }
  6285. if (doc != NULL)
  6286. elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
  6287. if ((elem == NULL) && (doc != NULL))
  6288. elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
  6289. if ((elem == NULL) && (cur->parent != NULL) &&
  6290. (cur->parent->type == XML_DTD_NODE))
  6291. elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
  6292. if (elem == NULL) {
  6293. xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
  6294. "attribute %s: could not find decl for element %s\n",
  6295. cur->name, cur->elem, NULL);
  6296. return;
  6297. }
  6298. if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
  6299. xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
  6300. "NOTATION attribute %s declared for EMPTY element %s\n",
  6301. cur->name, cur->elem, NULL);
  6302. ctxt->valid = 0;
  6303. }
  6304. }
  6305. }
  6306. /**
  6307. * xmlValidateDtdFinal:
  6308. * @ctxt: the validation context
  6309. * @doc: a document instance
  6310. *
  6311. * Does the final step for the dtds validation once all the
  6312. * subsets have been parsed
  6313. *
  6314. * basically it does the following checks described by the XML Rec
  6315. * - check that ENTITY and ENTITIES type attributes default or
  6316. * possible values matches one of the defined entities.
  6317. * - check that NOTATION type attributes default or
  6318. * possible values matches one of the defined notations.
  6319. *
  6320. * returns 1 if valid or 0 if invalid and -1 if not well-formed
  6321. */
  6322. int
  6323. xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
  6324. xmlDtdPtr dtd;
  6325. xmlAttributeTablePtr table;
  6326. xmlEntitiesTablePtr entities;
  6327. if ((doc == NULL) || (ctxt == NULL)) return(0);
  6328. if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
  6329. return(0);
  6330. ctxt->doc = doc;
  6331. ctxt->valid = 1;
  6332. dtd = doc->intSubset;
  6333. if ((dtd != NULL) && (dtd->attributes != NULL)) {
  6334. table = (xmlAttributeTablePtr) dtd->attributes;
  6335. xmlHashScan(table, xmlValidateAttributeCallback, ctxt);
  6336. }
  6337. if ((dtd != NULL) && (dtd->entities != NULL)) {
  6338. entities = (xmlEntitiesTablePtr) dtd->entities;
  6339. xmlHashScan(entities, xmlValidateNotationCallback, ctxt);
  6340. }
  6341. dtd = doc->extSubset;
  6342. if ((dtd != NULL) && (dtd->attributes != NULL)) {
  6343. table = (xmlAttributeTablePtr) dtd->attributes;
  6344. xmlHashScan(table, xmlValidateAttributeCallback, ctxt);
  6345. }
  6346. if ((dtd != NULL) && (dtd->entities != NULL)) {
  6347. entities = (xmlEntitiesTablePtr) dtd->entities;
  6348. xmlHashScan(entities, xmlValidateNotationCallback, ctxt);
  6349. }
  6350. return(ctxt->valid);
  6351. }
  6352. /**
  6353. * xmlValidateDocument:
  6354. * @ctxt: the validation context
  6355. * @doc: a document instance
  6356. *
  6357. * Try to validate the document instance
  6358. *
  6359. * basically it does the all the checks described by the XML Rec
  6360. * i.e. validates the internal and external subset (if present)
  6361. * and validate the document tree.
  6362. *
  6363. * returns 1 if valid or 0 otherwise
  6364. */
  6365. int
  6366. xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
  6367. int ret;
  6368. xmlNodePtr root;
  6369. if (doc == NULL)
  6370. return(0);
  6371. if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
  6372. xmlErrValid(ctxt, XML_DTD_NO_DTD,
  6373. "no DTD found!\n", NULL);
  6374. return(0);
  6375. }
  6376. if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
  6377. (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
  6378. xmlChar *sysID;
  6379. if (doc->intSubset->SystemID != NULL) {
  6380. sysID = xmlBuildURI(doc->intSubset->SystemID,
  6381. doc->URL);
  6382. if (sysID == NULL) {
  6383. xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
  6384. "Could not build URI for external subset \"%s\"\n",
  6385. (const char *) doc->intSubset->SystemID);
  6386. return 0;
  6387. }
  6388. } else
  6389. sysID = NULL;
  6390. doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
  6391. (const xmlChar *)sysID);
  6392. if (sysID != NULL)
  6393. xmlFree(sysID);
  6394. if (doc->extSubset == NULL) {
  6395. if (doc->intSubset->SystemID != NULL) {
  6396. xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
  6397. "Could not load the external subset \"%s\"\n",
  6398. (const char *) doc->intSubset->SystemID);
  6399. } else {
  6400. xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
  6401. "Could not load the external subset \"%s\"\n",
  6402. (const char *) doc->intSubset->ExternalID);
  6403. }
  6404. return(0);
  6405. }
  6406. }
  6407. if (doc->ids != NULL) {
  6408. xmlFreeIDTable(doc->ids);
  6409. doc->ids = NULL;
  6410. }
  6411. if (doc->refs != NULL) {
  6412. xmlFreeRefTable(doc->refs);
  6413. doc->refs = NULL;
  6414. }
  6415. ret = xmlValidateDtdFinal(ctxt, doc);
  6416. if (!xmlValidateRoot(ctxt, doc)) return(0);
  6417. root = xmlDocGetRootElement(doc);
  6418. ret &= xmlValidateElement(ctxt, doc, root);
  6419. ret &= xmlValidateDocumentFinal(ctxt, doc);
  6420. return(ret);
  6421. }
  6422. /************************************************************************
  6423. * *
  6424. * Routines for dynamic validation editing *
  6425. * *
  6426. ************************************************************************/
  6427. /**
  6428. * xmlValidGetPotentialChildren:
  6429. * @ctree: an element content tree
  6430. * @names: an array to store the list of child names
  6431. * @len: a pointer to the number of element in the list
  6432. * @max: the size of the array
  6433. *
  6434. * Build/extend a list of potential children allowed by the content tree
  6435. *
  6436. * returns the number of element in the list, or -1 in case of error.
  6437. */
  6438. int
  6439. xmlValidGetPotentialChildren(xmlElementContent *ctree,
  6440. const xmlChar **names,
  6441. int *len, int max) {
  6442. int i;
  6443. if ((ctree == NULL) || (names == NULL) || (len == NULL))
  6444. return(-1);
  6445. if (*len >= max) return(*len);
  6446. switch (ctree->type) {
  6447. case XML_ELEMENT_CONTENT_PCDATA:
  6448. for (i = 0; i < *len;i++)
  6449. if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len);
  6450. names[(*len)++] = BAD_CAST "#PCDATA";
  6451. break;
  6452. case XML_ELEMENT_CONTENT_ELEMENT:
  6453. for (i = 0; i < *len;i++)
  6454. if (xmlStrEqual(ctree->name, names[i])) return(*len);
  6455. names[(*len)++] = ctree->name;
  6456. break;
  6457. case XML_ELEMENT_CONTENT_SEQ:
  6458. xmlValidGetPotentialChildren(ctree->c1, names, len, max);
  6459. xmlValidGetPotentialChildren(ctree->c2, names, len, max);
  6460. break;
  6461. case XML_ELEMENT_CONTENT_OR:
  6462. xmlValidGetPotentialChildren(ctree->c1, names, len, max);
  6463. xmlValidGetPotentialChildren(ctree->c2, names, len, max);
  6464. break;
  6465. }
  6466. return(*len);
  6467. }
  6468. /*
  6469. * Dummy function to suppress messages while we try out valid elements
  6470. */
  6471. static void XMLCDECL xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
  6472. const char *msg ATTRIBUTE_UNUSED, ...) {
  6473. return;
  6474. }
  6475. /**
  6476. * xmlValidGetValidElements:
  6477. * @prev: an element to insert after
  6478. * @next: an element to insert next
  6479. * @names: an array to store the list of child names
  6480. * @max: the size of the array
  6481. *
  6482. * This function returns the list of authorized children to insert
  6483. * within an existing tree while respecting the validity constraints
  6484. * forced by the Dtd. The insertion point is defined using @prev and
  6485. * @next in the following ways:
  6486. * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
  6487. * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
  6488. * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
  6489. * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
  6490. * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
  6491. *
  6492. * pointers to the element names are inserted at the beginning of the array
  6493. * and do not need to be freed.
  6494. *
  6495. * returns the number of element in the list, or -1 in case of error. If
  6496. * the function returns the value @max the caller is invited to grow the
  6497. * receiving array and retry.
  6498. */
  6499. int
  6500. xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
  6501. int max) {
  6502. xmlValidCtxt vctxt;
  6503. int nb_valid_elements = 0;
  6504. const xmlChar *elements[256]={0};
  6505. int nb_elements = 0, i;
  6506. const xmlChar *name;
  6507. xmlNode *ref_node;
  6508. xmlNode *parent;
  6509. xmlNode *test_node;
  6510. xmlNode *prev_next;
  6511. xmlNode *next_prev;
  6512. xmlNode *parent_childs;
  6513. xmlNode *parent_last;
  6514. xmlElement *element_desc;
  6515. if (prev == NULL && next == NULL)
  6516. return(-1);
  6517. if (names == NULL) return(-1);
  6518. if (max <= 0) return(-1);
  6519. memset(&vctxt, 0, sizeof (xmlValidCtxt));
  6520. vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
  6521. nb_valid_elements = 0;
  6522. ref_node = prev ? prev : next;
  6523. parent = ref_node->parent;
  6524. /*
  6525. * Retrieves the parent element declaration
  6526. */
  6527. element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
  6528. parent->name);
  6529. if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
  6530. element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
  6531. parent->name);
  6532. if (element_desc == NULL) return(-1);
  6533. /*
  6534. * Do a backup of the current tree structure
  6535. */
  6536. prev_next = prev ? prev->next : NULL;
  6537. next_prev = next ? next->prev : NULL;
  6538. parent_childs = parent->children;
  6539. parent_last = parent->last;
  6540. /*
  6541. * Creates a dummy node and insert it into the tree
  6542. */
  6543. test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
  6544. if (test_node == NULL)
  6545. return(-1);
  6546. test_node->parent = parent;
  6547. test_node->prev = prev;
  6548. test_node->next = next;
  6549. name = test_node->name;
  6550. if (prev) prev->next = test_node;
  6551. else parent->children = test_node;
  6552. if (next) next->prev = test_node;
  6553. else parent->last = test_node;
  6554. /*
  6555. * Insert each potential child node and check if the parent is
  6556. * still valid
  6557. */
  6558. nb_elements = xmlValidGetPotentialChildren(element_desc->content,
  6559. elements, &nb_elements, 256);
  6560. for (i = 0;i < nb_elements;i++) {
  6561. test_node->name = elements[i];
  6562. if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
  6563. int j;
  6564. for (j = 0; j < nb_valid_elements;j++)
  6565. if (xmlStrEqual(elements[i], names[j])) break;
  6566. names[nb_valid_elements++] = elements[i];
  6567. if (nb_valid_elements >= max) break;
  6568. }
  6569. }
  6570. /*
  6571. * Restore the tree structure
  6572. */
  6573. if (prev) prev->next = prev_next;
  6574. if (next) next->prev = next_prev;
  6575. parent->children = parent_childs;
  6576. parent->last = parent_last;
  6577. /*
  6578. * Free up the dummy node
  6579. */
  6580. test_node->name = name;
  6581. xmlFreeNode(test_node);
  6582. return(nb_valid_elements);
  6583. }
  6584. #endif /* LIBXML_VALID_ENABLED */
  6585. #define bottom_valid
  6586. #include "elfgcchack.h"