valid.c 192 KB

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