valid.c 191 KB

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