valid.c 188 KB

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