valid.c 188 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040
  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. return(0);
  2501. } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
  2502. if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
  2503. ((xmlStrEqual(BAD_CAST "name", attr->name)) &&
  2504. ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a")))))
  2505. return(1);
  2506. return(0);
  2507. } else if (elem == NULL) {
  2508. return(0);
  2509. } else {
  2510. xmlAttributePtr attrDecl = NULL;
  2511. xmlChar felem[50], fattr[50];
  2512. xmlChar *fullelemname, *fullattrname;
  2513. fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ?
  2514. xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) :
  2515. (xmlChar *)elem->name;
  2516. fullattrname = (attr->ns != NULL && attr->ns->prefix != NULL) ?
  2517. xmlBuildQName(attr->name, attr->ns->prefix, fattr, 50) :
  2518. (xmlChar *)attr->name;
  2519. if (fullelemname != NULL && fullattrname != NULL) {
  2520. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullelemname,
  2521. fullattrname);
  2522. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  2523. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullelemname,
  2524. fullattrname);
  2525. }
  2526. if ((fullattrname != fattr) && (fullattrname != attr->name))
  2527. xmlFree(fullattrname);
  2528. if ((fullelemname != felem) && (fullelemname != elem->name))
  2529. xmlFree(fullelemname);
  2530. if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
  2531. return(1);
  2532. }
  2533. return(0);
  2534. }
  2535. /**
  2536. * xmlRemoveID:
  2537. * @doc: the document
  2538. * @attr: the attribute
  2539. *
  2540. * Remove the given attribute from the ID table maintained internally.
  2541. *
  2542. * Returns -1 if the lookup failed and 0 otherwise
  2543. */
  2544. int
  2545. xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
  2546. xmlIDTablePtr table;
  2547. xmlIDPtr id;
  2548. xmlChar *ID;
  2549. if (doc == NULL) return(-1);
  2550. if (attr == NULL) return(-1);
  2551. table = (xmlIDTablePtr) doc->ids;
  2552. if (table == NULL)
  2553. return(-1);
  2554. if (attr == NULL)
  2555. return(-1);
  2556. ID = xmlNodeListGetString(doc, attr->children, 1);
  2557. if (ID == NULL)
  2558. return(-1);
  2559. id = xmlHashLookup(table, ID);
  2560. if (id == NULL || id->attr != attr) {
  2561. xmlFree(ID);
  2562. return(-1);
  2563. }
  2564. xmlHashRemoveEntry(table, ID, (xmlHashDeallocator) xmlFreeID);
  2565. xmlFree(ID);
  2566. attr->atype = 0;
  2567. return(0);
  2568. }
  2569. /**
  2570. * xmlGetID:
  2571. * @doc: pointer to the document
  2572. * @ID: the ID value
  2573. *
  2574. * Search the attribute declaring the given ID
  2575. *
  2576. * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
  2577. */
  2578. xmlAttrPtr
  2579. xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
  2580. xmlIDTablePtr table;
  2581. xmlIDPtr id;
  2582. if (doc == NULL) {
  2583. return(NULL);
  2584. }
  2585. if (ID == NULL) {
  2586. return(NULL);
  2587. }
  2588. table = (xmlIDTablePtr) doc->ids;
  2589. if (table == NULL)
  2590. return(NULL);
  2591. id = xmlHashLookup(table, ID);
  2592. if (id == NULL)
  2593. return(NULL);
  2594. if (id->attr == NULL) {
  2595. /*
  2596. * We are operating on a stream, return a well known reference
  2597. * since the attribute node doesn't exist anymore
  2598. */
  2599. return((xmlAttrPtr) doc);
  2600. }
  2601. return(id->attr);
  2602. }
  2603. /************************************************************************
  2604. * *
  2605. * Refs *
  2606. * *
  2607. ************************************************************************/
  2608. typedef struct xmlRemoveMemo_t
  2609. {
  2610. xmlListPtr l;
  2611. xmlAttrPtr ap;
  2612. } xmlRemoveMemo;
  2613. typedef xmlRemoveMemo *xmlRemoveMemoPtr;
  2614. typedef struct xmlValidateMemo_t
  2615. {
  2616. xmlValidCtxtPtr ctxt;
  2617. const xmlChar *name;
  2618. } xmlValidateMemo;
  2619. typedef xmlValidateMemo *xmlValidateMemoPtr;
  2620. /**
  2621. * xmlFreeRef:
  2622. * @lk: A list link
  2623. *
  2624. * Deallocate the memory used by a ref definition
  2625. */
  2626. static void
  2627. xmlFreeRef(xmlLinkPtr lk) {
  2628. xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
  2629. if (ref == NULL) return;
  2630. if (ref->value != NULL)
  2631. xmlFree((xmlChar *)ref->value);
  2632. if (ref->name != NULL)
  2633. xmlFree((xmlChar *)ref->name);
  2634. xmlFree(ref);
  2635. }
  2636. /**
  2637. * xmlFreeRefList:
  2638. * @list_ref: A list of references.
  2639. *
  2640. * Deallocate the memory used by a list of references
  2641. */
  2642. static void
  2643. xmlFreeRefList(xmlListPtr list_ref) {
  2644. if (list_ref == NULL) return;
  2645. xmlListDelete(list_ref);
  2646. }
  2647. /**
  2648. * xmlWalkRemoveRef:
  2649. * @data: Contents of current link
  2650. * @user: Value supplied by the user
  2651. *
  2652. * Returns 0 to abort the walk or 1 to continue
  2653. */
  2654. static int
  2655. xmlWalkRemoveRef(const void *data, const void *user)
  2656. {
  2657. xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
  2658. xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
  2659. xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
  2660. if (attr0 == attr1) { /* Matched: remove and terminate walk */
  2661. xmlListRemoveFirst(ref_list, (void *)data);
  2662. return 0;
  2663. }
  2664. return 1;
  2665. }
  2666. /**
  2667. * xmlDummyCompare
  2668. * @data0: Value supplied by the user
  2669. * @data1: Value supplied by the user
  2670. *
  2671. * Do nothing, return 0. Used to create unordered lists.
  2672. */
  2673. static int
  2674. xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
  2675. const void *data1 ATTRIBUTE_UNUSED)
  2676. {
  2677. return (0);
  2678. }
  2679. /**
  2680. * xmlAddRef:
  2681. * @ctxt: the validation context
  2682. * @doc: pointer to the document
  2683. * @value: the value name
  2684. * @attr: the attribute holding the Ref
  2685. *
  2686. * Register a new ref declaration
  2687. *
  2688. * Returns NULL if not, otherwise the new xmlRefPtr
  2689. */
  2690. xmlRefPtr
  2691. xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
  2692. xmlAttrPtr attr) {
  2693. xmlRefPtr ret;
  2694. xmlRefTablePtr table;
  2695. xmlListPtr ref_list;
  2696. if (doc == NULL) {
  2697. return(NULL);
  2698. }
  2699. if (value == NULL) {
  2700. return(NULL);
  2701. }
  2702. if (attr == NULL) {
  2703. return(NULL);
  2704. }
  2705. /*
  2706. * Create the Ref table if needed.
  2707. */
  2708. table = (xmlRefTablePtr) doc->refs;
  2709. if (table == NULL) {
  2710. doc->refs = table = xmlHashCreateDict(0, doc->dict);
  2711. }
  2712. if (table == NULL) {
  2713. xmlVErrMemory(ctxt,
  2714. "xmlAddRef: Table creation failed!\n");
  2715. return(NULL);
  2716. }
  2717. ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
  2718. if (ret == NULL) {
  2719. xmlVErrMemory(ctxt, "malloc failed");
  2720. return(NULL);
  2721. }
  2722. /*
  2723. * fill the structure.
  2724. */
  2725. ret->value = xmlStrdup(value);
  2726. if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
  2727. /*
  2728. * Operating in streaming mode, attr is gonna disapear
  2729. */
  2730. ret->name = xmlStrdup(attr->name);
  2731. ret->attr = NULL;
  2732. } else {
  2733. ret->name = NULL;
  2734. ret->attr = attr;
  2735. }
  2736. ret->lineno = xmlGetLineNo(attr->parent);
  2737. /* To add a reference :-
  2738. * References are maintained as a list of references,
  2739. * Lookup the entry, if no entry create new nodelist
  2740. * Add the owning node to the NodeList
  2741. * Return the ref
  2742. */
  2743. if (NULL == (ref_list = xmlHashLookup(table, value))) {
  2744. if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
  2745. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  2746. "xmlAddRef: Reference list creation failed!\n",
  2747. NULL);
  2748. goto failed;
  2749. }
  2750. if (xmlHashAddEntry(table, value, ref_list) < 0) {
  2751. xmlListDelete(ref_list);
  2752. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  2753. "xmlAddRef: Reference list insertion failed!\n",
  2754. NULL);
  2755. goto failed;
  2756. }
  2757. }
  2758. if (xmlListAppend(ref_list, ret) != 0) {
  2759. xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
  2760. "xmlAddRef: Reference list insertion failed!\n",
  2761. NULL);
  2762. goto failed;
  2763. }
  2764. return(ret);
  2765. failed:
  2766. if (ret != NULL) {
  2767. if (ret->value != NULL)
  2768. xmlFree((char *)ret->value);
  2769. if (ret->name != NULL)
  2770. xmlFree((char *)ret->name);
  2771. xmlFree(ret);
  2772. }
  2773. return(NULL);
  2774. }
  2775. /**
  2776. * xmlFreeRefTable:
  2777. * @table: An ref table
  2778. *
  2779. * Deallocate the memory used by an Ref hash table.
  2780. */
  2781. void
  2782. xmlFreeRefTable(xmlRefTablePtr table) {
  2783. xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
  2784. }
  2785. /**
  2786. * xmlIsRef:
  2787. * @doc: the document
  2788. * @elem: the element carrying the attribute
  2789. * @attr: the attribute
  2790. *
  2791. * Determine whether an attribute is of type Ref. In case we have DTD(s)
  2792. * then this is simple, otherwise we use an heuristic: name Ref (upper
  2793. * or lowercase).
  2794. *
  2795. * Returns 0 or 1 depending on the lookup result
  2796. */
  2797. int
  2798. xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
  2799. if (attr == NULL)
  2800. return(0);
  2801. if (doc == NULL) {
  2802. doc = attr->doc;
  2803. if (doc == NULL) return(0);
  2804. }
  2805. if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
  2806. return(0);
  2807. } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
  2808. /* TODO @@@ */
  2809. return(0);
  2810. } else {
  2811. xmlAttributePtr attrDecl;
  2812. if (elem == NULL) return(0);
  2813. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
  2814. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  2815. attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
  2816. elem->name, attr->name);
  2817. if ((attrDecl != NULL) &&
  2818. (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
  2819. attrDecl->atype == XML_ATTRIBUTE_IDREFS))
  2820. return(1);
  2821. }
  2822. return(0);
  2823. }
  2824. /**
  2825. * xmlRemoveRef:
  2826. * @doc: the document
  2827. * @attr: the attribute
  2828. *
  2829. * Remove the given attribute from the Ref table maintained internally.
  2830. *
  2831. * Returns -1 if the lookup failed and 0 otherwise
  2832. */
  2833. int
  2834. xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
  2835. xmlListPtr ref_list;
  2836. xmlRefTablePtr table;
  2837. xmlChar *ID;
  2838. xmlRemoveMemo target;
  2839. if (doc == NULL) return(-1);
  2840. if (attr == NULL) return(-1);
  2841. table = (xmlRefTablePtr) doc->refs;
  2842. if (table == NULL)
  2843. return(-1);
  2844. if (attr == NULL)
  2845. return(-1);
  2846. ID = xmlNodeListGetString(doc, attr->children, 1);
  2847. if (ID == NULL)
  2848. return(-1);
  2849. ref_list = xmlHashLookup(table, ID);
  2850. if(ref_list == NULL) {
  2851. xmlFree(ID);
  2852. return (-1);
  2853. }
  2854. /* At this point, ref_list refers to a list of references which
  2855. * have the same key as the supplied attr. Our list of references
  2856. * is ordered by reference address and we don't have that information
  2857. * here to use when removing. We'll have to walk the list and
  2858. * check for a matching attribute, when we find one stop the walk
  2859. * and remove the entry.
  2860. * The list is ordered by reference, so that means we don't have the
  2861. * key. Passing the list and the reference to the walker means we
  2862. * will have enough data to be able to remove the entry.
  2863. */
  2864. target.l = ref_list;
  2865. target.ap = attr;
  2866. /* Remove the supplied attr from our list */
  2867. xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
  2868. /*If the list is empty then remove the list entry in the hash */
  2869. if (xmlListEmpty(ref_list))
  2870. xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
  2871. xmlFreeRefList);
  2872. xmlFree(ID);
  2873. return(0);
  2874. }
  2875. /**
  2876. * xmlGetRefs:
  2877. * @doc: pointer to the document
  2878. * @ID: the ID value
  2879. *
  2880. * Find the set of references for the supplied ID.
  2881. *
  2882. * Returns NULL if not found, otherwise node set for the ID.
  2883. */
  2884. xmlListPtr
  2885. xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
  2886. xmlRefTablePtr table;
  2887. if (doc == NULL) {
  2888. return(NULL);
  2889. }
  2890. if (ID == NULL) {
  2891. return(NULL);
  2892. }
  2893. table = (xmlRefTablePtr) doc->refs;
  2894. if (table == NULL)
  2895. return(NULL);
  2896. return (xmlHashLookup(table, ID));
  2897. }
  2898. /************************************************************************
  2899. * *
  2900. * Routines for validity checking *
  2901. * *
  2902. ************************************************************************/
  2903. /**
  2904. * xmlGetDtdElementDesc:
  2905. * @dtd: a pointer to the DtD to search
  2906. * @name: the element name
  2907. *
  2908. * Search the DTD for the description of this element
  2909. *
  2910. * returns the xmlElementPtr if found or NULL
  2911. */
  2912. xmlElementPtr
  2913. xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
  2914. xmlElementTablePtr table;
  2915. xmlElementPtr cur;
  2916. xmlChar *uqname = NULL, *prefix = NULL;
  2917. if ((dtd == NULL) || (name == NULL)) return(NULL);
  2918. if (dtd->elements == NULL)
  2919. return(NULL);
  2920. table = (xmlElementTablePtr) dtd->elements;
  2921. uqname = xmlSplitQName2(name, &prefix);
  2922. if (uqname != NULL)
  2923. name = uqname;
  2924. cur = xmlHashLookup2(table, name, prefix);
  2925. if (prefix != NULL) xmlFree(prefix);
  2926. if (uqname != NULL) xmlFree(uqname);
  2927. return(cur);
  2928. }
  2929. /**
  2930. * xmlGetDtdElementDesc2:
  2931. * @dtd: a pointer to the DtD to search
  2932. * @name: the element name
  2933. * @create: create an empty description if not found
  2934. *
  2935. * Search the DTD for the description of this element
  2936. *
  2937. * returns the xmlElementPtr if found or NULL
  2938. */
  2939. static xmlElementPtr
  2940. xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
  2941. xmlElementTablePtr table;
  2942. xmlElementPtr cur;
  2943. xmlChar *uqname = NULL, *prefix = NULL;
  2944. if (dtd == NULL) return(NULL);
  2945. if (dtd->elements == NULL) {
  2946. xmlDictPtr dict = NULL;
  2947. if (dtd->doc != NULL)
  2948. dict = dtd->doc->dict;
  2949. if (!create)
  2950. return(NULL);
  2951. /*
  2952. * Create the Element table if needed.
  2953. */
  2954. table = (xmlElementTablePtr) dtd->elements;
  2955. if (table == NULL) {
  2956. table = xmlHashCreateDict(0, dict);
  2957. dtd->elements = (void *) table;
  2958. }
  2959. if (table == NULL) {
  2960. xmlVErrMemory(NULL, "element table allocation failed");
  2961. return(NULL);
  2962. }
  2963. }
  2964. table = (xmlElementTablePtr) dtd->elements;
  2965. uqname = xmlSplitQName2(name, &prefix);
  2966. if (uqname != NULL)
  2967. name = uqname;
  2968. cur = xmlHashLookup2(table, name, prefix);
  2969. if ((cur == NULL) && (create)) {
  2970. cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
  2971. if (cur == NULL) {
  2972. xmlVErrMemory(NULL, "malloc failed");
  2973. return(NULL);
  2974. }
  2975. memset(cur, 0, sizeof(xmlElement));
  2976. cur->type = XML_ELEMENT_DECL;
  2977. /*
  2978. * fill the structure.
  2979. */
  2980. cur->name = xmlStrdup(name);
  2981. cur->prefix = xmlStrdup(prefix);
  2982. cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
  2983. xmlHashAddEntry2(table, name, prefix, cur);
  2984. }
  2985. if (prefix != NULL) xmlFree(prefix);
  2986. if (uqname != NULL) xmlFree(uqname);
  2987. return(cur);
  2988. }
  2989. /**
  2990. * xmlGetDtdQElementDesc:
  2991. * @dtd: a pointer to the DtD to search
  2992. * @name: the element name
  2993. * @prefix: the element namespace prefix
  2994. *
  2995. * Search the DTD for the description of this element
  2996. *
  2997. * returns the xmlElementPtr if found or NULL
  2998. */
  2999. xmlElementPtr
  3000. xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
  3001. const xmlChar *prefix) {
  3002. xmlElementTablePtr table;
  3003. if (dtd == NULL) return(NULL);
  3004. if (dtd->elements == NULL) return(NULL);
  3005. table = (xmlElementTablePtr) dtd->elements;
  3006. return(xmlHashLookup2(table, name, prefix));
  3007. }
  3008. /**
  3009. * xmlGetDtdAttrDesc:
  3010. * @dtd: a pointer to the DtD to search
  3011. * @elem: the element name
  3012. * @name: the attribute name
  3013. *
  3014. * Search the DTD for the description of this attribute on
  3015. * this element.
  3016. *
  3017. * returns the xmlAttributePtr if found or NULL
  3018. */
  3019. xmlAttributePtr
  3020. xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
  3021. xmlAttributeTablePtr table;
  3022. xmlAttributePtr cur;
  3023. xmlChar *uqname = NULL, *prefix = NULL;
  3024. if (dtd == NULL) return(NULL);
  3025. if (dtd->attributes == NULL) return(NULL);
  3026. table = (xmlAttributeTablePtr) dtd->attributes;
  3027. if (table == NULL)
  3028. return(NULL);
  3029. uqname = xmlSplitQName2(name, &prefix);
  3030. if (uqname != NULL) {
  3031. cur = xmlHashLookup3(table, uqname, prefix, elem);
  3032. if (prefix != NULL) xmlFree(prefix);
  3033. if (uqname != NULL) xmlFree(uqname);
  3034. } else
  3035. cur = xmlHashLookup3(table, name, NULL, elem);
  3036. return(cur);
  3037. }
  3038. /**
  3039. * xmlGetDtdQAttrDesc:
  3040. * @dtd: a pointer to the DtD to search
  3041. * @elem: the element name
  3042. * @name: the attribute name
  3043. * @prefix: the attribute namespace prefix
  3044. *
  3045. * Search the DTD for the description of this qualified attribute on
  3046. * this element.
  3047. *
  3048. * returns the xmlAttributePtr if found or NULL
  3049. */
  3050. xmlAttributePtr
  3051. xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
  3052. const xmlChar *prefix) {
  3053. xmlAttributeTablePtr table;
  3054. if (dtd == NULL) return(NULL);
  3055. if (dtd->attributes == NULL) return(NULL);
  3056. table = (xmlAttributeTablePtr) dtd->attributes;
  3057. return(xmlHashLookup3(table, name, prefix, elem));
  3058. }
  3059. /**
  3060. * xmlGetDtdNotationDesc:
  3061. * @dtd: a pointer to the DtD to search
  3062. * @name: the notation name
  3063. *
  3064. * Search the DTD for the description of this notation
  3065. *
  3066. * returns the xmlNotationPtr if found or NULL
  3067. */
  3068. xmlNotationPtr
  3069. xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
  3070. xmlNotationTablePtr table;
  3071. if (dtd == NULL) return(NULL);
  3072. if (dtd->notations == NULL) return(NULL);
  3073. table = (xmlNotationTablePtr) dtd->notations;
  3074. return(xmlHashLookup(table, name));
  3075. }
  3076. #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  3077. /**
  3078. * xmlValidateNotationUse:
  3079. * @ctxt: the validation context
  3080. * @doc: the document
  3081. * @notationName: the notation name to check
  3082. *
  3083. * Validate that the given name match a notation declaration.
  3084. * - [ VC: Notation Declared ]
  3085. *
  3086. * returns 1 if valid or 0 otherwise
  3087. */
  3088. int
  3089. xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3090. const xmlChar *notationName) {
  3091. xmlNotationPtr notaDecl;
  3092. if ((doc == NULL) || (doc->intSubset == NULL) ||
  3093. (notationName == NULL)) return(-1);
  3094. notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
  3095. if ((notaDecl == NULL) && (doc->extSubset != NULL))
  3096. notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
  3097. if ((notaDecl == NULL) && (ctxt != NULL)) {
  3098. xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
  3099. "NOTATION %s is not declared\n",
  3100. notationName, NULL, NULL);
  3101. return(0);
  3102. }
  3103. return(1);
  3104. }
  3105. #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
  3106. /**
  3107. * xmlIsMixedElement:
  3108. * @doc: the document
  3109. * @name: the element name
  3110. *
  3111. * Search in the DtDs whether an element accept Mixed content (or ANY)
  3112. * basically if it is supposed to accept text childs
  3113. *
  3114. * returns 0 if no, 1 if yes, and -1 if no element description is available
  3115. */
  3116. int
  3117. xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
  3118. xmlElementPtr elemDecl;
  3119. if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
  3120. elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
  3121. if ((elemDecl == NULL) && (doc->extSubset != NULL))
  3122. elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
  3123. if (elemDecl == NULL) return(-1);
  3124. switch (elemDecl->etype) {
  3125. case XML_ELEMENT_TYPE_UNDEFINED:
  3126. return(-1);
  3127. case XML_ELEMENT_TYPE_ELEMENT:
  3128. return(0);
  3129. case XML_ELEMENT_TYPE_EMPTY:
  3130. /*
  3131. * return 1 for EMPTY since we want VC error to pop up
  3132. * on <empty> </empty> for example
  3133. */
  3134. case XML_ELEMENT_TYPE_ANY:
  3135. case XML_ELEMENT_TYPE_MIXED:
  3136. return(1);
  3137. }
  3138. return(1);
  3139. }
  3140. #ifdef LIBXML_VALID_ENABLED
  3141. static int
  3142. xmlIsDocNameStartChar(xmlDocPtr doc, int c) {
  3143. if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
  3144. /*
  3145. * Use the new checks of production [4] [4a] amd [5] of the
  3146. * Update 5 of XML-1.0
  3147. */
  3148. if (((c >= 'a') && (c <= 'z')) ||
  3149. ((c >= 'A') && (c <= 'Z')) ||
  3150. (c == '_') || (c == ':') ||
  3151. ((c >= 0xC0) && (c <= 0xD6)) ||
  3152. ((c >= 0xD8) && (c <= 0xF6)) ||
  3153. ((c >= 0xF8) && (c <= 0x2FF)) ||
  3154. ((c >= 0x370) && (c <= 0x37D)) ||
  3155. ((c >= 0x37F) && (c <= 0x1FFF)) ||
  3156. ((c >= 0x200C) && (c <= 0x200D)) ||
  3157. ((c >= 0x2070) && (c <= 0x218F)) ||
  3158. ((c >= 0x2C00) && (c <= 0x2FEF)) ||
  3159. ((c >= 0x3001) && (c <= 0xD7FF)) ||
  3160. ((c >= 0xF900) && (c <= 0xFDCF)) ||
  3161. ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
  3162. ((c >= 0x10000) && (c <= 0xEFFFF)))
  3163. return(1);
  3164. } else {
  3165. if (IS_LETTER(c) || (c == '_') || (c == ':'))
  3166. return(1);
  3167. }
  3168. return(0);
  3169. }
  3170. static int
  3171. xmlIsDocNameChar(xmlDocPtr doc, int c) {
  3172. if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
  3173. /*
  3174. * Use the new checks of production [4] [4a] amd [5] of the
  3175. * Update 5 of XML-1.0
  3176. */
  3177. if (((c >= 'a') && (c <= 'z')) ||
  3178. ((c >= 'A') && (c <= 'Z')) ||
  3179. ((c >= '0') && (c <= '9')) || /* !start */
  3180. (c == '_') || (c == ':') ||
  3181. (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
  3182. ((c >= 0xC0) && (c <= 0xD6)) ||
  3183. ((c >= 0xD8) && (c <= 0xF6)) ||
  3184. ((c >= 0xF8) && (c <= 0x2FF)) ||
  3185. ((c >= 0x300) && (c <= 0x36F)) || /* !start */
  3186. ((c >= 0x370) && (c <= 0x37D)) ||
  3187. ((c >= 0x37F) && (c <= 0x1FFF)) ||
  3188. ((c >= 0x200C) && (c <= 0x200D)) ||
  3189. ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
  3190. ((c >= 0x2070) && (c <= 0x218F)) ||
  3191. ((c >= 0x2C00) && (c <= 0x2FEF)) ||
  3192. ((c >= 0x3001) && (c <= 0xD7FF)) ||
  3193. ((c >= 0xF900) && (c <= 0xFDCF)) ||
  3194. ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
  3195. ((c >= 0x10000) && (c <= 0xEFFFF)))
  3196. return(1);
  3197. } else {
  3198. if ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
  3199. (c == '.') || (c == '-') ||
  3200. (c == '_') || (c == ':') ||
  3201. (IS_COMBINING(c)) ||
  3202. (IS_EXTENDER(c)))
  3203. return(1);
  3204. }
  3205. return(0);
  3206. }
  3207. /**
  3208. * xmlValidateNameValue:
  3209. * @doc: pointer to the document or NULL
  3210. * @value: an Name value
  3211. *
  3212. * Validate that the given value match Name production
  3213. *
  3214. * returns 1 if valid or 0 otherwise
  3215. */
  3216. static int
  3217. xmlValidateNameValueInternal(xmlDocPtr doc, const xmlChar *value) {
  3218. const xmlChar *cur;
  3219. int val, len;
  3220. if (value == NULL) return(0);
  3221. cur = value;
  3222. val = xmlStringCurrentChar(NULL, cur, &len);
  3223. cur += len;
  3224. if (!xmlIsDocNameStartChar(doc, val))
  3225. return(0);
  3226. val = xmlStringCurrentChar(NULL, cur, &len);
  3227. cur += len;
  3228. while (xmlIsDocNameChar(doc, val)) {
  3229. val = xmlStringCurrentChar(NULL, cur, &len);
  3230. cur += len;
  3231. }
  3232. if (val != 0) return(0);
  3233. return(1);
  3234. }
  3235. /**
  3236. * xmlValidateNameValue:
  3237. * @value: an Name value
  3238. *
  3239. * Validate that the given value match Name production
  3240. *
  3241. * returns 1 if valid or 0 otherwise
  3242. */
  3243. int
  3244. xmlValidateNameValue(const xmlChar *value) {
  3245. return(xmlValidateNameValueInternal(NULL, value));
  3246. }
  3247. /**
  3248. * xmlValidateNamesValueInternal:
  3249. * @doc: pointer to the document or NULL
  3250. * @value: an Names value
  3251. *
  3252. * Validate that the given value match Names production
  3253. *
  3254. * returns 1 if valid or 0 otherwise
  3255. */
  3256. static int
  3257. xmlValidateNamesValueInternal(xmlDocPtr doc, const xmlChar *value) {
  3258. const xmlChar *cur;
  3259. int val, len;
  3260. if (value == NULL) return(0);
  3261. cur = value;
  3262. val = xmlStringCurrentChar(NULL, cur, &len);
  3263. cur += len;
  3264. if (!xmlIsDocNameStartChar(doc, val))
  3265. return(0);
  3266. val = xmlStringCurrentChar(NULL, cur, &len);
  3267. cur += len;
  3268. while (xmlIsDocNameChar(doc, val)) {
  3269. val = xmlStringCurrentChar(NULL, cur, &len);
  3270. cur += len;
  3271. }
  3272. /* Should not test IS_BLANK(val) here -- see erratum E20*/
  3273. while (val == 0x20) {
  3274. while (val == 0x20) {
  3275. val = xmlStringCurrentChar(NULL, cur, &len);
  3276. cur += len;
  3277. }
  3278. if (!xmlIsDocNameStartChar(doc, val))
  3279. return(0);
  3280. val = xmlStringCurrentChar(NULL, cur, &len);
  3281. cur += len;
  3282. while (xmlIsDocNameChar(doc, val)) {
  3283. val = xmlStringCurrentChar(NULL, cur, &len);
  3284. cur += len;
  3285. }
  3286. }
  3287. if (val != 0) return(0);
  3288. return(1);
  3289. }
  3290. /**
  3291. * xmlValidateNamesValue:
  3292. * @value: an Names value
  3293. *
  3294. * Validate that the given value match Names production
  3295. *
  3296. * returns 1 if valid or 0 otherwise
  3297. */
  3298. int
  3299. xmlValidateNamesValue(const xmlChar *value) {
  3300. return(xmlValidateNamesValueInternal(NULL, value));
  3301. }
  3302. /**
  3303. * xmlValidateNmtokenValueInternal:
  3304. * @doc: pointer to the document or NULL
  3305. * @value: an Nmtoken value
  3306. *
  3307. * Validate that the given value match Nmtoken production
  3308. *
  3309. * [ VC: Name Token ]
  3310. *
  3311. * returns 1 if valid or 0 otherwise
  3312. */
  3313. static int
  3314. xmlValidateNmtokenValueInternal(xmlDocPtr doc, const xmlChar *value) {
  3315. const xmlChar *cur;
  3316. int val, len;
  3317. if (value == NULL) return(0);
  3318. cur = value;
  3319. val = xmlStringCurrentChar(NULL, cur, &len);
  3320. cur += len;
  3321. if (!xmlIsDocNameChar(doc, val))
  3322. return(0);
  3323. val = xmlStringCurrentChar(NULL, cur, &len);
  3324. cur += len;
  3325. while (xmlIsDocNameChar(doc, val)) {
  3326. val = xmlStringCurrentChar(NULL, cur, &len);
  3327. cur += len;
  3328. }
  3329. if (val != 0) return(0);
  3330. return(1);
  3331. }
  3332. /**
  3333. * xmlValidateNmtokenValue:
  3334. * @value: an Nmtoken value
  3335. *
  3336. * Validate that the given value match Nmtoken production
  3337. *
  3338. * [ VC: Name Token ]
  3339. *
  3340. * returns 1 if valid or 0 otherwise
  3341. */
  3342. int
  3343. xmlValidateNmtokenValue(const xmlChar *value) {
  3344. return(xmlValidateNmtokenValueInternal(NULL, value));
  3345. }
  3346. /**
  3347. * xmlValidateNmtokensValueInternal:
  3348. * @doc: pointer to the document or NULL
  3349. * @value: an Nmtokens value
  3350. *
  3351. * Validate that the given value match Nmtokens production
  3352. *
  3353. * [ VC: Name Token ]
  3354. *
  3355. * returns 1 if valid or 0 otherwise
  3356. */
  3357. static int
  3358. xmlValidateNmtokensValueInternal(xmlDocPtr doc, const xmlChar *value) {
  3359. const xmlChar *cur;
  3360. int val, len;
  3361. if (value == NULL) return(0);
  3362. cur = value;
  3363. val = xmlStringCurrentChar(NULL, cur, &len);
  3364. cur += len;
  3365. while (IS_BLANK(val)) {
  3366. val = xmlStringCurrentChar(NULL, cur, &len);
  3367. cur += len;
  3368. }
  3369. if (!xmlIsDocNameChar(doc, val))
  3370. return(0);
  3371. while (xmlIsDocNameChar(doc, val)) {
  3372. val = xmlStringCurrentChar(NULL, cur, &len);
  3373. cur += len;
  3374. }
  3375. /* Should not test IS_BLANK(val) here -- see erratum E20*/
  3376. while (val == 0x20) {
  3377. while (val == 0x20) {
  3378. val = xmlStringCurrentChar(NULL, cur, &len);
  3379. cur += len;
  3380. }
  3381. if (val == 0) return(1);
  3382. if (!xmlIsDocNameChar(doc, val))
  3383. return(0);
  3384. val = xmlStringCurrentChar(NULL, cur, &len);
  3385. cur += len;
  3386. while (xmlIsDocNameChar(doc, val)) {
  3387. val = xmlStringCurrentChar(NULL, cur, &len);
  3388. cur += len;
  3389. }
  3390. }
  3391. if (val != 0) return(0);
  3392. return(1);
  3393. }
  3394. /**
  3395. * xmlValidateNmtokensValue:
  3396. * @value: an Nmtokens value
  3397. *
  3398. * Validate that the given value match Nmtokens production
  3399. *
  3400. * [ VC: Name Token ]
  3401. *
  3402. * returns 1 if valid or 0 otherwise
  3403. */
  3404. int
  3405. xmlValidateNmtokensValue(const xmlChar *value) {
  3406. return(xmlValidateNmtokensValueInternal(NULL, value));
  3407. }
  3408. /**
  3409. * xmlValidateNotationDecl:
  3410. * @ctxt: the validation context
  3411. * @doc: a document instance
  3412. * @nota: a notation definition
  3413. *
  3414. * Try to validate a single notation definition
  3415. * basically it does the following checks as described by the
  3416. * XML-1.0 recommendation:
  3417. * - it seems that no validity constraint exists on notation declarations
  3418. * But this function get called anyway ...
  3419. *
  3420. * returns 1 if valid or 0 otherwise
  3421. */
  3422. int
  3423. xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
  3424. xmlNotationPtr nota ATTRIBUTE_UNUSED) {
  3425. int ret = 1;
  3426. return(ret);
  3427. }
  3428. /**
  3429. * xmlValidateAttributeValueInternal:
  3430. * @doc: the document
  3431. * @type: an attribute type
  3432. * @value: an attribute value
  3433. *
  3434. * Validate that the given attribute value match the proper production
  3435. *
  3436. * returns 1 if valid or 0 otherwise
  3437. */
  3438. static int
  3439. xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
  3440. const xmlChar *value) {
  3441. switch (type) {
  3442. case XML_ATTRIBUTE_ENTITIES:
  3443. case XML_ATTRIBUTE_IDREFS:
  3444. return(xmlValidateNamesValueInternal(doc, value));
  3445. case XML_ATTRIBUTE_ENTITY:
  3446. case XML_ATTRIBUTE_IDREF:
  3447. case XML_ATTRIBUTE_ID:
  3448. case XML_ATTRIBUTE_NOTATION:
  3449. return(xmlValidateNameValueInternal(doc, value));
  3450. case XML_ATTRIBUTE_NMTOKENS:
  3451. case XML_ATTRIBUTE_ENUMERATION:
  3452. return(xmlValidateNmtokensValueInternal(doc, value));
  3453. case XML_ATTRIBUTE_NMTOKEN:
  3454. return(xmlValidateNmtokenValueInternal(doc, value));
  3455. case XML_ATTRIBUTE_CDATA:
  3456. break;
  3457. }
  3458. return(1);
  3459. }
  3460. /**
  3461. * xmlValidateAttributeValue:
  3462. * @type: an attribute type
  3463. * @value: an attribute value
  3464. *
  3465. * Validate that the given attribute value match the proper production
  3466. *
  3467. * [ VC: ID ]
  3468. * Values of type ID must match the Name production....
  3469. *
  3470. * [ VC: IDREF ]
  3471. * Values of type IDREF must match the Name production, and values
  3472. * of type IDREFS must match Names ...
  3473. *
  3474. * [ VC: Entity Name ]
  3475. * Values of type ENTITY must match the Name production, values
  3476. * of type ENTITIES must match Names ...
  3477. *
  3478. * [ VC: Name Token ]
  3479. * Values of type NMTOKEN must match the Nmtoken production; values
  3480. * of type NMTOKENS must match Nmtokens.
  3481. *
  3482. * returns 1 if valid or 0 otherwise
  3483. */
  3484. int
  3485. xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
  3486. return(xmlValidateAttributeValueInternal(NULL, type, value));
  3487. }
  3488. /**
  3489. * xmlValidateAttributeValue2:
  3490. * @ctxt: the validation context
  3491. * @doc: the document
  3492. * @name: the attribute name (used for error reporting only)
  3493. * @type: the attribute type
  3494. * @value: the attribute value
  3495. *
  3496. * Validate that the given attribute value match a given type.
  3497. * This typically cannot be done before having finished parsing
  3498. * the subsets.
  3499. *
  3500. * [ VC: IDREF ]
  3501. * Values of type IDREF must match one of the declared IDs
  3502. * Values of type IDREFS must match a sequence of the declared IDs
  3503. * each Name must match the value of an ID attribute on some element
  3504. * in the XML document; i.e. IDREF values must match the value of
  3505. * some ID attribute
  3506. *
  3507. * [ VC: Entity Name ]
  3508. * Values of type ENTITY must match one declared entity
  3509. * Values of type ENTITIES must match a sequence of declared entities
  3510. *
  3511. * [ VC: Notation Attributes ]
  3512. * all notation names in the declaration must be declared.
  3513. *
  3514. * returns 1 if valid or 0 otherwise
  3515. */
  3516. static int
  3517. xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3518. const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
  3519. int ret = 1;
  3520. switch (type) {
  3521. case XML_ATTRIBUTE_IDREFS:
  3522. case XML_ATTRIBUTE_IDREF:
  3523. case XML_ATTRIBUTE_ID:
  3524. case XML_ATTRIBUTE_NMTOKENS:
  3525. case XML_ATTRIBUTE_ENUMERATION:
  3526. case XML_ATTRIBUTE_NMTOKEN:
  3527. case XML_ATTRIBUTE_CDATA:
  3528. break;
  3529. case XML_ATTRIBUTE_ENTITY: {
  3530. xmlEntityPtr ent;
  3531. ent = xmlGetDocEntity(doc, value);
  3532. /* yeah it's a bit messy... */
  3533. if ((ent == NULL) && (doc->standalone == 1)) {
  3534. doc->standalone = 0;
  3535. ent = xmlGetDocEntity(doc, value);
  3536. }
  3537. if (ent == NULL) {
  3538. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3539. XML_DTD_UNKNOWN_ENTITY,
  3540. "ENTITY attribute %s reference an unknown entity \"%s\"\n",
  3541. name, value, NULL);
  3542. ret = 0;
  3543. } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
  3544. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3545. XML_DTD_ENTITY_TYPE,
  3546. "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
  3547. name, value, NULL);
  3548. ret = 0;
  3549. }
  3550. break;
  3551. }
  3552. case XML_ATTRIBUTE_ENTITIES: {
  3553. xmlChar *dup, *nam = NULL, *cur, save;
  3554. xmlEntityPtr ent;
  3555. dup = xmlStrdup(value);
  3556. if (dup == NULL)
  3557. return(0);
  3558. cur = dup;
  3559. while (*cur != 0) {
  3560. nam = cur;
  3561. while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
  3562. save = *cur;
  3563. *cur = 0;
  3564. ent = xmlGetDocEntity(doc, nam);
  3565. if (ent == NULL) {
  3566. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3567. XML_DTD_UNKNOWN_ENTITY,
  3568. "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
  3569. name, nam, NULL);
  3570. ret = 0;
  3571. } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
  3572. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3573. XML_DTD_ENTITY_TYPE,
  3574. "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
  3575. name, nam, NULL);
  3576. ret = 0;
  3577. }
  3578. if (save == 0)
  3579. break;
  3580. *cur = save;
  3581. while (IS_BLANK_CH(*cur)) cur++;
  3582. }
  3583. xmlFree(dup);
  3584. break;
  3585. }
  3586. case XML_ATTRIBUTE_NOTATION: {
  3587. xmlNotationPtr nota;
  3588. nota = xmlGetDtdNotationDesc(doc->intSubset, value);
  3589. if ((nota == NULL) && (doc->extSubset != NULL))
  3590. nota = xmlGetDtdNotationDesc(doc->extSubset, value);
  3591. if (nota == NULL) {
  3592. xmlErrValidNode(ctxt, (xmlNodePtr) doc,
  3593. XML_DTD_UNKNOWN_NOTATION,
  3594. "NOTATION attribute %s reference an unknown notation \"%s\"\n",
  3595. name, value, NULL);
  3596. ret = 0;
  3597. }
  3598. break;
  3599. }
  3600. }
  3601. return(ret);
  3602. }
  3603. /**
  3604. * xmlValidCtxtNormalizeAttributeValue:
  3605. * @ctxt: the validation context
  3606. * @doc: the document
  3607. * @elem: the parent
  3608. * @name: the attribute name
  3609. * @value: the attribute value
  3610. * @ctxt: the validation context or NULL
  3611. *
  3612. * Does the validation related extra step of the normalization of attribute
  3613. * values:
  3614. *
  3615. * If the declared value is not CDATA, then the XML processor must further
  3616. * process the normalized attribute value by discarding any leading and
  3617. * trailing space (#x20) characters, and by replacing sequences of space
  3618. * (#x20) characters by single space (#x20) character.
  3619. *
  3620. * Also check VC: Standalone Document Declaration in P32, and update
  3621. * ctxt->valid accordingly
  3622. *
  3623. * returns a new normalized string if normalization is needed, NULL otherwise
  3624. * the caller must free the returned value.
  3625. */
  3626. xmlChar *
  3627. xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3628. xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
  3629. xmlChar *ret, *dst;
  3630. const xmlChar *src;
  3631. xmlAttributePtr attrDecl = NULL;
  3632. int extsubset = 0;
  3633. if (doc == NULL) return(NULL);
  3634. if (elem == NULL) return(NULL);
  3635. if (name == NULL) return(NULL);
  3636. if (value == NULL) return(NULL);
  3637. if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
  3638. xmlChar fn[50];
  3639. xmlChar *fullname;
  3640. fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
  3641. if (fullname == NULL)
  3642. return(NULL);
  3643. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
  3644. if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
  3645. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
  3646. if (attrDecl != NULL)
  3647. extsubset = 1;
  3648. }
  3649. if ((fullname != fn) && (fullname != elem->name))
  3650. xmlFree(fullname);
  3651. }
  3652. if ((attrDecl == NULL) && (doc->intSubset != NULL))
  3653. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
  3654. if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
  3655. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
  3656. if (attrDecl != NULL)
  3657. extsubset = 1;
  3658. }
  3659. if (attrDecl == NULL)
  3660. return(NULL);
  3661. if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
  3662. return(NULL);
  3663. ret = xmlStrdup(value);
  3664. if (ret == NULL)
  3665. return(NULL);
  3666. src = value;
  3667. dst = ret;
  3668. while (*src == 0x20) src++;
  3669. while (*src != 0) {
  3670. if (*src == 0x20) {
  3671. while (*src == 0x20) src++;
  3672. if (*src != 0)
  3673. *dst++ = 0x20;
  3674. } else {
  3675. *dst++ = *src++;
  3676. }
  3677. }
  3678. *dst = 0;
  3679. if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
  3680. xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
  3681. "standalone: %s on %s value had to be normalized based on external subset declaration\n",
  3682. name, elem->name, NULL);
  3683. ctxt->valid = 0;
  3684. }
  3685. return(ret);
  3686. }
  3687. /**
  3688. * xmlValidNormalizeAttributeValue:
  3689. * @doc: the document
  3690. * @elem: the parent
  3691. * @name: the attribute name
  3692. * @value: the attribute value
  3693. *
  3694. * Does the validation related extra step of the normalization of attribute
  3695. * values:
  3696. *
  3697. * If the declared value is not CDATA, then the XML processor must further
  3698. * process the normalized attribute value by discarding any leading and
  3699. * trailing space (#x20) characters, and by replacing sequences of space
  3700. * (#x20) characters by single space (#x20) character.
  3701. *
  3702. * Returns a new normalized string if normalization is needed, NULL otherwise
  3703. * the caller must free the returned value.
  3704. */
  3705. xmlChar *
  3706. xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
  3707. const xmlChar *name, const xmlChar *value) {
  3708. xmlChar *ret, *dst;
  3709. const xmlChar *src;
  3710. xmlAttributePtr attrDecl = NULL;
  3711. if (doc == NULL) return(NULL);
  3712. if (elem == NULL) return(NULL);
  3713. if (name == NULL) return(NULL);
  3714. if (value == NULL) return(NULL);
  3715. if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
  3716. xmlChar fn[50];
  3717. xmlChar *fullname;
  3718. fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
  3719. if (fullname == NULL)
  3720. return(NULL);
  3721. if ((fullname != fn) && (fullname != elem->name))
  3722. xmlFree(fullname);
  3723. }
  3724. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
  3725. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  3726. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
  3727. if (attrDecl == NULL)
  3728. return(NULL);
  3729. if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
  3730. return(NULL);
  3731. ret = xmlStrdup(value);
  3732. if (ret == NULL)
  3733. return(NULL);
  3734. src = value;
  3735. dst = ret;
  3736. while (*src == 0x20) src++;
  3737. while (*src != 0) {
  3738. if (*src == 0x20) {
  3739. while (*src == 0x20) src++;
  3740. if (*src != 0)
  3741. *dst++ = 0x20;
  3742. } else {
  3743. *dst++ = *src++;
  3744. }
  3745. }
  3746. *dst = 0;
  3747. return(ret);
  3748. }
  3749. static void
  3750. xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
  3751. const xmlChar* name ATTRIBUTE_UNUSED) {
  3752. if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
  3753. }
  3754. /**
  3755. * xmlValidateAttributeDecl:
  3756. * @ctxt: the validation context
  3757. * @doc: a document instance
  3758. * @attr: an attribute definition
  3759. *
  3760. * Try to validate a single attribute definition
  3761. * basically it does the following checks as described by the
  3762. * XML-1.0 recommendation:
  3763. * - [ VC: Attribute Default Legal ]
  3764. * - [ VC: Enumeration ]
  3765. * - [ VC: ID Attribute Default ]
  3766. *
  3767. * The ID/IDREF uniqueness and matching are done separately
  3768. *
  3769. * returns 1 if valid or 0 otherwise
  3770. */
  3771. int
  3772. xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3773. xmlAttributePtr attr) {
  3774. int ret = 1;
  3775. int val;
  3776. CHECK_DTD;
  3777. if(attr == NULL) return(1);
  3778. /* Attribute Default Legal */
  3779. /* Enumeration */
  3780. if (attr->defaultValue != NULL) {
  3781. val = xmlValidateAttributeValueInternal(doc, attr->atype,
  3782. attr->defaultValue);
  3783. if (val == 0) {
  3784. xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
  3785. "Syntax of default value for attribute %s of %s is not valid\n",
  3786. attr->name, attr->elem, NULL);
  3787. }
  3788. ret &= val;
  3789. }
  3790. /* ID Attribute Default */
  3791. if ((attr->atype == XML_ATTRIBUTE_ID)&&
  3792. (attr->def != XML_ATTRIBUTE_IMPLIED) &&
  3793. (attr->def != XML_ATTRIBUTE_REQUIRED)) {
  3794. xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
  3795. "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
  3796. attr->name, attr->elem, NULL);
  3797. ret = 0;
  3798. }
  3799. /* One ID per Element Type */
  3800. if (attr->atype == XML_ATTRIBUTE_ID) {
  3801. int nbId;
  3802. /* the trick is that we parse DtD as their own internal subset */
  3803. xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
  3804. attr->elem);
  3805. if (elem != NULL) {
  3806. nbId = xmlScanIDAttributeDecl(NULL, elem, 0);
  3807. } else {
  3808. xmlAttributeTablePtr table;
  3809. /*
  3810. * The attribute may be declared in the internal subset and the
  3811. * element in the external subset.
  3812. */
  3813. nbId = 0;
  3814. if (doc->intSubset != NULL) {
  3815. table = (xmlAttributeTablePtr) doc->intSubset->attributes;
  3816. xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
  3817. xmlValidateAttributeIdCallback, &nbId);
  3818. }
  3819. }
  3820. if (nbId > 1) {
  3821. xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
  3822. "Element %s has %d ID attribute defined in the internal subset : %s\n",
  3823. attr->elem, nbId, attr->name);
  3824. } else if (doc->extSubset != NULL) {
  3825. int extId = 0;
  3826. elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
  3827. if (elem != NULL) {
  3828. extId = xmlScanIDAttributeDecl(NULL, elem, 0);
  3829. }
  3830. if (extId > 1) {
  3831. xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
  3832. "Element %s has %d ID attribute defined in the external subset : %s\n",
  3833. attr->elem, extId, attr->name);
  3834. } else if (extId + nbId > 1) {
  3835. xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
  3836. "Element %s has ID attributes defined in the internal and external subset : %s\n",
  3837. attr->elem, attr->name, NULL);
  3838. }
  3839. }
  3840. }
  3841. /* Validity Constraint: Enumeration */
  3842. if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
  3843. xmlEnumerationPtr tree = attr->tree;
  3844. while (tree != NULL) {
  3845. if (xmlStrEqual(tree->name, attr->defaultValue)) break;
  3846. tree = tree->next;
  3847. }
  3848. if (tree == NULL) {
  3849. xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
  3850. "Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
  3851. attr->defaultValue, attr->name, attr->elem);
  3852. ret = 0;
  3853. }
  3854. }
  3855. return(ret);
  3856. }
  3857. /**
  3858. * xmlValidateElementDecl:
  3859. * @ctxt: the validation context
  3860. * @doc: a document instance
  3861. * @elem: an element definition
  3862. *
  3863. * Try to validate a single element definition
  3864. * basically it does the following checks as described by the
  3865. * XML-1.0 recommendation:
  3866. * - [ VC: One ID per Element Type ]
  3867. * - [ VC: No Duplicate Types ]
  3868. * - [ VC: Unique Element Type Declaration ]
  3869. *
  3870. * returns 1 if valid or 0 otherwise
  3871. */
  3872. int
  3873. xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3874. xmlElementPtr elem) {
  3875. int ret = 1;
  3876. xmlElementPtr tst;
  3877. CHECK_DTD;
  3878. if (elem == NULL) return(1);
  3879. #if 0
  3880. #ifdef LIBXML_REGEXP_ENABLED
  3881. /* Build the regexp associated to the content model */
  3882. ret = xmlValidBuildContentModel(ctxt, elem);
  3883. #endif
  3884. #endif
  3885. /* No Duplicate Types */
  3886. if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
  3887. xmlElementContentPtr cur, next;
  3888. const xmlChar *name;
  3889. cur = elem->content;
  3890. while (cur != NULL) {
  3891. if (cur->type != XML_ELEMENT_CONTENT_OR) break;
  3892. if (cur->c1 == NULL) break;
  3893. if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
  3894. name = cur->c1->name;
  3895. next = cur->c2;
  3896. while (next != NULL) {
  3897. if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
  3898. if ((xmlStrEqual(next->name, name)) &&
  3899. (xmlStrEqual(next->prefix, cur->c1->prefix))) {
  3900. if (cur->c1->prefix == NULL) {
  3901. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
  3902. "Definition of %s has duplicate references of %s\n",
  3903. elem->name, name, NULL);
  3904. } else {
  3905. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
  3906. "Definition of %s has duplicate references of %s:%s\n",
  3907. elem->name, cur->c1->prefix, name);
  3908. }
  3909. ret = 0;
  3910. }
  3911. break;
  3912. }
  3913. if (next->c1 == NULL) break;
  3914. if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
  3915. if ((xmlStrEqual(next->c1->name, name)) &&
  3916. (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) {
  3917. if (cur->c1->prefix == NULL) {
  3918. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
  3919. "Definition of %s has duplicate references to %s\n",
  3920. elem->name, name, NULL);
  3921. } else {
  3922. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
  3923. "Definition of %s has duplicate references to %s:%s\n",
  3924. elem->name, cur->c1->prefix, name);
  3925. }
  3926. ret = 0;
  3927. }
  3928. next = next->c2;
  3929. }
  3930. }
  3931. cur = cur->c2;
  3932. }
  3933. }
  3934. /* VC: Unique Element Type Declaration */
  3935. tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
  3936. if ((tst != NULL ) && (tst != elem) &&
  3937. ((tst->prefix == elem->prefix) ||
  3938. (xmlStrEqual(tst->prefix, elem->prefix))) &&
  3939. (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
  3940. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
  3941. "Redefinition of element %s\n",
  3942. elem->name, NULL, NULL);
  3943. ret = 0;
  3944. }
  3945. tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
  3946. if ((tst != NULL ) && (tst != elem) &&
  3947. ((tst->prefix == elem->prefix) ||
  3948. (xmlStrEqual(tst->prefix, elem->prefix))) &&
  3949. (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
  3950. xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
  3951. "Redefinition of element %s\n",
  3952. elem->name, NULL, NULL);
  3953. ret = 0;
  3954. }
  3955. /* One ID per Element Type
  3956. * already done when registering the attribute
  3957. if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
  3958. ret = 0;
  3959. } */
  3960. return(ret);
  3961. }
  3962. /**
  3963. * xmlValidateOneAttribute:
  3964. * @ctxt: the validation context
  3965. * @doc: a document instance
  3966. * @elem: an element instance
  3967. * @attr: an attribute instance
  3968. * @value: the attribute value (without entities processing)
  3969. *
  3970. * Try to validate a single attribute for an element
  3971. * basically it does the following checks as described by the
  3972. * XML-1.0 recommendation:
  3973. * - [ VC: Attribute Value Type ]
  3974. * - [ VC: Fixed Attribute Default ]
  3975. * - [ VC: Entity Name ]
  3976. * - [ VC: Name Token ]
  3977. * - [ VC: ID ]
  3978. * - [ VC: IDREF ]
  3979. * - [ VC: Entity Name ]
  3980. * - [ VC: Notation Attributes ]
  3981. *
  3982. * The ID/IDREF uniqueness and matching are done separately
  3983. *
  3984. * returns 1 if valid or 0 otherwise
  3985. */
  3986. int
  3987. xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  3988. xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
  3989. {
  3990. xmlAttributePtr attrDecl = NULL;
  3991. int val;
  3992. int ret = 1;
  3993. CHECK_DTD;
  3994. if ((elem == NULL) || (elem->name == NULL)) return(0);
  3995. if ((attr == NULL) || (attr->name == NULL)) return(0);
  3996. if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
  3997. xmlChar fn[50];
  3998. xmlChar *fullname;
  3999. fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
  4000. if (fullname == NULL)
  4001. return(0);
  4002. if (attr->ns != NULL) {
  4003. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
  4004. attr->name, attr->ns->prefix);
  4005. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4006. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
  4007. attr->name, attr->ns->prefix);
  4008. } else {
  4009. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
  4010. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4011. attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
  4012. fullname, attr->name);
  4013. }
  4014. if ((fullname != fn) && (fullname != elem->name))
  4015. xmlFree(fullname);
  4016. }
  4017. if (attrDecl == NULL) {
  4018. if (attr->ns != NULL) {
  4019. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
  4020. attr->name, attr->ns->prefix);
  4021. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4022. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
  4023. attr->name, attr->ns->prefix);
  4024. } else {
  4025. attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
  4026. elem->name, attr->name);
  4027. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4028. attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
  4029. elem->name, attr->name);
  4030. }
  4031. }
  4032. /* Validity Constraint: Attribute Value Type */
  4033. if (attrDecl == NULL) {
  4034. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
  4035. "No declaration for attribute %s of element %s\n",
  4036. attr->name, elem->name, NULL);
  4037. return(0);
  4038. }
  4039. attr->atype = attrDecl->atype;
  4040. val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
  4041. if (val == 0) {
  4042. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4043. "Syntax of value for attribute %s of %s is not valid\n",
  4044. attr->name, elem->name, NULL);
  4045. ret = 0;
  4046. }
  4047. /* Validity constraint: Fixed Attribute Default */
  4048. if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
  4049. if (!xmlStrEqual(value, attrDecl->defaultValue)) {
  4050. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
  4051. "Value for attribute %s of %s is different from default \"%s\"\n",
  4052. attr->name, elem->name, attrDecl->defaultValue);
  4053. ret = 0;
  4054. }
  4055. }
  4056. /* Validity Constraint: ID uniqueness */
  4057. if (attrDecl->atype == XML_ATTRIBUTE_ID) {
  4058. if (xmlAddID(ctxt, doc, value, attr) == NULL)
  4059. ret = 0;
  4060. }
  4061. if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
  4062. (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
  4063. if (xmlAddRef(ctxt, doc, value, attr) == NULL)
  4064. ret = 0;
  4065. }
  4066. /* Validity Constraint: Notation Attributes */
  4067. if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
  4068. xmlEnumerationPtr tree = attrDecl->tree;
  4069. xmlNotationPtr nota;
  4070. /* First check that the given NOTATION was declared */
  4071. nota = xmlGetDtdNotationDesc(doc->intSubset, value);
  4072. if (nota == NULL)
  4073. nota = xmlGetDtdNotationDesc(doc->extSubset, value);
  4074. if (nota == NULL) {
  4075. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
  4076. "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
  4077. value, attr->name, elem->name);
  4078. ret = 0;
  4079. }
  4080. /* Second, verify that it's among the list */
  4081. while (tree != NULL) {
  4082. if (xmlStrEqual(tree->name, value)) break;
  4083. tree = tree->next;
  4084. }
  4085. if (tree == NULL) {
  4086. xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
  4087. "Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
  4088. value, attr->name, elem->name);
  4089. ret = 0;
  4090. }
  4091. }
  4092. /* Validity Constraint: Enumeration */
  4093. if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
  4094. xmlEnumerationPtr tree = attrDecl->tree;
  4095. while (tree != NULL) {
  4096. if (xmlStrEqual(tree->name, value)) break;
  4097. tree = tree->next;
  4098. }
  4099. if (tree == NULL) {
  4100. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4101. "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
  4102. value, attr->name, elem->name);
  4103. ret = 0;
  4104. }
  4105. }
  4106. /* Fixed Attribute Default */
  4107. if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
  4108. (!xmlStrEqual(attrDecl->defaultValue, value))) {
  4109. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4110. "Value for attribute %s of %s must be \"%s\"\n",
  4111. attr->name, elem->name, attrDecl->defaultValue);
  4112. ret = 0;
  4113. }
  4114. /* Extra check for the attribute value */
  4115. ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
  4116. attrDecl->atype, value);
  4117. return(ret);
  4118. }
  4119. /**
  4120. * xmlValidateOneNamespace:
  4121. * @ctxt: the validation context
  4122. * @doc: a document instance
  4123. * @elem: an element instance
  4124. * @prefix: the namespace prefix
  4125. * @ns: an namespace declaration instance
  4126. * @value: the attribute value (without entities processing)
  4127. *
  4128. * Try to validate a single namespace declaration for an element
  4129. * basically it does the following checks as described by the
  4130. * XML-1.0 recommendation:
  4131. * - [ VC: Attribute Value Type ]
  4132. * - [ VC: Fixed Attribute Default ]
  4133. * - [ VC: Entity Name ]
  4134. * - [ VC: Name Token ]
  4135. * - [ VC: ID ]
  4136. * - [ VC: IDREF ]
  4137. * - [ VC: Entity Name ]
  4138. * - [ VC: Notation Attributes ]
  4139. *
  4140. * The ID/IDREF uniqueness and matching are done separately
  4141. *
  4142. * returns 1 if valid or 0 otherwise
  4143. */
  4144. int
  4145. xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  4146. xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
  4147. /* xmlElementPtr elemDecl; */
  4148. xmlAttributePtr attrDecl = NULL;
  4149. int val;
  4150. int ret = 1;
  4151. CHECK_DTD;
  4152. if ((elem == NULL) || (elem->name == NULL)) return(0);
  4153. if ((ns == NULL) || (ns->href == NULL)) return(0);
  4154. if (prefix != NULL) {
  4155. xmlChar fn[50];
  4156. xmlChar *fullname;
  4157. fullname = xmlBuildQName(elem->name, prefix, fn, 50);
  4158. if (fullname == NULL) {
  4159. xmlVErrMemory(ctxt, "Validating namespace");
  4160. return(0);
  4161. }
  4162. if (ns->prefix != NULL) {
  4163. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
  4164. ns->prefix, BAD_CAST "xmlns");
  4165. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4166. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
  4167. ns->prefix, BAD_CAST "xmlns");
  4168. } else {
  4169. attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
  4170. BAD_CAST "xmlns");
  4171. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4172. attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
  4173. BAD_CAST "xmlns");
  4174. }
  4175. if ((fullname != fn) && (fullname != elem->name))
  4176. xmlFree(fullname);
  4177. }
  4178. if (attrDecl == NULL) {
  4179. if (ns->prefix != NULL) {
  4180. attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
  4181. ns->prefix, BAD_CAST "xmlns");
  4182. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4183. attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
  4184. ns->prefix, BAD_CAST "xmlns");
  4185. } else {
  4186. attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
  4187. elem->name, BAD_CAST "xmlns");
  4188. if ((attrDecl == NULL) && (doc->extSubset != NULL))
  4189. attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
  4190. elem->name, BAD_CAST "xmlns");
  4191. }
  4192. }
  4193. /* Validity Constraint: Attribute Value Type */
  4194. if (attrDecl == NULL) {
  4195. if (ns->prefix != NULL) {
  4196. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
  4197. "No declaration for attribute xmlns:%s of element %s\n",
  4198. ns->prefix, elem->name, NULL);
  4199. } else {
  4200. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
  4201. "No declaration for attribute xmlns of element %s\n",
  4202. elem->name, NULL, NULL);
  4203. }
  4204. return(0);
  4205. }
  4206. val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
  4207. if (val == 0) {
  4208. if (ns->prefix != NULL) {
  4209. xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
  4210. "Syntax of value for attribute xmlns:%s of %s is not valid\n",
  4211. ns->prefix, elem->name, NULL);
  4212. } else {
  4213. xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
  4214. "Syntax of value for attribute xmlns of %s is not valid\n",
  4215. elem->name, NULL, NULL);
  4216. }
  4217. ret = 0;
  4218. }
  4219. /* Validity constraint: Fixed Attribute Default */
  4220. if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
  4221. if (!xmlStrEqual(value, attrDecl->defaultValue)) {
  4222. if (ns->prefix != NULL) {
  4223. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
  4224. "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
  4225. ns->prefix, elem->name, attrDecl->defaultValue);
  4226. } else {
  4227. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
  4228. "Value for attribute xmlns of %s is different from default \"%s\"\n",
  4229. elem->name, attrDecl->defaultValue, NULL);
  4230. }
  4231. ret = 0;
  4232. }
  4233. }
  4234. /* Validity Constraint: ID uniqueness */
  4235. if (attrDecl->atype == XML_ATTRIBUTE_ID) {
  4236. if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
  4237. ret = 0;
  4238. }
  4239. if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
  4240. (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
  4241. if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
  4242. ret = 0;
  4243. }
  4244. /* Validity Constraint: Notation Attributes */
  4245. if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
  4246. xmlEnumerationPtr tree = attrDecl->tree;
  4247. xmlNotationPtr nota;
  4248. /* First check that the given NOTATION was declared */
  4249. nota = xmlGetDtdNotationDesc(doc->intSubset, value);
  4250. if (nota == NULL)
  4251. nota = xmlGetDtdNotationDesc(doc->extSubset, value);
  4252. if (nota == NULL) {
  4253. if (ns->prefix != NULL) {
  4254. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
  4255. "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
  4256. value, ns->prefix, elem->name);
  4257. } else {
  4258. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
  4259. "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
  4260. value, elem->name, NULL);
  4261. }
  4262. ret = 0;
  4263. }
  4264. /* Second, verify that it's among the list */
  4265. while (tree != NULL) {
  4266. if (xmlStrEqual(tree->name, value)) break;
  4267. tree = tree->next;
  4268. }
  4269. if (tree == NULL) {
  4270. if (ns->prefix != NULL) {
  4271. xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
  4272. "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
  4273. value, ns->prefix, elem->name);
  4274. } else {
  4275. xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
  4276. "Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
  4277. value, elem->name, NULL);
  4278. }
  4279. ret = 0;
  4280. }
  4281. }
  4282. /* Validity Constraint: Enumeration */
  4283. if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
  4284. xmlEnumerationPtr tree = attrDecl->tree;
  4285. while (tree != NULL) {
  4286. if (xmlStrEqual(tree->name, value)) break;
  4287. tree = tree->next;
  4288. }
  4289. if (tree == NULL) {
  4290. if (ns->prefix != NULL) {
  4291. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4292. "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
  4293. value, ns->prefix, elem->name);
  4294. } else {
  4295. xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
  4296. "Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
  4297. value, elem->name, NULL);
  4298. }
  4299. ret = 0;
  4300. }
  4301. }
  4302. /* Fixed Attribute Default */
  4303. if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
  4304. (!xmlStrEqual(attrDecl->defaultValue, value))) {
  4305. if (ns->prefix != NULL) {
  4306. xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
  4307. "Value for attribute xmlns:%s of %s must be \"%s\"\n",
  4308. ns->prefix, elem->name, attrDecl->defaultValue);
  4309. } else {
  4310. xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
  4311. "Value for attribute xmlns of %s must be \"%s\"\n",
  4312. elem->name, attrDecl->defaultValue, NULL);
  4313. }
  4314. ret = 0;
  4315. }
  4316. /* Extra check for the attribute value */
  4317. if (ns->prefix != NULL) {
  4318. ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
  4319. attrDecl->atype, value);
  4320. } else {
  4321. ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
  4322. attrDecl->atype, value);
  4323. }
  4324. return(ret);
  4325. }
  4326. #ifndef LIBXML_REGEXP_ENABLED
  4327. /**
  4328. * xmlValidateSkipIgnorable:
  4329. * @ctxt: the validation context
  4330. * @child: the child list
  4331. *
  4332. * Skip ignorable elements w.r.t. the validation process
  4333. *
  4334. * returns the first element to consider for validation of the content model
  4335. */
  4336. static xmlNodePtr
  4337. xmlValidateSkipIgnorable(xmlNodePtr child) {
  4338. while (child != NULL) {
  4339. switch (child->type) {
  4340. /* These things are ignored (skipped) during validation. */
  4341. case XML_PI_NODE:
  4342. case XML_COMMENT_NODE:
  4343. case XML_XINCLUDE_START:
  4344. case XML_XINCLUDE_END:
  4345. child = child->next;
  4346. break;
  4347. case XML_TEXT_NODE:
  4348. if (xmlIsBlankNode(child))
  4349. child = child->next;
  4350. else
  4351. return(child);
  4352. break;
  4353. /* keep current node */
  4354. default:
  4355. return(child);
  4356. }
  4357. }
  4358. return(child);
  4359. }
  4360. /**
  4361. * xmlValidateElementType:
  4362. * @ctxt: the validation context
  4363. *
  4364. * Try to validate the content model of an element internal function
  4365. *
  4366. * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
  4367. * reference is found and -3 if the validation succeeded but
  4368. * the content model is not determinist.
  4369. */
  4370. static int
  4371. xmlValidateElementType(xmlValidCtxtPtr ctxt) {
  4372. int ret = -1;
  4373. int determinist = 1;
  4374. NODE = xmlValidateSkipIgnorable(NODE);
  4375. if ((NODE == NULL) && (CONT == NULL))
  4376. return(1);
  4377. if ((NODE == NULL) &&
  4378. ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
  4379. (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
  4380. return(1);
  4381. }
  4382. if (CONT == NULL) return(-1);
  4383. if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
  4384. return(-2);
  4385. /*
  4386. * We arrive here when more states need to be examined
  4387. */
  4388. cont:
  4389. /*
  4390. * We just recovered from a rollback generated by a possible
  4391. * epsilon transition, go directly to the analysis phase
  4392. */
  4393. if (STATE == ROLLBACK_PARENT) {
  4394. DEBUG_VALID_MSG("restored parent branch");
  4395. DEBUG_VALID_STATE(NODE, CONT)
  4396. ret = 1;
  4397. goto analyze;
  4398. }
  4399. DEBUG_VALID_STATE(NODE, CONT)
  4400. /*
  4401. * we may have to save a backup state here. This is the equivalent
  4402. * of handling epsilon transition in NFAs.
  4403. */
  4404. if ((CONT != NULL) &&
  4405. ((CONT->parent == NULL) ||
  4406. (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
  4407. ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
  4408. (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
  4409. ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
  4410. DEBUG_VALID_MSG("saving parent branch");
  4411. if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
  4412. return(0);
  4413. }
  4414. /*
  4415. * Check first if the content matches
  4416. */
  4417. switch (CONT->type) {
  4418. case XML_ELEMENT_CONTENT_PCDATA:
  4419. if (NODE == NULL) {
  4420. DEBUG_VALID_MSG("pcdata failed no node");
  4421. ret = 0;
  4422. break;
  4423. }
  4424. if (NODE->type == XML_TEXT_NODE) {
  4425. DEBUG_VALID_MSG("pcdata found, skip to next");
  4426. /*
  4427. * go to next element in the content model
  4428. * skipping ignorable elems
  4429. */
  4430. do {
  4431. NODE = NODE->next;
  4432. NODE = xmlValidateSkipIgnorable(NODE);
  4433. if ((NODE != NULL) &&
  4434. (NODE->type == XML_ENTITY_REF_NODE))
  4435. return(-2);
  4436. } while ((NODE != NULL) &&
  4437. ((NODE->type != XML_ELEMENT_NODE) &&
  4438. (NODE->type != XML_TEXT_NODE) &&
  4439. (NODE->type != XML_CDATA_SECTION_NODE)));
  4440. ret = 1;
  4441. break;
  4442. } else {
  4443. DEBUG_VALID_MSG("pcdata failed");
  4444. ret = 0;
  4445. break;
  4446. }
  4447. break;
  4448. case XML_ELEMENT_CONTENT_ELEMENT:
  4449. if (NODE == NULL) {
  4450. DEBUG_VALID_MSG("element failed no node");
  4451. ret = 0;
  4452. break;
  4453. }
  4454. ret = ((NODE->type == XML_ELEMENT_NODE) &&
  4455. (xmlStrEqual(NODE->name, CONT->name)));
  4456. if (ret == 1) {
  4457. if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
  4458. ret = (CONT->prefix == NULL);
  4459. } else if (CONT->prefix == NULL) {
  4460. ret = 0;
  4461. } else {
  4462. ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
  4463. }
  4464. }
  4465. if (ret == 1) {
  4466. DEBUG_VALID_MSG("element found, skip to next");
  4467. /*
  4468. * go to next element in the content model
  4469. * skipping ignorable elems
  4470. */
  4471. do {
  4472. NODE = NODE->next;
  4473. NODE = xmlValidateSkipIgnorable(NODE);
  4474. if ((NODE != NULL) &&
  4475. (NODE->type == XML_ENTITY_REF_NODE))
  4476. return(-2);
  4477. } while ((NODE != NULL) &&
  4478. ((NODE->type != XML_ELEMENT_NODE) &&
  4479. (NODE->type != XML_TEXT_NODE) &&
  4480. (NODE->type != XML_CDATA_SECTION_NODE)));
  4481. } else {
  4482. DEBUG_VALID_MSG("element failed");
  4483. ret = 0;
  4484. break;
  4485. }
  4486. break;
  4487. case XML_ELEMENT_CONTENT_OR:
  4488. /*
  4489. * Small optimization.
  4490. */
  4491. if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
  4492. if ((NODE == NULL) ||
  4493. (!xmlStrEqual(NODE->name, CONT->c1->name))) {
  4494. DEPTH++;
  4495. CONT = CONT->c2;
  4496. goto cont;
  4497. }
  4498. if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
  4499. ret = (CONT->c1->prefix == NULL);
  4500. } else if (CONT->c1->prefix == NULL) {
  4501. ret = 0;
  4502. } else {
  4503. ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
  4504. }
  4505. if (ret == 0) {
  4506. DEPTH++;
  4507. CONT = CONT->c2;
  4508. goto cont;
  4509. }
  4510. }
  4511. /*
  4512. * save the second branch 'or' branch
  4513. */
  4514. DEBUG_VALID_MSG("saving 'or' branch");
  4515. if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
  4516. OCCURS, ROLLBACK_OR) < 0)
  4517. return(-1);
  4518. DEPTH++;
  4519. CONT = CONT->c1;
  4520. goto cont;
  4521. case XML_ELEMENT_CONTENT_SEQ:
  4522. /*
  4523. * Small optimization.
  4524. */
  4525. if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
  4526. ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
  4527. (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
  4528. if ((NODE == NULL) ||
  4529. (!xmlStrEqual(NODE->name, CONT->c1->name))) {
  4530. DEPTH++;
  4531. CONT = CONT->c2;
  4532. goto cont;
  4533. }
  4534. if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
  4535. ret = (CONT->c1->prefix == NULL);
  4536. } else if (CONT->c1->prefix == NULL) {
  4537. ret = 0;
  4538. } else {
  4539. ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
  4540. }
  4541. if (ret == 0) {
  4542. DEPTH++;
  4543. CONT = CONT->c2;
  4544. goto cont;
  4545. }
  4546. }
  4547. DEPTH++;
  4548. CONT = CONT->c1;
  4549. goto cont;
  4550. }
  4551. /*
  4552. * At this point handle going up in the tree
  4553. */
  4554. if (ret == -1) {
  4555. DEBUG_VALID_MSG("error found returning");
  4556. return(ret);
  4557. }
  4558. analyze:
  4559. while (CONT != NULL) {
  4560. /*
  4561. * First do the analysis depending on the occurrence model at
  4562. * this level.
  4563. */
  4564. if (ret == 0) {
  4565. switch (CONT->ocur) {
  4566. xmlNodePtr cur;
  4567. case XML_ELEMENT_CONTENT_ONCE:
  4568. cur = ctxt->vstate->node;
  4569. DEBUG_VALID_MSG("Once branch failed, rollback");
  4570. if (vstateVPop(ctxt) < 0 ) {
  4571. DEBUG_VALID_MSG("exhaustion, failed");
  4572. return(0);
  4573. }
  4574. if (cur != ctxt->vstate->node)
  4575. determinist = -3;
  4576. goto cont;
  4577. case XML_ELEMENT_CONTENT_PLUS:
  4578. if (OCCURRENCE == 0) {
  4579. cur = ctxt->vstate->node;
  4580. DEBUG_VALID_MSG("Plus branch failed, rollback");
  4581. if (vstateVPop(ctxt) < 0 ) {
  4582. DEBUG_VALID_MSG("exhaustion, failed");
  4583. return(0);
  4584. }
  4585. if (cur != ctxt->vstate->node)
  4586. determinist = -3;
  4587. goto cont;
  4588. }
  4589. DEBUG_VALID_MSG("Plus branch found");
  4590. ret = 1;
  4591. break;
  4592. case XML_ELEMENT_CONTENT_MULT:
  4593. #ifdef DEBUG_VALID_ALGO
  4594. if (OCCURRENCE == 0) {
  4595. DEBUG_VALID_MSG("Mult branch failed");
  4596. } else {
  4597. DEBUG_VALID_MSG("Mult branch found");
  4598. }
  4599. #endif
  4600. ret = 1;
  4601. break;
  4602. case XML_ELEMENT_CONTENT_OPT:
  4603. DEBUG_VALID_MSG("Option branch failed");
  4604. ret = 1;
  4605. break;
  4606. }
  4607. } else {
  4608. switch (CONT->ocur) {
  4609. case XML_ELEMENT_CONTENT_OPT:
  4610. DEBUG_VALID_MSG("Option branch succeeded");
  4611. ret = 1;
  4612. break;
  4613. case XML_ELEMENT_CONTENT_ONCE:
  4614. DEBUG_VALID_MSG("Once branch succeeded");
  4615. ret = 1;
  4616. break;
  4617. case XML_ELEMENT_CONTENT_PLUS:
  4618. if (STATE == ROLLBACK_PARENT) {
  4619. DEBUG_VALID_MSG("Plus branch rollback");
  4620. ret = 1;
  4621. break;
  4622. }
  4623. if (NODE == NULL) {
  4624. DEBUG_VALID_MSG("Plus branch exhausted");
  4625. ret = 1;
  4626. break;
  4627. }
  4628. DEBUG_VALID_MSG("Plus branch succeeded, continuing");
  4629. SET_OCCURRENCE;
  4630. goto cont;
  4631. case XML_ELEMENT_CONTENT_MULT:
  4632. if (STATE == ROLLBACK_PARENT) {
  4633. DEBUG_VALID_MSG("Mult branch rollback");
  4634. ret = 1;
  4635. break;
  4636. }
  4637. if (NODE == NULL) {
  4638. DEBUG_VALID_MSG("Mult branch exhausted");
  4639. ret = 1;
  4640. break;
  4641. }
  4642. DEBUG_VALID_MSG("Mult branch succeeded, continuing");
  4643. /* SET_OCCURRENCE; */
  4644. goto cont;
  4645. }
  4646. }
  4647. STATE = 0;
  4648. /*
  4649. * Then act accordingly at the parent level
  4650. */
  4651. RESET_OCCURRENCE;
  4652. if (CONT->parent == NULL)
  4653. break;
  4654. switch (CONT->parent->type) {
  4655. case XML_ELEMENT_CONTENT_PCDATA:
  4656. DEBUG_VALID_MSG("Error: parent pcdata");
  4657. return(-1);
  4658. case XML_ELEMENT_CONTENT_ELEMENT:
  4659. DEBUG_VALID_MSG("Error: parent element");
  4660. return(-1);
  4661. case XML_ELEMENT_CONTENT_OR:
  4662. if (ret == 1) {
  4663. DEBUG_VALID_MSG("Or succeeded");
  4664. CONT = CONT->parent;
  4665. DEPTH--;
  4666. } else {
  4667. DEBUG_VALID_MSG("Or failed");
  4668. CONT = CONT->parent;
  4669. DEPTH--;
  4670. }
  4671. break;
  4672. case XML_ELEMENT_CONTENT_SEQ:
  4673. if (ret == 0) {
  4674. DEBUG_VALID_MSG("Sequence failed");
  4675. CONT = CONT->parent;
  4676. DEPTH--;
  4677. } else if (CONT == CONT->parent->c1) {
  4678. DEBUG_VALID_MSG("Sequence testing 2nd branch");
  4679. CONT = CONT->parent->c2;
  4680. goto cont;
  4681. } else {
  4682. DEBUG_VALID_MSG("Sequence succeeded");
  4683. CONT = CONT->parent;
  4684. DEPTH--;
  4685. }
  4686. }
  4687. }
  4688. if (NODE != NULL) {
  4689. xmlNodePtr cur;
  4690. cur = ctxt->vstate->node;
  4691. DEBUG_VALID_MSG("Failed, remaining input, rollback");
  4692. if (vstateVPop(ctxt) < 0 ) {
  4693. DEBUG_VALID_MSG("exhaustion, failed");
  4694. return(0);
  4695. }
  4696. if (cur != ctxt->vstate->node)
  4697. determinist = -3;
  4698. goto cont;
  4699. }
  4700. if (ret == 0) {
  4701. xmlNodePtr cur;
  4702. cur = ctxt->vstate->node;
  4703. DEBUG_VALID_MSG("Failure, rollback");
  4704. if (vstateVPop(ctxt) < 0 ) {
  4705. DEBUG_VALID_MSG("exhaustion, failed");
  4706. return(0);
  4707. }
  4708. if (cur != ctxt->vstate->node)
  4709. determinist = -3;
  4710. goto cont;
  4711. }
  4712. return(determinist);
  4713. }
  4714. #endif
  4715. /**
  4716. * xmlSnprintfElements:
  4717. * @buf: an output buffer
  4718. * @size: the size of the buffer
  4719. * @content: An element
  4720. * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
  4721. *
  4722. * This will dump the list of elements to the buffer
  4723. * Intended just for the debug routine
  4724. */
  4725. static void
  4726. xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
  4727. xmlNodePtr cur;
  4728. int len;
  4729. if (node == NULL) return;
  4730. if (glob) strcat(buf, "(");
  4731. cur = node;
  4732. while (cur != NULL) {
  4733. len = strlen(buf);
  4734. if (size - len < 50) {
  4735. if ((size - len > 4) && (buf[len - 1] != '.'))
  4736. strcat(buf, " ...");
  4737. return;
  4738. }
  4739. switch (cur->type) {
  4740. case XML_ELEMENT_NODE:
  4741. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  4742. if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
  4743. if ((size - len > 4) && (buf[len - 1] != '.'))
  4744. strcat(buf, " ...");
  4745. return;
  4746. }
  4747. strcat(buf, (char *) cur->ns->prefix);
  4748. strcat(buf, ":");
  4749. }
  4750. if (size - len < xmlStrlen(cur->name) + 10) {
  4751. if ((size - len > 4) && (buf[len - 1] != '.'))
  4752. strcat(buf, " ...");
  4753. return;
  4754. }
  4755. strcat(buf, (char *) cur->name);
  4756. if (cur->next != NULL)
  4757. strcat(buf, " ");
  4758. break;
  4759. case XML_TEXT_NODE:
  4760. if (xmlIsBlankNode(cur))
  4761. break;
  4762. case XML_CDATA_SECTION_NODE:
  4763. case XML_ENTITY_REF_NODE:
  4764. strcat(buf, "CDATA");
  4765. if (cur->next != NULL)
  4766. strcat(buf, " ");
  4767. break;
  4768. case XML_ATTRIBUTE_NODE:
  4769. case XML_DOCUMENT_NODE:
  4770. #ifdef LIBXML_DOCB_ENABLED
  4771. case XML_DOCB_DOCUMENT_NODE:
  4772. #endif
  4773. case XML_HTML_DOCUMENT_NODE:
  4774. case XML_DOCUMENT_TYPE_NODE:
  4775. case XML_DOCUMENT_FRAG_NODE:
  4776. case XML_NOTATION_NODE:
  4777. case XML_NAMESPACE_DECL:
  4778. strcat(buf, "???");
  4779. if (cur->next != NULL)
  4780. strcat(buf, " ");
  4781. break;
  4782. case XML_ENTITY_NODE:
  4783. case XML_PI_NODE:
  4784. case XML_DTD_NODE:
  4785. case XML_COMMENT_NODE:
  4786. case XML_ELEMENT_DECL:
  4787. case XML_ATTRIBUTE_DECL:
  4788. case XML_ENTITY_DECL:
  4789. case XML_XINCLUDE_START:
  4790. case XML_XINCLUDE_END:
  4791. break;
  4792. }
  4793. cur = cur->next;
  4794. }
  4795. if (glob) strcat(buf, ")");
  4796. }
  4797. /**
  4798. * xmlValidateElementContent:
  4799. * @ctxt: the validation context
  4800. * @child: the child list
  4801. * @elemDecl: pointer to the element declaration
  4802. * @warn: emit the error message
  4803. * @parent: the parent element (for error reporting)
  4804. *
  4805. * Try to validate the content model of an element
  4806. *
  4807. * returns 1 if valid or 0 if not and -1 in case of error
  4808. */
  4809. static int
  4810. xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
  4811. xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
  4812. int ret = 1;
  4813. #ifndef LIBXML_REGEXP_ENABLED
  4814. xmlNodePtr repl = NULL, last = NULL, tmp;
  4815. #endif
  4816. xmlNodePtr cur;
  4817. xmlElementContentPtr cont;
  4818. const xmlChar *name;
  4819. if (elemDecl == NULL)
  4820. return(-1);
  4821. cont = elemDecl->content;
  4822. name = elemDecl->name;
  4823. #ifdef LIBXML_REGEXP_ENABLED
  4824. /* Build the regexp associated to the content model */
  4825. if (elemDecl->contModel == NULL)
  4826. ret = xmlValidBuildContentModel(ctxt, elemDecl);
  4827. if (elemDecl->contModel == NULL) {
  4828. return(-1);
  4829. } else {
  4830. xmlRegExecCtxtPtr exec;
  4831. if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
  4832. return(-1);
  4833. }
  4834. ctxt->nodeMax = 0;
  4835. ctxt->nodeNr = 0;
  4836. ctxt->nodeTab = NULL;
  4837. exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
  4838. if (exec != NULL) {
  4839. cur = child;
  4840. while (cur != NULL) {
  4841. switch (cur->type) {
  4842. case XML_ENTITY_REF_NODE:
  4843. /*
  4844. * Push the current node to be able to roll back
  4845. * and process within the entity
  4846. */
  4847. if ((cur->children != NULL) &&
  4848. (cur->children->children != NULL)) {
  4849. nodeVPush(ctxt, cur);
  4850. cur = cur->children->children;
  4851. continue;
  4852. }
  4853. break;
  4854. case XML_TEXT_NODE:
  4855. if (xmlIsBlankNode(cur))
  4856. break;
  4857. ret = 0;
  4858. goto fail;
  4859. case XML_CDATA_SECTION_NODE:
  4860. /* TODO */
  4861. ret = 0;
  4862. goto fail;
  4863. case XML_ELEMENT_NODE:
  4864. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  4865. xmlChar fn[50];
  4866. xmlChar *fullname;
  4867. fullname = xmlBuildQName(cur->name,
  4868. cur->ns->prefix, fn, 50);
  4869. if (fullname == NULL) {
  4870. ret = -1;
  4871. goto fail;
  4872. }
  4873. ret = xmlRegExecPushString(exec, fullname, NULL);
  4874. if ((fullname != fn) && (fullname != cur->name))
  4875. xmlFree(fullname);
  4876. } else {
  4877. ret = xmlRegExecPushString(exec, cur->name, NULL);
  4878. }
  4879. break;
  4880. default:
  4881. break;
  4882. }
  4883. /*
  4884. * Switch to next element
  4885. */
  4886. cur = cur->next;
  4887. while (cur == NULL) {
  4888. cur = nodeVPop(ctxt);
  4889. if (cur == NULL)
  4890. break;
  4891. cur = cur->next;
  4892. }
  4893. }
  4894. ret = xmlRegExecPushString(exec, NULL, NULL);
  4895. fail:
  4896. xmlRegFreeExecCtxt(exec);
  4897. }
  4898. }
  4899. #else /* LIBXML_REGEXP_ENABLED */
  4900. /*
  4901. * Allocate the stack
  4902. */
  4903. ctxt->vstateMax = 8;
  4904. ctxt->vstateTab = (xmlValidState *) xmlMalloc(
  4905. ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
  4906. if (ctxt->vstateTab == NULL) {
  4907. xmlVErrMemory(ctxt, "malloc failed");
  4908. return(-1);
  4909. }
  4910. /*
  4911. * The first entry in the stack is reserved to the current state
  4912. */
  4913. ctxt->nodeMax = 0;
  4914. ctxt->nodeNr = 0;
  4915. ctxt->nodeTab = NULL;
  4916. ctxt->vstate = &ctxt->vstateTab[0];
  4917. ctxt->vstateNr = 1;
  4918. CONT = cont;
  4919. NODE = child;
  4920. DEPTH = 0;
  4921. OCCURS = 0;
  4922. STATE = 0;
  4923. ret = xmlValidateElementType(ctxt);
  4924. if ((ret == -3) && (warn)) {
  4925. xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
  4926. "Content model for Element %s is ambiguous\n",
  4927. name, NULL, NULL);
  4928. } else if (ret == -2) {
  4929. /*
  4930. * An entities reference appeared at this level.
  4931. * Buid a minimal representation of this node content
  4932. * sufficient to run the validation process on it
  4933. */
  4934. DEBUG_VALID_MSG("Found an entity reference, linearizing");
  4935. cur = child;
  4936. while (cur != NULL) {
  4937. switch (cur->type) {
  4938. case XML_ENTITY_REF_NODE:
  4939. /*
  4940. * Push the current node to be able to roll back
  4941. * and process within the entity
  4942. */
  4943. if ((cur->children != NULL) &&
  4944. (cur->children->children != NULL)) {
  4945. nodeVPush(ctxt, cur);
  4946. cur = cur->children->children;
  4947. continue;
  4948. }
  4949. break;
  4950. case XML_TEXT_NODE:
  4951. if (xmlIsBlankNode(cur))
  4952. break;
  4953. /* no break on purpose */
  4954. case XML_CDATA_SECTION_NODE:
  4955. /* no break on purpose */
  4956. case XML_ELEMENT_NODE:
  4957. /*
  4958. * Allocate a new node and minimally fills in
  4959. * what's required
  4960. */
  4961. tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  4962. if (tmp == NULL) {
  4963. xmlVErrMemory(ctxt, "malloc failed");
  4964. xmlFreeNodeList(repl);
  4965. ret = -1;
  4966. goto done;
  4967. }
  4968. tmp->type = cur->type;
  4969. tmp->name = cur->name;
  4970. tmp->ns = cur->ns;
  4971. tmp->next = NULL;
  4972. tmp->content = NULL;
  4973. if (repl == NULL)
  4974. repl = last = tmp;
  4975. else {
  4976. last->next = tmp;
  4977. last = tmp;
  4978. }
  4979. if (cur->type == XML_CDATA_SECTION_NODE) {
  4980. /*
  4981. * E59 spaces in CDATA does not match the
  4982. * nonterminal S
  4983. */
  4984. tmp->content = xmlStrdup(BAD_CAST "CDATA");
  4985. }
  4986. break;
  4987. default:
  4988. break;
  4989. }
  4990. /*
  4991. * Switch to next element
  4992. */
  4993. cur = cur->next;
  4994. while (cur == NULL) {
  4995. cur = nodeVPop(ctxt);
  4996. if (cur == NULL)
  4997. break;
  4998. cur = cur->next;
  4999. }
  5000. }
  5001. /*
  5002. * Relaunch the validation
  5003. */
  5004. ctxt->vstate = &ctxt->vstateTab[0];
  5005. ctxt->vstateNr = 1;
  5006. CONT = cont;
  5007. NODE = repl;
  5008. DEPTH = 0;
  5009. OCCURS = 0;
  5010. STATE = 0;
  5011. ret = xmlValidateElementType(ctxt);
  5012. }
  5013. #endif /* LIBXML_REGEXP_ENABLED */
  5014. if ((warn) && ((ret != 1) && (ret != -3))) {
  5015. if (ctxt != NULL) {
  5016. char expr[5000];
  5017. char list[5000];
  5018. expr[0] = 0;
  5019. xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
  5020. list[0] = 0;
  5021. #ifndef LIBXML_REGEXP_ENABLED
  5022. if (repl != NULL)
  5023. xmlSnprintfElements(&list[0], 5000, repl, 1);
  5024. else
  5025. #endif /* LIBXML_REGEXP_ENABLED */
  5026. xmlSnprintfElements(&list[0], 5000, child, 1);
  5027. if (name != NULL) {
  5028. xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
  5029. "Element %s content does not follow the DTD, expecting %s, got %s\n",
  5030. name, BAD_CAST expr, BAD_CAST list);
  5031. } else {
  5032. xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
  5033. "Element content does not follow the DTD, expecting %s, got %s\n",
  5034. BAD_CAST expr, BAD_CAST list, NULL);
  5035. }
  5036. } else {
  5037. if (name != NULL) {
  5038. xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
  5039. "Element %s content does not follow the DTD\n",
  5040. name, NULL, NULL);
  5041. } else {
  5042. xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
  5043. "Element content does not follow the DTD\n",
  5044. NULL, NULL, NULL);
  5045. }
  5046. }
  5047. ret = 0;
  5048. }
  5049. if (ret == -3)
  5050. ret = 1;
  5051. #ifndef LIBXML_REGEXP_ENABLED
  5052. done:
  5053. /*
  5054. * Deallocate the copy if done, and free up the validation stack
  5055. */
  5056. while (repl != NULL) {
  5057. tmp = repl->next;
  5058. xmlFree(repl);
  5059. repl = tmp;
  5060. }
  5061. ctxt->vstateMax = 0;
  5062. if (ctxt->vstateTab != NULL) {
  5063. xmlFree(ctxt->vstateTab);
  5064. ctxt->vstateTab = NULL;
  5065. }
  5066. #endif
  5067. ctxt->nodeMax = 0;
  5068. ctxt->nodeNr = 0;
  5069. if (ctxt->nodeTab != NULL) {
  5070. xmlFree(ctxt->nodeTab);
  5071. ctxt->nodeTab = NULL;
  5072. }
  5073. return(ret);
  5074. }
  5075. /**
  5076. * xmlValidateCdataElement:
  5077. * @ctxt: the validation context
  5078. * @doc: a document instance
  5079. * @elem: an element instance
  5080. *
  5081. * Check that an element follows #CDATA
  5082. *
  5083. * returns 1 if valid or 0 otherwise
  5084. */
  5085. static int
  5086. xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  5087. xmlNodePtr elem) {
  5088. int ret = 1;
  5089. xmlNodePtr cur, child;
  5090. if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
  5091. return(0);
  5092. child = elem->children;
  5093. cur = child;
  5094. while (cur != NULL) {
  5095. switch (cur->type) {
  5096. case XML_ENTITY_REF_NODE:
  5097. /*
  5098. * Push the current node to be able to roll back
  5099. * and process within the entity
  5100. */
  5101. if ((cur->children != NULL) &&
  5102. (cur->children->children != NULL)) {
  5103. nodeVPush(ctxt, cur);
  5104. cur = cur->children->children;
  5105. continue;
  5106. }
  5107. break;
  5108. case XML_COMMENT_NODE:
  5109. case XML_PI_NODE:
  5110. case XML_TEXT_NODE:
  5111. case XML_CDATA_SECTION_NODE:
  5112. break;
  5113. default:
  5114. ret = 0;
  5115. goto done;
  5116. }
  5117. /*
  5118. * Switch to next element
  5119. */
  5120. cur = cur->next;
  5121. while (cur == NULL) {
  5122. cur = nodeVPop(ctxt);
  5123. if (cur == NULL)
  5124. break;
  5125. cur = cur->next;
  5126. }
  5127. }
  5128. done:
  5129. ctxt->nodeMax = 0;
  5130. ctxt->nodeNr = 0;
  5131. if (ctxt->nodeTab != NULL) {
  5132. xmlFree(ctxt->nodeTab);
  5133. ctxt->nodeTab = NULL;
  5134. }
  5135. return(ret);
  5136. }
  5137. /**
  5138. * xmlValidateCheckMixed:
  5139. * @ctxt: the validation context
  5140. * @cont: the mixed content model
  5141. * @qname: the qualified name as appearing in the serialization
  5142. *
  5143. * Check if the given node is part of the content model.
  5144. *
  5145. * Returns 1 if yes, 0 if no, -1 in case of error
  5146. */
  5147. static int
  5148. xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
  5149. xmlElementContentPtr cont, const xmlChar *qname) {
  5150. const xmlChar *name;
  5151. int plen;
  5152. name = xmlSplitQName3(qname, &plen);
  5153. if (name == NULL) {
  5154. while (cont != NULL) {
  5155. if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
  5156. if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
  5157. return(1);
  5158. } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
  5159. (cont->c1 != NULL) &&
  5160. (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
  5161. if ((cont->c1->prefix == NULL) &&
  5162. (xmlStrEqual(cont->c1->name, qname)))
  5163. return(1);
  5164. } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
  5165. (cont->c1 == NULL) ||
  5166. (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
  5167. xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
  5168. "Internal: MIXED struct corrupted\n",
  5169. NULL);
  5170. break;
  5171. }
  5172. cont = cont->c2;
  5173. }
  5174. } else {
  5175. while (cont != NULL) {
  5176. if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
  5177. if ((cont->prefix != NULL) &&
  5178. (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
  5179. (xmlStrEqual(cont->name, name)))
  5180. return(1);
  5181. } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
  5182. (cont->c1 != NULL) &&
  5183. (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
  5184. if ((cont->c1->prefix != NULL) &&
  5185. (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
  5186. (xmlStrEqual(cont->c1->name, name)))
  5187. return(1);
  5188. } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
  5189. (cont->c1 == NULL) ||
  5190. (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
  5191. xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
  5192. "Internal: MIXED struct corrupted\n",
  5193. NULL);
  5194. break;
  5195. }
  5196. cont = cont->c2;
  5197. }
  5198. }
  5199. return(0);
  5200. }
  5201. /**
  5202. * xmlValidGetElemDecl:
  5203. * @ctxt: the validation context
  5204. * @doc: a document instance
  5205. * @elem: an element instance
  5206. * @extsubset: pointer, (out) indicate if the declaration was found
  5207. * in the external subset.
  5208. *
  5209. * Finds a declaration associated to an element in the document.
  5210. *
  5211. * returns the pointer to the declaration or NULL if not found.
  5212. */
  5213. static xmlElementPtr
  5214. xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  5215. xmlNodePtr elem, int *extsubset) {
  5216. xmlElementPtr elemDecl = NULL;
  5217. const xmlChar *prefix = NULL;
  5218. if ((ctxt == NULL) || (doc == NULL) ||
  5219. (elem == NULL) || (elem->name == NULL))
  5220. return(NULL);
  5221. if (extsubset != NULL)
  5222. *extsubset = 0;
  5223. /*
  5224. * Fetch the declaration for the qualified name
  5225. */
  5226. if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
  5227. prefix = elem->ns->prefix;
  5228. if (prefix != NULL) {
  5229. elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
  5230. elem->name, prefix);
  5231. if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
  5232. elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
  5233. elem->name, prefix);
  5234. if ((elemDecl != NULL) && (extsubset != NULL))
  5235. *extsubset = 1;
  5236. }
  5237. }
  5238. /*
  5239. * Fetch the declaration for the non qualified name
  5240. * This is "non-strict" validation should be done on the
  5241. * full QName but in that case being flexible makes sense.
  5242. */
  5243. if (elemDecl == NULL) {
  5244. elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
  5245. if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
  5246. elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
  5247. if ((elemDecl != NULL) && (extsubset != NULL))
  5248. *extsubset = 1;
  5249. }
  5250. }
  5251. if (elemDecl == NULL) {
  5252. xmlErrValidNode(ctxt, elem,
  5253. XML_DTD_UNKNOWN_ELEM,
  5254. "No declaration for element %s\n",
  5255. elem->name, NULL, NULL);
  5256. }
  5257. return(elemDecl);
  5258. }
  5259. #ifdef LIBXML_REGEXP_ENABLED
  5260. /**
  5261. * xmlValidatePushElement:
  5262. * @ctxt: the validation context
  5263. * @doc: a document instance
  5264. * @elem: an element instance
  5265. * @qname: the qualified name as appearing in the serialization
  5266. *
  5267. * Push a new element start on the validation stack.
  5268. *
  5269. * returns 1 if no validation problem was found or 0 otherwise
  5270. */
  5271. int
  5272. xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  5273. xmlNodePtr elem, const xmlChar *qname) {
  5274. int ret = 1;
  5275. xmlElementPtr eDecl;
  5276. int extsubset = 0;
  5277. if (ctxt == NULL)
  5278. return(0);
  5279. /* printf("PushElem %s\n", qname); */
  5280. if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
  5281. xmlValidStatePtr state = ctxt->vstate;
  5282. xmlElementPtr elemDecl;
  5283. /*
  5284. * Check the new element agaisnt the content model of the new elem.
  5285. */
  5286. if (state->elemDecl != NULL) {
  5287. elemDecl = state->elemDecl;
  5288. switch(elemDecl->etype) {
  5289. case XML_ELEMENT_TYPE_UNDEFINED:
  5290. ret = 0;
  5291. break;
  5292. case XML_ELEMENT_TYPE_EMPTY:
  5293. xmlErrValidNode(ctxt, state->node,
  5294. XML_DTD_NOT_EMPTY,
  5295. "Element %s was declared EMPTY this one has content\n",
  5296. state->node->name, NULL, NULL);
  5297. ret = 0;
  5298. break;
  5299. case XML_ELEMENT_TYPE_ANY:
  5300. /* I don't think anything is required then */
  5301. break;
  5302. case XML_ELEMENT_TYPE_MIXED:
  5303. /* simple case of declared as #PCDATA */
  5304. if ((elemDecl->content != NULL) &&
  5305. (elemDecl->content->type ==
  5306. XML_ELEMENT_CONTENT_PCDATA)) {
  5307. xmlErrValidNode(ctxt, state->node,
  5308. XML_DTD_NOT_PCDATA,
  5309. "Element %s was declared #PCDATA but contains non text nodes\n",
  5310. state->node->name, NULL, NULL);
  5311. ret = 0;
  5312. } else {
  5313. ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
  5314. qname);
  5315. if (ret != 1) {
  5316. xmlErrValidNode(ctxt, state->node,
  5317. XML_DTD_INVALID_CHILD,
  5318. "Element %s is not declared in %s list of possible children\n",
  5319. qname, state->node->name, NULL);
  5320. }
  5321. }
  5322. break;
  5323. case XML_ELEMENT_TYPE_ELEMENT:
  5324. /*
  5325. * TODO:
  5326. * VC: Standalone Document Declaration
  5327. * - element types with element content, if white space
  5328. * occurs directly within any instance of those types.
  5329. */
  5330. if (state->exec != NULL) {
  5331. ret = xmlRegExecPushString(state->exec, qname, NULL);
  5332. if (ret < 0) {
  5333. xmlErrValidNode(ctxt, state->node,
  5334. XML_DTD_CONTENT_MODEL,
  5335. "Element %s content does not follow the DTD, Misplaced %s\n",
  5336. state->node->name, qname, NULL);
  5337. ret = 0;
  5338. } else {
  5339. ret = 1;
  5340. }
  5341. }
  5342. break;
  5343. }
  5344. }
  5345. }
  5346. eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
  5347. vstateVPush(ctxt, eDecl, elem);
  5348. return(ret);
  5349. }
  5350. /**
  5351. * xmlValidatePushCData:
  5352. * @ctxt: the validation context
  5353. * @data: some character data read
  5354. * @len: the lenght of the data
  5355. *
  5356. * check the CData parsed for validation in the current stack
  5357. *
  5358. * returns 1 if no validation problem was found or 0 otherwise
  5359. */
  5360. int
  5361. xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
  5362. int ret = 1;
  5363. /* printf("CDATA %s %d\n", data, len); */
  5364. if (ctxt == NULL)
  5365. return(0);
  5366. if (len <= 0)
  5367. return(ret);
  5368. if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
  5369. xmlValidStatePtr state = ctxt->vstate;
  5370. xmlElementPtr elemDecl;
  5371. /*
  5372. * Check the new element agaisnt the content model of the new elem.
  5373. */
  5374. if (state->elemDecl != NULL) {
  5375. elemDecl = state->elemDecl;
  5376. switch(elemDecl->etype) {
  5377. case XML_ELEMENT_TYPE_UNDEFINED:
  5378. ret = 0;
  5379. break;
  5380. case XML_ELEMENT_TYPE_EMPTY:
  5381. xmlErrValidNode(ctxt, state->node,
  5382. XML_DTD_NOT_EMPTY,
  5383. "Element %s was declared EMPTY this one has content\n",
  5384. state->node->name, NULL, NULL);
  5385. ret = 0;
  5386. break;
  5387. case XML_ELEMENT_TYPE_ANY:
  5388. break;
  5389. case XML_ELEMENT_TYPE_MIXED:
  5390. break;
  5391. case XML_ELEMENT_TYPE_ELEMENT:
  5392. if (len > 0) {
  5393. int i;
  5394. for (i = 0;i < len;i++) {
  5395. if (!IS_BLANK_CH(data[i])) {
  5396. xmlErrValidNode(ctxt, state->node,
  5397. XML_DTD_CONTENT_MODEL,
  5398. "Element %s content does not follow the DTD, Text not allowed\n",
  5399. state->node->name, NULL, NULL);
  5400. ret = 0;
  5401. goto done;
  5402. }
  5403. }
  5404. /*
  5405. * TODO:
  5406. * VC: Standalone Document Declaration
  5407. * element types with element content, if white space
  5408. * occurs directly within any instance of those types.
  5409. */
  5410. }
  5411. break;
  5412. }
  5413. }
  5414. }
  5415. done:
  5416. return(ret);
  5417. }
  5418. /**
  5419. * xmlValidatePopElement:
  5420. * @ctxt: the validation context
  5421. * @doc: a document instance
  5422. * @elem: an element instance
  5423. * @qname: the qualified name as appearing in the serialization
  5424. *
  5425. * Pop the element end from the validation stack.
  5426. *
  5427. * returns 1 if no validation problem was found or 0 otherwise
  5428. */
  5429. int
  5430. xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
  5431. xmlNodePtr elem ATTRIBUTE_UNUSED,
  5432. const xmlChar *qname ATTRIBUTE_UNUSED) {
  5433. int ret = 1;
  5434. if (ctxt == NULL)
  5435. return(0);
  5436. /* printf("PopElem %s\n", qname); */
  5437. if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
  5438. xmlValidStatePtr state = ctxt->vstate;
  5439. xmlElementPtr elemDecl;
  5440. /*
  5441. * Check the new element agaisnt the content model of the new elem.
  5442. */
  5443. if (state->elemDecl != NULL) {
  5444. elemDecl = state->elemDecl;
  5445. if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
  5446. if (state->exec != NULL) {
  5447. ret = xmlRegExecPushString(state->exec, NULL, NULL);
  5448. if (ret == 0) {
  5449. xmlErrValidNode(ctxt, state->node,
  5450. XML_DTD_CONTENT_MODEL,
  5451. "Element %s content does not follow the DTD, Expecting more child\n",
  5452. state->node->name, NULL,NULL);
  5453. } else {
  5454. /*
  5455. * previous validation errors should not generate
  5456. * a new one here
  5457. */
  5458. ret = 1;
  5459. }
  5460. }
  5461. }
  5462. }
  5463. vstateVPop(ctxt);
  5464. }
  5465. return(ret);
  5466. }
  5467. #endif /* LIBXML_REGEXP_ENABLED */
  5468. /**
  5469. * xmlValidateOneElement:
  5470. * @ctxt: the validation context
  5471. * @doc: a document instance
  5472. * @elem: an element instance
  5473. *
  5474. * Try to validate a single element and it's attributes,
  5475. * basically it does the following checks as described by the
  5476. * XML-1.0 recommendation:
  5477. * - [ VC: Element Valid ]
  5478. * - [ VC: Required Attribute ]
  5479. * Then call xmlValidateOneAttribute() for each attribute present.
  5480. *
  5481. * The ID/IDREF checkings are done separately
  5482. *
  5483. * returns 1 if valid or 0 otherwise
  5484. */
  5485. int
  5486. xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
  5487. xmlNodePtr elem) {
  5488. xmlElementPtr elemDecl = NULL;
  5489. xmlElementContentPtr cont;
  5490. xmlAttributePtr attr;
  5491. xmlNodePtr child;
  5492. int ret = 1, tmp;
  5493. const xmlChar *name;
  5494. int extsubset = 0;
  5495. CHECK_DTD;
  5496. if (elem == NULL) return(0);
  5497. switch (elem->type) {
  5498. case XML_ATTRIBUTE_NODE:
  5499. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5500. "Attribute element not expected\n", NULL, NULL ,NULL);
  5501. return(0);
  5502. case XML_TEXT_NODE:
  5503. if (elem->children != NULL) {
  5504. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5505. "Text element has children !\n",
  5506. NULL,NULL,NULL);
  5507. return(0);
  5508. }
  5509. if (elem->ns != NULL) {
  5510. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5511. "Text element has namespace !\n",
  5512. NULL,NULL,NULL);
  5513. return(0);
  5514. }
  5515. if (elem->content == NULL) {
  5516. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5517. "Text element has no content !\n",
  5518. NULL,NULL,NULL);
  5519. return(0);
  5520. }
  5521. return(1);
  5522. case XML_XINCLUDE_START:
  5523. case XML_XINCLUDE_END:
  5524. return(1);
  5525. case XML_CDATA_SECTION_NODE:
  5526. case XML_ENTITY_REF_NODE:
  5527. case XML_PI_NODE:
  5528. case XML_COMMENT_NODE:
  5529. return(1);
  5530. case XML_ENTITY_NODE:
  5531. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5532. "Entity element not expected\n", NULL, NULL ,NULL);
  5533. return(0);
  5534. case XML_NOTATION_NODE:
  5535. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5536. "Notation element not expected\n", NULL, NULL ,NULL);
  5537. return(0);
  5538. case XML_DOCUMENT_NODE:
  5539. case XML_DOCUMENT_TYPE_NODE:
  5540. case XML_DOCUMENT_FRAG_NODE:
  5541. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5542. "Document element not expected\n", NULL, NULL ,NULL);
  5543. return(0);
  5544. case XML_HTML_DOCUMENT_NODE:
  5545. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5546. "HTML Document not expected\n", NULL, NULL ,NULL);
  5547. return(0);
  5548. case XML_ELEMENT_NODE:
  5549. break;
  5550. default:
  5551. xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
  5552. "unknown element type\n", NULL, NULL ,NULL);
  5553. return(0);
  5554. }
  5555. /*
  5556. * Fetch the declaration
  5557. */
  5558. elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
  5559. if (elemDecl == NULL)
  5560. return(0);
  5561. /*
  5562. * If vstateNr is not zero that means continuous validation is
  5563. * activated, do not try to check the content model at that level.
  5564. */
  5565. if (ctxt->vstateNr == 0) {
  5566. /* Check that the element content matches the definition */
  5567. switch (elemDecl->etype) {
  5568. case XML_ELEMENT_TYPE_UNDEFINED:
  5569. xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
  5570. "No declaration for element %s\n",
  5571. elem->name, NULL, NULL);
  5572. return(0);
  5573. case XML_ELEMENT_TYPE_EMPTY:
  5574. if (elem->children != NULL) {
  5575. xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
  5576. "Element %s was declared EMPTY this one has content\n",
  5577. elem->name, NULL, NULL);
  5578. ret = 0;
  5579. }
  5580. break;
  5581. case XML_ELEMENT_TYPE_ANY:
  5582. /* I don't think anything is required then */
  5583. break;
  5584. case XML_ELEMENT_TYPE_MIXED:
  5585. /* simple case of declared as #PCDATA */
  5586. if ((elemDecl->content != NULL) &&
  5587. (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
  5588. ret = xmlValidateOneCdataElement(ctxt, doc, elem);
  5589. if (!ret) {
  5590. xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
  5591. "Element %s was declared #PCDATA but contains non text nodes\n",
  5592. elem->name, NULL, NULL);
  5593. }
  5594. break;
  5595. }
  5596. child = elem->children;
  5597. /* Hum, this start to get messy */
  5598. while (child != NULL) {
  5599. if (child->type == XML_ELEMENT_NODE) {
  5600. name = child->name;
  5601. if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
  5602. xmlChar fn[50];
  5603. xmlChar *fullname;
  5604. fullname = xmlBuildQName(child->name, child->ns->prefix,
  5605. fn, 50);
  5606. if (fullname == NULL)
  5607. return(0);
  5608. cont = elemDecl->content;
  5609. while (cont != NULL) {
  5610. if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
  5611. if (xmlStrEqual(cont->name, fullname))
  5612. break;
  5613. } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
  5614. (cont->c1 != NULL) &&
  5615. (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
  5616. if (xmlStrEqual(cont->c1->name, fullname))
  5617. break;
  5618. } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
  5619. (cont->c1 == NULL) ||
  5620. (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
  5621. xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
  5622. "Internal: MIXED struct corrupted\n",
  5623. NULL);
  5624. break;
  5625. }
  5626. cont = cont->c2;
  5627. }
  5628. if ((fullname != fn) && (fullname != child->name))
  5629. xmlFree(fullname);
  5630. if (cont != NULL)
  5631. goto child_ok;
  5632. }
  5633. cont = elemDecl->content;
  5634. while (cont != NULL) {
  5635. if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
  5636. if (xmlStrEqual(cont->name, name)) break;
  5637. } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
  5638. (cont->c1 != NULL) &&
  5639. (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
  5640. if (xmlStrEqual(cont->c1->name, name)) break;
  5641. } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
  5642. (cont->c1 == NULL) ||
  5643. (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
  5644. xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
  5645. "Internal: MIXED struct corrupted\n",
  5646. NULL);
  5647. break;
  5648. }
  5649. cont = cont->c2;
  5650. }
  5651. if (cont == NULL) {
  5652. xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
  5653. "Element %s is not declared in %s list of possible children\n",
  5654. name, elem->name, NULL);
  5655. ret = 0;
  5656. }
  5657. }
  5658. child_ok:
  5659. child = child->next;
  5660. }
  5661. break;
  5662. case XML_ELEMENT_TYPE_ELEMENT:
  5663. if ((doc->standalone == 1) && (extsubset == 1)) {
  5664. /*
  5665. * VC: Standalone Document Declaration
  5666. * - element types with element content, if white space
  5667. * occurs directly within any instance of those types.
  5668. */
  5669. child = elem->children;
  5670. while (child != NULL) {
  5671. if (child->type == XML_TEXT_NODE) {
  5672. const xmlChar *content = child->content;
  5673. while (IS_BLANK_CH(*content))
  5674. content++;
  5675. if (*content == 0) {
  5676. xmlErrValidNode(ctxt, elem,
  5677. XML_DTD_STANDALONE_WHITE_SPACE,
  5678. "standalone: %s declared in the external subset contains white spaces nodes\n",
  5679. elem->name, NULL, NULL);
  5680. ret = 0;
  5681. break;
  5682. }
  5683. }
  5684. child =child->next;
  5685. }
  5686. }
  5687. child = elem->children;
  5688. cont = elemDecl->content;
  5689. tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
  5690. if (tmp <= 0)
  5691. ret = tmp;
  5692. break;
  5693. }
  5694. } /* not continuous */
  5695. /* [ VC: Required Attribute ] */
  5696. attr = elemDecl->attributes;
  5697. while (attr != NULL) {
  5698. if (attr->def == XML_ATTRIBUTE_REQUIRED) {
  5699. int qualified = -1;
  5700. if ((attr->prefix == NULL) &&
  5701. (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
  5702. xmlNsPtr ns;
  5703. ns = elem->nsDef;
  5704. while (ns != NULL) {
  5705. if (ns->prefix == NULL)
  5706. goto found;
  5707. ns = ns->next;
  5708. }
  5709. } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
  5710. xmlNsPtr ns;
  5711. ns = elem->nsDef;
  5712. while (ns != NULL) {
  5713. if (xmlStrEqual(attr->name, ns->prefix))
  5714. goto found;
  5715. ns = ns->next;
  5716. }
  5717. } else {
  5718. xmlAttrPtr attrib;
  5719. attrib = elem->properties;
  5720. while (attrib != NULL) {
  5721. if (xmlStrEqual(attrib->name, attr->name)) {
  5722. if (attr->prefix != NULL) {
  5723. xmlNsPtr nameSpace = attrib->ns;
  5724. if (nameSpace == NULL)
  5725. nameSpace = elem->ns;
  5726. /*
  5727. * qualified names handling is problematic, having a
  5728. * different prefix should be possible but DTDs don't
  5729. * allow to define the URI instead of the prefix :-(
  5730. */
  5731. if (nameSpace == NULL) {
  5732. if (qualified < 0)
  5733. qualified = 0;
  5734. } else if (!xmlStrEqual(nameSpace->prefix,
  5735. attr->prefix)) {
  5736. if (qualified < 1)
  5737. qualified = 1;
  5738. } else
  5739. goto found;
  5740. } else {
  5741. /*
  5742. * We should allow applications to define namespaces
  5743. * for their application even if the DTD doesn't
  5744. * carry one, otherwise, basically we would always
  5745. * break.
  5746. */
  5747. goto found;
  5748. }
  5749. }
  5750. attrib = attrib->next;
  5751. }
  5752. }
  5753. if (qualified == -1) {
  5754. if (attr->prefix == NULL) {
  5755. xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
  5756. "Element %s does not carry attribute %s\n",
  5757. elem->name, attr->name, NULL);
  5758. ret = 0;
  5759. } else {
  5760. xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
  5761. "Element %s does not carry attribute %s:%s\n",
  5762. elem->name, attr->prefix,attr->name);
  5763. ret = 0;
  5764. }
  5765. } else if (qualified == 0) {
  5766. xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
  5767. "Element %s required attribute %s:%s has no prefix\n",
  5768. elem->name, attr->prefix, attr->name);
  5769. } else if (qualified == 1) {
  5770. xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
  5771. "Element %s required attribute %s:%s has different prefix\n",
  5772. elem->name, attr->prefix, attr->name);
  5773. }
  5774. } else if (attr->def == XML_ATTRIBUTE_FIXED) {
  5775. /*
  5776. * Special tests checking #FIXED namespace declarations
  5777. * have the right value since this is not done as an
  5778. * attribute checking
  5779. */
  5780. if ((attr->prefix == NULL) &&
  5781. (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
  5782. xmlNsPtr ns;
  5783. ns = elem->nsDef;
  5784. while (ns != NULL) {
  5785. if (ns->prefix == NULL) {
  5786. if (!xmlStrEqual(attr->defaultValue, ns->href)) {
  5787. xmlErrValidNode(ctxt, elem,
  5788. XML_DTD_ELEM_DEFAULT_NAMESPACE,
  5789. "Element %s namespace name for default namespace does not match the DTD\n",
  5790. elem->name, NULL, NULL);
  5791. ret = 0;
  5792. }
  5793. goto found;
  5794. }
  5795. ns = ns->next;
  5796. }
  5797. } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
  5798. xmlNsPtr ns;
  5799. ns = elem->nsDef;
  5800. while (ns != NULL) {
  5801. if (xmlStrEqual(attr->name, ns->prefix)) {
  5802. if (!xmlStrEqual(attr->defaultValue, ns->href)) {
  5803. xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
  5804. "Element %s namespace name for %s does not match the DTD\n",
  5805. elem->name, ns->prefix, NULL);
  5806. ret = 0;
  5807. }
  5808. goto found;
  5809. }
  5810. ns = ns->next;
  5811. }
  5812. }
  5813. }
  5814. found:
  5815. attr = attr->nexth;
  5816. }
  5817. return(ret);
  5818. }
  5819. /**
  5820. * xmlValidateRoot:
  5821. * @ctxt: the validation context
  5822. * @doc: a document instance
  5823. *
  5824. * Try to validate a the root element
  5825. * basically it does the following check as described by the
  5826. * XML-1.0 recommendation:
  5827. * - [ VC: Root Element Type ]
  5828. * it doesn't try to recurse or apply other check to the element
  5829. *
  5830. * returns 1 if valid or 0 otherwise
  5831. */
  5832. int
  5833. xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
  5834. xmlNodePtr root;
  5835. int ret;
  5836. if (doc == NULL) return(0);
  5837. root = xmlDocGetRootElement(doc);
  5838. if ((root == NULL) || (root->name == NULL)) {
  5839. xmlErrValid(ctxt, XML_DTD_NO_ROOT,
  5840. "no root element\n", NULL);
  5841. return(0);
  5842. }
  5843. /*
  5844. * When doing post validation against a separate DTD, those may
  5845. * no internal subset has been generated
  5846. */
  5847. if ((doc->intSubset != NULL) &&
  5848. (doc->intSubset->name != NULL)) {
  5849. /*
  5850. * Check first the document root against the NQName
  5851. */
  5852. if (!xmlStrEqual(doc->intSubset->name, root->name)) {
  5853. if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
  5854. xmlChar fn[50];
  5855. xmlChar *fullname;
  5856. fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
  5857. if (fullname == NULL) {
  5858. xmlVErrMemory(ctxt, NULL);
  5859. return(0);
  5860. }
  5861. ret = xmlStrEqual(doc->intSubset->name, fullname);
  5862. if ((fullname != fn) && (fullname != root->name))
  5863. xmlFree(fullname);
  5864. if (ret == 1)
  5865. goto name_ok;
  5866. }
  5867. if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
  5868. (xmlStrEqual(root->name, BAD_CAST "html")))
  5869. goto name_ok;
  5870. xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
  5871. "root and DTD name do not match '%s' and '%s'\n",
  5872. root->name, doc->intSubset->name, NULL);
  5873. return(0);
  5874. }
  5875. }
  5876. name_ok:
  5877. return(1);
  5878. }
  5879. /**
  5880. * xmlValidateElement:
  5881. * @ctxt: the validation context
  5882. * @doc: a document instance
  5883. * @elem: an element instance
  5884. *
  5885. * Try to validate the subtree under an element
  5886. *
  5887. * returns 1 if valid or 0 otherwise
  5888. */
  5889. int
  5890. xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
  5891. xmlNodePtr child;
  5892. xmlAttrPtr attr;
  5893. xmlNsPtr ns;
  5894. const xmlChar *value;
  5895. int ret = 1;
  5896. if (elem == NULL) return(0);
  5897. /*
  5898. * XInclude elements were added after parsing in the infoset,
  5899. * they don't really mean anything validation wise.
  5900. */
  5901. if ((elem->type == XML_XINCLUDE_START) ||
  5902. (elem->type == XML_XINCLUDE_END))
  5903. return(1);
  5904. CHECK_DTD;
  5905. /*
  5906. * Entities references have to be handled separately
  5907. */
  5908. if (elem->type == XML_ENTITY_REF_NODE) {
  5909. return(1);
  5910. }
  5911. ret &= xmlValidateOneElement(ctxt, doc, elem);
  5912. if (elem->type == XML_ELEMENT_NODE) {
  5913. attr = elem->properties;
  5914. while (attr != NULL) {
  5915. value = xmlNodeListGetString(doc, attr->children, 0);
  5916. ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
  5917. if (value != NULL)
  5918. xmlFree((char *)value);
  5919. attr= attr->next;
  5920. }
  5921. ns = elem->nsDef;
  5922. while (ns != NULL) {
  5923. if (elem->ns == NULL)
  5924. ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
  5925. ns, ns->href);
  5926. else
  5927. ret &= xmlValidateOneNamespace(ctxt, doc, elem,
  5928. elem->ns->prefix, ns, ns->href);
  5929. ns = ns->next;
  5930. }
  5931. }
  5932. child = elem->children;
  5933. while (child != NULL) {
  5934. ret &= xmlValidateElement(ctxt, doc, child);
  5935. child = child->next;
  5936. }
  5937. return(ret);
  5938. }
  5939. /**
  5940. * xmlValidateRef:
  5941. * @ref: A reference to be validated
  5942. * @ctxt: Validation context
  5943. * @name: Name of ID we are searching for
  5944. *
  5945. */
  5946. static void
  5947. xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
  5948. const xmlChar *name) {
  5949. xmlAttrPtr id;
  5950. xmlAttrPtr attr;
  5951. if (ref == NULL)
  5952. return;
  5953. if ((ref->attr == NULL) && (ref->name == NULL))
  5954. return;
  5955. attr = ref->attr;
  5956. if (attr == NULL) {
  5957. xmlChar *dup, *str = NULL, *cur, save;
  5958. dup = xmlStrdup(name);
  5959. if (dup == NULL) {
  5960. ctxt->valid = 0;
  5961. return;
  5962. }
  5963. cur = dup;
  5964. while (*cur != 0) {
  5965. str = cur;
  5966. while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
  5967. save = *cur;
  5968. *cur = 0;
  5969. id = xmlGetID(ctxt->doc, str);
  5970. if (id == NULL) {
  5971. xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
  5972. "attribute %s line %d references an unknown ID \"%s\"\n",
  5973. ref->name, ref->lineno, str);
  5974. ctxt->valid = 0;
  5975. }
  5976. if (save == 0)
  5977. break;
  5978. *cur = save;
  5979. while (IS_BLANK_CH(*cur)) cur++;
  5980. }
  5981. xmlFree(dup);
  5982. } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
  5983. id = xmlGetID(ctxt->doc, name);
  5984. if (id == NULL) {
  5985. xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
  5986. "IDREF attribute %s references an unknown ID \"%s\"\n",
  5987. attr->name, name, NULL);
  5988. ctxt->valid = 0;
  5989. }
  5990. } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
  5991. xmlChar *dup, *str = NULL, *cur, save;
  5992. dup = xmlStrdup(name);
  5993. if (dup == NULL) {
  5994. xmlVErrMemory(ctxt, "IDREFS split");
  5995. ctxt->valid = 0;
  5996. return;
  5997. }
  5998. cur = dup;
  5999. while (*cur != 0) {
  6000. str = cur;
  6001. while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
  6002. save = *cur;
  6003. *cur = 0;
  6004. id = xmlGetID(ctxt->doc, str);
  6005. if (id == NULL) {
  6006. xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
  6007. "IDREFS attribute %s references an unknown ID \"%s\"\n",
  6008. attr->name, str, NULL);
  6009. ctxt->valid = 0;
  6010. }
  6011. if (save == 0)
  6012. break;
  6013. *cur = save;
  6014. while (IS_BLANK_CH(*cur)) cur++;
  6015. }
  6016. xmlFree(dup);
  6017. }
  6018. }
  6019. /**
  6020. * xmlWalkValidateList:
  6021. * @data: Contents of current link
  6022. * @user: Value supplied by the user
  6023. *
  6024. * Returns 0 to abort the walk or 1 to continue
  6025. */
  6026. static int
  6027. xmlWalkValidateList(const void *data, const void *user)
  6028. {
  6029. xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
  6030. xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
  6031. return 1;
  6032. }
  6033. /**
  6034. * xmlValidateCheckRefCallback:
  6035. * @ref_list: List of references
  6036. * @ctxt: Validation context
  6037. * @name: Name of ID we are searching for
  6038. *
  6039. */
  6040. static void
  6041. xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
  6042. const xmlChar *name) {
  6043. xmlValidateMemo memo;
  6044. if (ref_list == NULL)
  6045. return;
  6046. memo.ctxt = ctxt;
  6047. memo.name = name;
  6048. xmlListWalk(ref_list, xmlWalkValidateList, &memo);
  6049. }
  6050. /**
  6051. * xmlValidateDocumentFinal:
  6052. * @ctxt: the validation context
  6053. * @doc: a document instance
  6054. *
  6055. * Does the final step for the document validation once all the
  6056. * incremental validation steps have been completed
  6057. *
  6058. * basically it does the following checks described by the XML Rec
  6059. *
  6060. * Check all the IDREF/IDREFS attributes definition for validity
  6061. *
  6062. * returns 1 if valid or 0 otherwise
  6063. */
  6064. int
  6065. xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
  6066. xmlRefTablePtr table;
  6067. if (ctxt == NULL)
  6068. return(0);
  6069. if (doc == NULL) {
  6070. xmlErrValid(ctxt, XML_DTD_NO_DOC,
  6071. "xmlValidateDocumentFinal: doc == NULL\n", NULL);
  6072. return(0);
  6073. }
  6074. /*
  6075. * Check all the NOTATION/NOTATIONS attributes
  6076. */
  6077. /*
  6078. * Check all the ENTITY/ENTITIES attributes definition for validity
  6079. */
  6080. /*
  6081. * Check all the IDREF/IDREFS attributes definition for validity
  6082. */
  6083. table = (xmlRefTablePtr) doc->refs;
  6084. ctxt->doc = doc;
  6085. ctxt->valid = 1;
  6086. xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
  6087. return(ctxt->valid);
  6088. }
  6089. /**
  6090. * xmlValidateDtd:
  6091. * @ctxt: the validation context
  6092. * @doc: a document instance
  6093. * @dtd: a dtd instance
  6094. *
  6095. * Try to validate the document against the dtd instance
  6096. *
  6097. * Basically it does check all the definitions in the DtD.
  6098. * Note the the internal subset (if present) is de-coupled
  6099. * (i.e. not used), which could give problems if ID or IDREF
  6100. * is present.
  6101. *
  6102. * returns 1 if valid or 0 otherwise
  6103. */
  6104. int
  6105. xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
  6106. int ret;
  6107. xmlDtdPtr oldExt, oldInt;
  6108. xmlNodePtr root;
  6109. if (dtd == NULL) return(0);
  6110. if (doc == NULL) return(0);
  6111. oldExt = doc->extSubset;
  6112. oldInt = doc->intSubset;
  6113. doc->extSubset = dtd;
  6114. doc->intSubset = NULL;
  6115. ret = xmlValidateRoot(ctxt, doc);
  6116. if (ret == 0) {
  6117. doc->extSubset = oldExt;
  6118. doc->intSubset = oldInt;
  6119. return(ret);
  6120. }
  6121. if (doc->ids != NULL) {
  6122. xmlFreeIDTable(doc->ids);
  6123. doc->ids = NULL;
  6124. }
  6125. if (doc->refs != NULL) {
  6126. xmlFreeRefTable(doc->refs);
  6127. doc->refs = NULL;
  6128. }
  6129. root = xmlDocGetRootElement(doc);
  6130. ret = xmlValidateElement(ctxt, doc, root);
  6131. ret &= xmlValidateDocumentFinal(ctxt, doc);
  6132. doc->extSubset = oldExt;
  6133. doc->intSubset = oldInt;
  6134. return(ret);
  6135. }
  6136. static void
  6137. xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
  6138. const xmlChar *name ATTRIBUTE_UNUSED) {
  6139. if (cur == NULL)
  6140. return;
  6141. if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
  6142. xmlChar *notation = cur->content;
  6143. if (notation != NULL) {
  6144. int ret;
  6145. ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
  6146. if (ret != 1) {
  6147. ctxt->valid = 0;
  6148. }
  6149. }
  6150. }
  6151. }
  6152. static void
  6153. xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
  6154. const xmlChar *name ATTRIBUTE_UNUSED) {
  6155. int ret;
  6156. xmlDocPtr doc;
  6157. xmlElementPtr elem = NULL;
  6158. if (cur == NULL)
  6159. return;
  6160. switch (cur->atype) {
  6161. case XML_ATTRIBUTE_CDATA:
  6162. case XML_ATTRIBUTE_ID:
  6163. case XML_ATTRIBUTE_IDREF :
  6164. case XML_ATTRIBUTE_IDREFS:
  6165. case XML_ATTRIBUTE_NMTOKEN:
  6166. case XML_ATTRIBUTE_NMTOKENS:
  6167. case XML_ATTRIBUTE_ENUMERATION:
  6168. break;
  6169. case XML_ATTRIBUTE_ENTITY:
  6170. case XML_ATTRIBUTE_ENTITIES:
  6171. case XML_ATTRIBUTE_NOTATION:
  6172. if (cur->defaultValue != NULL) {
  6173. ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
  6174. cur->atype, cur->defaultValue);
  6175. if ((ret == 0) && (ctxt->valid == 1))
  6176. ctxt->valid = 0;
  6177. }
  6178. if (cur->tree != NULL) {
  6179. xmlEnumerationPtr tree = cur->tree;
  6180. while (tree != NULL) {
  6181. ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
  6182. cur->name, cur->atype, tree->name);
  6183. if ((ret == 0) && (ctxt->valid == 1))
  6184. ctxt->valid = 0;
  6185. tree = tree->next;
  6186. }
  6187. }
  6188. }
  6189. if (cur->atype == XML_ATTRIBUTE_NOTATION) {
  6190. doc = cur->doc;
  6191. if (cur->elem == NULL) {
  6192. xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
  6193. "xmlValidateAttributeCallback(%s): internal error\n",
  6194. (const char *) cur->name);
  6195. return;
  6196. }
  6197. if (doc != NULL)
  6198. elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
  6199. if ((elem == NULL) && (doc != NULL))
  6200. elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
  6201. if ((elem == NULL) && (cur->parent != NULL) &&
  6202. (cur->parent->type == XML_DTD_NODE))
  6203. elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
  6204. if (elem == NULL) {
  6205. xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
  6206. "attribute %s: could not find decl for element %s\n",
  6207. cur->name, cur->elem, NULL);
  6208. return;
  6209. }
  6210. if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
  6211. xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
  6212. "NOTATION attribute %s declared for EMPTY element %s\n",
  6213. cur->name, cur->elem, NULL);
  6214. ctxt->valid = 0;
  6215. }
  6216. }
  6217. }
  6218. /**
  6219. * xmlValidateDtdFinal:
  6220. * @ctxt: the validation context
  6221. * @doc: a document instance
  6222. *
  6223. * Does the final step for the dtds validation once all the
  6224. * subsets have been parsed
  6225. *
  6226. * basically it does the following checks described by the XML Rec
  6227. * - check that ENTITY and ENTITIES type attributes default or
  6228. * possible values matches one of the defined entities.
  6229. * - check that NOTATION type attributes default or
  6230. * possible values matches one of the defined notations.
  6231. *
  6232. * returns 1 if valid or 0 if invalid and -1 if not well-formed
  6233. */
  6234. int
  6235. xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
  6236. xmlDtdPtr dtd;
  6237. xmlAttributeTablePtr table;
  6238. xmlEntitiesTablePtr entities;
  6239. if ((doc == NULL) || (ctxt == NULL)) return(0);
  6240. if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
  6241. return(0);
  6242. ctxt->doc = doc;
  6243. ctxt->valid = 1;
  6244. dtd = doc->intSubset;
  6245. if ((dtd != NULL) && (dtd->attributes != NULL)) {
  6246. table = (xmlAttributeTablePtr) dtd->attributes;
  6247. xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
  6248. }
  6249. if ((dtd != NULL) && (dtd->entities != NULL)) {
  6250. entities = (xmlEntitiesTablePtr) dtd->entities;
  6251. xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
  6252. ctxt);
  6253. }
  6254. dtd = doc->extSubset;
  6255. if ((dtd != NULL) && (dtd->attributes != NULL)) {
  6256. table = (xmlAttributeTablePtr) dtd->attributes;
  6257. xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
  6258. }
  6259. if ((dtd != NULL) && (dtd->entities != NULL)) {
  6260. entities = (xmlEntitiesTablePtr) dtd->entities;
  6261. xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
  6262. ctxt);
  6263. }
  6264. return(ctxt->valid);
  6265. }
  6266. /**
  6267. * xmlValidateDocument:
  6268. * @ctxt: the validation context
  6269. * @doc: a document instance
  6270. *
  6271. * Try to validate the document instance
  6272. *
  6273. * basically it does the all the checks described by the XML Rec
  6274. * i.e. validates the internal and external subset (if present)
  6275. * and validate the document tree.
  6276. *
  6277. * returns 1 if valid or 0 otherwise
  6278. */
  6279. int
  6280. xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
  6281. int ret;
  6282. xmlNodePtr root;
  6283. if (doc == NULL)
  6284. return(0);
  6285. if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
  6286. xmlErrValid(ctxt, XML_DTD_NO_DTD,
  6287. "no DTD found!\n", NULL);
  6288. return(0);
  6289. }
  6290. if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
  6291. (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
  6292. xmlChar *sysID;
  6293. if (doc->intSubset->SystemID != NULL) {
  6294. sysID = xmlBuildURI(doc->intSubset->SystemID,
  6295. doc->URL);
  6296. if (sysID == NULL) {
  6297. xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
  6298. "Could not build URI for external subset \"%s\"\n",
  6299. (const char *) doc->intSubset->SystemID);
  6300. return 0;
  6301. }
  6302. } else
  6303. sysID = NULL;
  6304. doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
  6305. (const xmlChar *)sysID);
  6306. if (sysID != NULL)
  6307. xmlFree(sysID);
  6308. if (doc->extSubset == NULL) {
  6309. if (doc->intSubset->SystemID != NULL) {
  6310. xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
  6311. "Could not load the external subset \"%s\"\n",
  6312. (const char *) doc->intSubset->SystemID);
  6313. } else {
  6314. xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
  6315. "Could not load the external subset \"%s\"\n",
  6316. (const char *) doc->intSubset->ExternalID);
  6317. }
  6318. return(0);
  6319. }
  6320. }
  6321. if (doc->ids != NULL) {
  6322. xmlFreeIDTable(doc->ids);
  6323. doc->ids = NULL;
  6324. }
  6325. if (doc->refs != NULL) {
  6326. xmlFreeRefTable(doc->refs);
  6327. doc->refs = NULL;
  6328. }
  6329. ret = xmlValidateDtdFinal(ctxt, doc);
  6330. if (!xmlValidateRoot(ctxt, doc)) return(0);
  6331. root = xmlDocGetRootElement(doc);
  6332. ret &= xmlValidateElement(ctxt, doc, root);
  6333. ret &= xmlValidateDocumentFinal(ctxt, doc);
  6334. return(ret);
  6335. }
  6336. /************************************************************************
  6337. * *
  6338. * Routines for dynamic validation editing *
  6339. * *
  6340. ************************************************************************/
  6341. /**
  6342. * xmlValidGetPotentialChildren:
  6343. * @ctree: an element content tree
  6344. * @names: an array to store the list of child names
  6345. * @len: a pointer to the number of element in the list
  6346. * @max: the size of the array
  6347. *
  6348. * Build/extend a list of potential children allowed by the content tree
  6349. *
  6350. * returns the number of element in the list, or -1 in case of error.
  6351. */
  6352. int
  6353. xmlValidGetPotentialChildren(xmlElementContent *ctree,
  6354. const xmlChar **names,
  6355. int *len, int max) {
  6356. int i;
  6357. if ((ctree == NULL) || (names == NULL) || (len == NULL))
  6358. return(-1);
  6359. if (*len >= max) return(*len);
  6360. switch (ctree->type) {
  6361. case XML_ELEMENT_CONTENT_PCDATA:
  6362. for (i = 0; i < *len;i++)
  6363. if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len);
  6364. names[(*len)++] = BAD_CAST "#PCDATA";
  6365. break;
  6366. case XML_ELEMENT_CONTENT_ELEMENT:
  6367. for (i = 0; i < *len;i++)
  6368. if (xmlStrEqual(ctree->name, names[i])) return(*len);
  6369. names[(*len)++] = ctree->name;
  6370. break;
  6371. case XML_ELEMENT_CONTENT_SEQ:
  6372. xmlValidGetPotentialChildren(ctree->c1, names, len, max);
  6373. xmlValidGetPotentialChildren(ctree->c2, names, len, max);
  6374. break;
  6375. case XML_ELEMENT_CONTENT_OR:
  6376. xmlValidGetPotentialChildren(ctree->c1, names, len, max);
  6377. xmlValidGetPotentialChildren(ctree->c2, names, len, max);
  6378. break;
  6379. }
  6380. return(*len);
  6381. }
  6382. /*
  6383. * Dummy function to suppress messages while we try out valid elements
  6384. */
  6385. static void XMLCDECL xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
  6386. const char *msg ATTRIBUTE_UNUSED, ...) {
  6387. return;
  6388. }
  6389. /**
  6390. * xmlValidGetValidElements:
  6391. * @prev: an element to insert after
  6392. * @next: an element to insert next
  6393. * @names: an array to store the list of child names
  6394. * @max: the size of the array
  6395. *
  6396. * This function returns the list of authorized children to insert
  6397. * within an existing tree while respecting the validity constraints
  6398. * forced by the Dtd. The insertion point is defined using @prev and
  6399. * @next in the following ways:
  6400. * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
  6401. * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
  6402. * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
  6403. * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
  6404. * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
  6405. *
  6406. * pointers to the element names are inserted at the beginning of the array
  6407. * and do not need to be freed.
  6408. *
  6409. * returns the number of element in the list, or -1 in case of error. If
  6410. * the function returns the value @max the caller is invited to grow the
  6411. * receiving array and retry.
  6412. */
  6413. int
  6414. xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
  6415. int max) {
  6416. xmlValidCtxt vctxt;
  6417. int nb_valid_elements = 0;
  6418. const xmlChar *elements[256];
  6419. int nb_elements = 0, i;
  6420. const xmlChar *name;
  6421. xmlNode *ref_node;
  6422. xmlNode *parent;
  6423. xmlNode *test_node;
  6424. xmlNode *prev_next;
  6425. xmlNode *next_prev;
  6426. xmlNode *parent_childs;
  6427. xmlNode *parent_last;
  6428. xmlElement *element_desc;
  6429. if (prev == NULL && next == NULL)
  6430. return(-1);
  6431. if (names == NULL) return(-1);
  6432. if (max <= 0) return(-1);
  6433. memset(&vctxt, 0, sizeof (xmlValidCtxt));
  6434. vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
  6435. nb_valid_elements = 0;
  6436. ref_node = prev ? prev : next;
  6437. parent = ref_node->parent;
  6438. /*
  6439. * Retrieves the parent element declaration
  6440. */
  6441. element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
  6442. parent->name);
  6443. if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
  6444. element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
  6445. parent->name);
  6446. if (element_desc == NULL) return(-1);
  6447. /*
  6448. * Do a backup of the current tree structure
  6449. */
  6450. prev_next = prev ? prev->next : NULL;
  6451. next_prev = next ? next->prev : NULL;
  6452. parent_childs = parent->children;
  6453. parent_last = parent->last;
  6454. /*
  6455. * Creates a dummy node and insert it into the tree
  6456. */
  6457. test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
  6458. test_node->parent = parent;
  6459. test_node->prev = prev;
  6460. test_node->next = next;
  6461. name = test_node->name;
  6462. if (prev) prev->next = test_node;
  6463. else parent->children = test_node;
  6464. if (next) next->prev = test_node;
  6465. else parent->last = test_node;
  6466. /*
  6467. * Insert each potential child node and check if the parent is
  6468. * still valid
  6469. */
  6470. nb_elements = xmlValidGetPotentialChildren(element_desc->content,
  6471. elements, &nb_elements, 256);
  6472. for (i = 0;i < nb_elements;i++) {
  6473. test_node->name = elements[i];
  6474. if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
  6475. int j;
  6476. for (j = 0; j < nb_valid_elements;j++)
  6477. if (xmlStrEqual(elements[i], names[j])) break;
  6478. names[nb_valid_elements++] = elements[i];
  6479. if (nb_valid_elements >= max) break;
  6480. }
  6481. }
  6482. /*
  6483. * Restore the tree structure
  6484. */
  6485. if (prev) prev->next = prev_next;
  6486. if (next) next->prev = next_prev;
  6487. parent->children = parent_childs;
  6488. parent->last = parent_last;
  6489. /*
  6490. * Free up the dummy node
  6491. */
  6492. test_node->name = name;
  6493. xmlFreeNode(test_node);
  6494. return(nb_valid_elements);
  6495. }
  6496. #endif /* LIBXML_VALID_ENABLED */
  6497. #define bottom_valid
  6498. #include "elfgcchack.h"