parser.c 422 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157
  1. /*
  2. * parser.c : an XML 1.0 parser, namespaces and validity support are mostly
  3. * implemented on top of the SAX interfaces
  4. *
  5. * References:
  6. * The XML specification:
  7. * http://www.w3.org/TR/REC-xml
  8. * Original 1.0 version:
  9. * http://www.w3.org/TR/1998/REC-xml-19980210
  10. * XML second edition working draft
  11. * http://www.w3.org/TR/2000/WD-xml-2e-20000814
  12. *
  13. * Okay this is a big file, the parser core is around 7000 lines, then it
  14. * is followed by the progressive parser top routines, then the various
  15. * high level APIs to call the parser and a few miscellaneous functions.
  16. * A number of helper functions and deprecated ones have been moved to
  17. * parserInternals.c to reduce this file size.
  18. * As much as possible the functions are associated with their relative
  19. * production in the XML specification. A few productions defining the
  20. * different ranges of character are actually implanted either in
  21. * parserInternals.h or parserInternals.c
  22. * The DOM tree build is realized from the default SAX callbacks in
  23. * the module SAX.c.
  24. * The routines doing the validation checks are in valid.c and called either
  25. * from the SAX callbacks or as standalone functions using a preparsed
  26. * document.
  27. *
  28. * See Copyright for the status of this software.
  29. *
  30. * daniel@veillard.com
  31. */
  32. /* To avoid EBCDIC trouble when parsing on zOS */
  33. #if defined(__MVS__)
  34. #pragma convert("ISO8859-1")
  35. #endif
  36. #define IN_LIBXML
  37. #include "libxml.h"
  38. #if defined(_WIN32)
  39. #define XML_DIR_SEP '\\'
  40. #else
  41. #define XML_DIR_SEP '/'
  42. #endif
  43. #include <stdlib.h>
  44. #include <limits.h>
  45. #include <string.h>
  46. #include <stdarg.h>
  47. #include <stddef.h>
  48. #include <ctype.h>
  49. #include <stdlib.h>
  50. #include <libxml/parser.h>
  51. #include <libxml/xmlmemory.h>
  52. #include <libxml/tree.h>
  53. #include <libxml/parserInternals.h>
  54. #include <libxml/valid.h>
  55. #include <libxml/entities.h>
  56. #include <libxml/xmlerror.h>
  57. #include <libxml/encoding.h>
  58. #include <libxml/xmlIO.h>
  59. #include <libxml/uri.h>
  60. #include <libxml/SAX2.h>
  61. #ifdef LIBXML_CATALOG_ENABLED
  62. #include <libxml/catalog.h>
  63. #endif
  64. #include "private/buf.h"
  65. #include "private/dict.h"
  66. #include "private/entities.h"
  67. #include "private/error.h"
  68. #include "private/html.h"
  69. #include "private/io.h"
  70. #include "private/parser.h"
  71. #define NS_INDEX_EMPTY INT_MAX
  72. #define NS_INDEX_XML (INT_MAX - 1)
  73. #define URI_HASH_EMPTY 0xD943A04E
  74. #define URI_HASH_XML 0xF0451F02
  75. struct _xmlStartTag {
  76. const xmlChar *prefix;
  77. const xmlChar *URI;
  78. int line;
  79. int nsNr;
  80. };
  81. typedef struct {
  82. void *saxData;
  83. unsigned prefixHashValue;
  84. unsigned uriHashValue;
  85. unsigned elementId;
  86. int oldIndex;
  87. } xmlParserNsExtra;
  88. typedef struct {
  89. unsigned hashValue;
  90. int index;
  91. } xmlParserNsBucket;
  92. struct _xmlParserNsData {
  93. xmlParserNsExtra *extra;
  94. unsigned hashSize;
  95. unsigned hashElems;
  96. xmlParserNsBucket *hash;
  97. unsigned elementId;
  98. int defaultNsIndex;
  99. };
  100. struct _xmlAttrHashBucket {
  101. int index;
  102. };
  103. static xmlParserCtxtPtr
  104. xmlCreateEntityParserCtxtInternal(xmlSAXHandlerPtr sax, void *userData,
  105. const xmlChar *URL, const xmlChar *ID, const xmlChar *base,
  106. xmlParserCtxtPtr pctx);
  107. static int
  108. xmlParseElementStart(xmlParserCtxtPtr ctxt);
  109. static void
  110. xmlParseElementEnd(xmlParserCtxtPtr ctxt);
  111. /************************************************************************
  112. * *
  113. * Arbitrary limits set in the parser. See XML_PARSE_HUGE *
  114. * *
  115. ************************************************************************/
  116. #define XML_PARSER_BIG_ENTITY 1000
  117. #define XML_PARSER_LOT_ENTITY 5000
  118. /*
  119. * Constants for protection against abusive entity expansion
  120. * ("billion laughs").
  121. */
  122. /*
  123. * A certain amount of entity expansion which is always allowed.
  124. */
  125. #define XML_PARSER_ALLOWED_EXPANSION 1000000
  126. /*
  127. * Fixed cost for each entity reference. This crudely models processing time
  128. * as well to protect, for example, against exponential expansion of empty
  129. * or very short entities.
  130. */
  131. #define XML_ENT_FIXED_COST 20
  132. /**
  133. * xmlParserMaxDepth:
  134. *
  135. * arbitrary depth limit for the XML documents that we allow to
  136. * process. This is not a limitation of the parser but a safety
  137. * boundary feature. It can be disabled with the XML_PARSE_HUGE
  138. * parser option.
  139. */
  140. unsigned int xmlParserMaxDepth = 256;
  141. #define XML_PARSER_BIG_BUFFER_SIZE 300
  142. #define XML_PARSER_BUFFER_SIZE 100
  143. #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document"
  144. /**
  145. * XML_PARSER_CHUNK_SIZE
  146. *
  147. * When calling GROW that's the minimal amount of data
  148. * the parser expected to have received. It is not a hard
  149. * limit but an optimization when reading strings like Names
  150. * It is not strictly needed as long as inputs available characters
  151. * are followed by 0, which should be provided by the I/O level
  152. */
  153. #define XML_PARSER_CHUNK_SIZE 100
  154. /**
  155. * xmlParserVersion:
  156. *
  157. * Constant string describing the internal version of the library
  158. */
  159. const char *const
  160. xmlParserVersion = LIBXML_VERSION_STRING LIBXML_VERSION_EXTRA;
  161. /*
  162. * List of XML prefixed PI allowed by W3C specs
  163. */
  164. static const char* const xmlW3CPIs[] = {
  165. "xml-stylesheet",
  166. "xml-model",
  167. NULL
  168. };
  169. /* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */
  170. static xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt,
  171. const xmlChar **str);
  172. static xmlParserErrors
  173. xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
  174. xmlSAXHandlerPtr sax,
  175. void *user_data, int depth, const xmlChar *URL,
  176. const xmlChar *ID, xmlNodePtr *list);
  177. static int
  178. xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options);
  179. #ifdef LIBXML_LEGACY_ENABLED
  180. static void
  181. xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
  182. xmlNodePtr lastNode);
  183. #endif /* LIBXML_LEGACY_ENABLED */
  184. static xmlParserErrors
  185. xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
  186. const xmlChar *string, void *user_data, xmlNodePtr *lst);
  187. static int
  188. xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity);
  189. /************************************************************************
  190. * *
  191. * Some factorized error routines *
  192. * *
  193. ************************************************************************/
  194. /**
  195. * xmlErrAttributeDup:
  196. * @ctxt: an XML parser context
  197. * @prefix: the attribute prefix
  198. * @localname: the attribute localname
  199. *
  200. * Handle a redefinition of attribute error
  201. */
  202. static void
  203. xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix,
  204. const xmlChar * localname)
  205. {
  206. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  207. (ctxt->instate == XML_PARSER_EOF))
  208. return;
  209. if (ctxt != NULL)
  210. ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
  211. if (prefix == NULL)
  212. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
  213. XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0,
  214. (const char *) localname, NULL, NULL, 0, 0,
  215. "Attribute %s redefined\n", localname);
  216. else
  217. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
  218. XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0,
  219. (const char *) prefix, (const char *) localname,
  220. NULL, 0, 0, "Attribute %s:%s redefined\n", prefix,
  221. localname);
  222. if (ctxt != NULL) {
  223. ctxt->wellFormed = 0;
  224. if (ctxt->recovery == 0)
  225. ctxt->disableSAX = 1;
  226. }
  227. }
  228. /**
  229. * xmlFatalErrMsg:
  230. * @ctxt: an XML parser context
  231. * @error: the error number
  232. * @msg: the error message
  233. *
  234. * Handle a fatal parser error, i.e. violating Well-Formedness constraints
  235. */
  236. static void LIBXML_ATTR_FORMAT(3,0)
  237. xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  238. const char *msg)
  239. {
  240. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  241. (ctxt->instate == XML_PARSER_EOF))
  242. return;
  243. if (ctxt != NULL)
  244. ctxt->errNo = error;
  245. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
  246. XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg);
  247. if (ctxt != NULL) {
  248. ctxt->wellFormed = 0;
  249. if (ctxt->recovery == 0)
  250. ctxt->disableSAX = 1;
  251. }
  252. }
  253. /**
  254. * xmlWarningMsg:
  255. * @ctxt: an XML parser context
  256. * @error: the error number
  257. * @msg: the error message
  258. * @str1: extra data
  259. * @str2: extra data
  260. *
  261. * Handle a warning.
  262. */
  263. void LIBXML_ATTR_FORMAT(3,0)
  264. xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  265. const char *msg, const xmlChar *str1, const xmlChar *str2)
  266. {
  267. xmlStructuredErrorFunc schannel = NULL;
  268. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  269. (ctxt->instate == XML_PARSER_EOF))
  270. return;
  271. if ((ctxt != NULL) && (ctxt->sax != NULL) &&
  272. (ctxt->sax->initialized == XML_SAX2_MAGIC))
  273. schannel = ctxt->sax->serror;
  274. if (ctxt != NULL) {
  275. __xmlRaiseError(schannel,
  276. (ctxt->sax) ? ctxt->sax->warning : NULL,
  277. ctxt->userData,
  278. ctxt, NULL, XML_FROM_PARSER, error,
  279. XML_ERR_WARNING, NULL, 0,
  280. (const char *) str1, (const char *) str2, NULL, 0, 0,
  281. msg, (const char *) str1, (const char *) str2);
  282. } else {
  283. __xmlRaiseError(schannel, NULL, NULL,
  284. ctxt, NULL, XML_FROM_PARSER, error,
  285. XML_ERR_WARNING, NULL, 0,
  286. (const char *) str1, (const char *) str2, NULL, 0, 0,
  287. msg, (const char *) str1, (const char *) str2);
  288. }
  289. }
  290. /**
  291. * xmlValidityError:
  292. * @ctxt: an XML parser context
  293. * @error: the error number
  294. * @msg: the error message
  295. * @str1: extra data
  296. *
  297. * Handle a validity error.
  298. */
  299. static void LIBXML_ATTR_FORMAT(3,0)
  300. xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  301. const char *msg, const xmlChar *str1, const xmlChar *str2)
  302. {
  303. xmlStructuredErrorFunc schannel = NULL;
  304. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  305. (ctxt->instate == XML_PARSER_EOF))
  306. return;
  307. if (ctxt != NULL) {
  308. ctxt->errNo = error;
  309. if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
  310. schannel = ctxt->sax->serror;
  311. }
  312. if (ctxt != NULL) {
  313. __xmlRaiseError(schannel,
  314. ctxt->vctxt.error, ctxt->vctxt.userData,
  315. ctxt, NULL, XML_FROM_DTD, error,
  316. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  317. (const char *) str2, NULL, 0, 0,
  318. msg, (const char *) str1, (const char *) str2);
  319. ctxt->valid = 0;
  320. } else {
  321. __xmlRaiseError(schannel, NULL, NULL,
  322. ctxt, NULL, XML_FROM_DTD, error,
  323. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  324. (const char *) str2, NULL, 0, 0,
  325. msg, (const char *) str1, (const char *) str2);
  326. }
  327. }
  328. /**
  329. * xmlFatalErrMsgInt:
  330. * @ctxt: an XML parser context
  331. * @error: the error number
  332. * @msg: the error message
  333. * @val: an integer value
  334. *
  335. * Handle a fatal parser error, i.e. violating Well-Formedness constraints
  336. */
  337. static void LIBXML_ATTR_FORMAT(3,0)
  338. xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  339. const char *msg, int val)
  340. {
  341. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  342. (ctxt->instate == XML_PARSER_EOF))
  343. return;
  344. if (ctxt != NULL)
  345. ctxt->errNo = error;
  346. __xmlRaiseError(NULL, NULL, NULL,
  347. ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
  348. NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
  349. if (ctxt != NULL) {
  350. ctxt->wellFormed = 0;
  351. if (ctxt->recovery == 0)
  352. ctxt->disableSAX = 1;
  353. }
  354. }
  355. /**
  356. * xmlFatalErrMsgStrIntStr:
  357. * @ctxt: an XML parser context
  358. * @error: the error number
  359. * @msg: the error message
  360. * @str1: an string info
  361. * @val: an integer value
  362. * @str2: an string info
  363. *
  364. * Handle a fatal parser error, i.e. violating Well-Formedness constraints
  365. */
  366. static void LIBXML_ATTR_FORMAT(3,0)
  367. xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  368. const char *msg, const xmlChar *str1, int val,
  369. const xmlChar *str2)
  370. {
  371. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  372. (ctxt->instate == XML_PARSER_EOF))
  373. return;
  374. if (ctxt != NULL)
  375. ctxt->errNo = error;
  376. __xmlRaiseError(NULL, NULL, NULL,
  377. ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
  378. NULL, 0, (const char *) str1, (const char *) str2,
  379. NULL, val, 0, msg, str1, val, str2);
  380. if (ctxt != NULL) {
  381. ctxt->wellFormed = 0;
  382. if (ctxt->recovery == 0)
  383. ctxt->disableSAX = 1;
  384. }
  385. }
  386. /**
  387. * xmlFatalErrMsgStr:
  388. * @ctxt: an XML parser context
  389. * @error: the error number
  390. * @msg: the error message
  391. * @val: a string value
  392. *
  393. * Handle a fatal parser error, i.e. violating Well-Formedness constraints
  394. */
  395. static void LIBXML_ATTR_FORMAT(3,0)
  396. xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  397. const char *msg, const xmlChar * val)
  398. {
  399. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  400. (ctxt->instate == XML_PARSER_EOF))
  401. return;
  402. if (ctxt != NULL)
  403. ctxt->errNo = error;
  404. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
  405. XML_FROM_PARSER, error, XML_ERR_FATAL,
  406. NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
  407. val);
  408. if (ctxt != NULL) {
  409. ctxt->wellFormed = 0;
  410. if (ctxt->recovery == 0)
  411. ctxt->disableSAX = 1;
  412. }
  413. }
  414. /**
  415. * xmlErrMsgStr:
  416. * @ctxt: an XML parser context
  417. * @error: the error number
  418. * @msg: the error message
  419. * @val: a string value
  420. *
  421. * Handle a non fatal parser error
  422. */
  423. static void LIBXML_ATTR_FORMAT(3,0)
  424. xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  425. const char *msg, const xmlChar * val)
  426. {
  427. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  428. (ctxt->instate == XML_PARSER_EOF))
  429. return;
  430. if (ctxt != NULL)
  431. ctxt->errNo = error;
  432. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
  433. XML_FROM_PARSER, error, XML_ERR_ERROR,
  434. NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
  435. val);
  436. }
  437. /**
  438. * xmlNsErr:
  439. * @ctxt: an XML parser context
  440. * @error: the error number
  441. * @msg: the message
  442. * @info1: extra information string
  443. * @info2: extra information string
  444. *
  445. * Handle a fatal parser error, i.e. violating Well-Formedness constraints
  446. */
  447. static void LIBXML_ATTR_FORMAT(3,0)
  448. xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  449. const char *msg,
  450. const xmlChar * info1, const xmlChar * info2,
  451. const xmlChar * info3)
  452. {
  453. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  454. (ctxt->instate == XML_PARSER_EOF))
  455. return;
  456. if (ctxt != NULL)
  457. ctxt->errNo = error;
  458. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
  459. XML_ERR_ERROR, NULL, 0, (const char *) info1,
  460. (const char *) info2, (const char *) info3, 0, 0, msg,
  461. info1, info2, info3);
  462. if (ctxt != NULL)
  463. ctxt->nsWellFormed = 0;
  464. }
  465. /**
  466. * xmlNsWarn
  467. * @ctxt: an XML parser context
  468. * @error: the error number
  469. * @msg: the message
  470. * @info1: extra information string
  471. * @info2: extra information string
  472. *
  473. * Handle a namespace warning error
  474. */
  475. static void LIBXML_ATTR_FORMAT(3,0)
  476. xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  477. const char *msg,
  478. const xmlChar * info1, const xmlChar * info2,
  479. const xmlChar * info3)
  480. {
  481. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  482. (ctxt->instate == XML_PARSER_EOF))
  483. return;
  484. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
  485. XML_ERR_WARNING, NULL, 0, (const char *) info1,
  486. (const char *) info2, (const char *) info3, 0, 0, msg,
  487. info1, info2, info3);
  488. }
  489. static void
  490. xmlSaturatedAdd(unsigned long *dst, unsigned long val) {
  491. if (val > ULONG_MAX - *dst)
  492. *dst = ULONG_MAX;
  493. else
  494. *dst += val;
  495. }
  496. static void
  497. xmlSaturatedAddSizeT(unsigned long *dst, unsigned long val) {
  498. if (val > ULONG_MAX - *dst)
  499. *dst = ULONG_MAX;
  500. else
  501. *dst += val;
  502. }
  503. /**
  504. * xmlParserEntityCheck:
  505. * @ctxt: parser context
  506. * @extra: sum of unexpanded entity sizes
  507. *
  508. * Check for non-linear entity expansion behaviour.
  509. *
  510. * In some cases like xmlStringDecodeEntities, this function is called
  511. * for each, possibly nested entity and its unexpanded content length.
  512. *
  513. * In other cases like xmlParseReference, it's only called for each
  514. * top-level entity with its unexpanded content length plus the sum of
  515. * the unexpanded content lengths (plus fixed cost) of all nested
  516. * entities.
  517. *
  518. * Summing the unexpanded lengths also adds the length of the reference.
  519. * This is by design. Taking the length of the entity name into account
  520. * discourages attacks that try to waste CPU time with abusively long
  521. * entity names. See test/recurse/lol6.xml for example. Each call also
  522. * adds some fixed cost XML_ENT_FIXED_COST to discourage attacks with
  523. * short entities.
  524. *
  525. * Returns 1 on error, 0 on success.
  526. */
  527. static int
  528. xmlParserEntityCheck(xmlParserCtxtPtr ctxt, unsigned long extra)
  529. {
  530. unsigned long consumed;
  531. xmlParserInputPtr input = ctxt->input;
  532. xmlEntityPtr entity = input->entity;
  533. /*
  534. * Compute total consumed bytes so far, including input streams of
  535. * external entities.
  536. */
  537. consumed = input->parentConsumed;
  538. if ((entity == NULL) ||
  539. ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
  540. ((entity->flags & XML_ENT_PARSED) == 0))) {
  541. xmlSaturatedAdd(&consumed, input->consumed);
  542. xmlSaturatedAddSizeT(&consumed, input->cur - input->base);
  543. }
  544. xmlSaturatedAdd(&consumed, ctxt->sizeentities);
  545. /*
  546. * Add extra cost and some fixed cost.
  547. */
  548. xmlSaturatedAdd(&ctxt->sizeentcopy, extra);
  549. xmlSaturatedAdd(&ctxt->sizeentcopy, XML_ENT_FIXED_COST);
  550. /*
  551. * It's important to always use saturation arithmetic when tracking
  552. * entity sizes to make the size checks reliable. If "sizeentcopy"
  553. * overflows, we have to abort.
  554. */
  555. if ((ctxt->sizeentcopy > XML_PARSER_ALLOWED_EXPANSION) &&
  556. ((ctxt->sizeentcopy >= ULONG_MAX) ||
  557. (ctxt->sizeentcopy / ctxt->maxAmpl > consumed))) {
  558. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_LOOP,
  559. "Maximum entity amplification factor exceeded, see "
  560. "xmlCtxtSetMaxAmplification.\n");
  561. xmlHaltParser(ctxt);
  562. return(1);
  563. }
  564. return(0);
  565. }
  566. /************************************************************************
  567. * *
  568. * Library wide options *
  569. * *
  570. ************************************************************************/
  571. /**
  572. * xmlHasFeature:
  573. * @feature: the feature to be examined
  574. *
  575. * Examines if the library has been compiled with a given feature.
  576. *
  577. * Returns a non-zero value if the feature exist, otherwise zero.
  578. * Returns zero (0) if the feature does not exist or an unknown
  579. * unknown feature is requested, non-zero otherwise.
  580. */
  581. int
  582. xmlHasFeature(xmlFeature feature)
  583. {
  584. switch (feature) {
  585. case XML_WITH_THREAD:
  586. #ifdef LIBXML_THREAD_ENABLED
  587. return(1);
  588. #else
  589. return(0);
  590. #endif
  591. case XML_WITH_TREE:
  592. #ifdef LIBXML_TREE_ENABLED
  593. return(1);
  594. #else
  595. return(0);
  596. #endif
  597. case XML_WITH_OUTPUT:
  598. #ifdef LIBXML_OUTPUT_ENABLED
  599. return(1);
  600. #else
  601. return(0);
  602. #endif
  603. case XML_WITH_PUSH:
  604. #ifdef LIBXML_PUSH_ENABLED
  605. return(1);
  606. #else
  607. return(0);
  608. #endif
  609. case XML_WITH_READER:
  610. #ifdef LIBXML_READER_ENABLED
  611. return(1);
  612. #else
  613. return(0);
  614. #endif
  615. case XML_WITH_PATTERN:
  616. #ifdef LIBXML_PATTERN_ENABLED
  617. return(1);
  618. #else
  619. return(0);
  620. #endif
  621. case XML_WITH_WRITER:
  622. #ifdef LIBXML_WRITER_ENABLED
  623. return(1);
  624. #else
  625. return(0);
  626. #endif
  627. case XML_WITH_SAX1:
  628. #ifdef LIBXML_SAX1_ENABLED
  629. return(1);
  630. #else
  631. return(0);
  632. #endif
  633. case XML_WITH_FTP:
  634. #ifdef LIBXML_FTP_ENABLED
  635. return(1);
  636. #else
  637. return(0);
  638. #endif
  639. case XML_WITH_HTTP:
  640. #ifdef LIBXML_HTTP_ENABLED
  641. return(1);
  642. #else
  643. return(0);
  644. #endif
  645. case XML_WITH_VALID:
  646. #ifdef LIBXML_VALID_ENABLED
  647. return(1);
  648. #else
  649. return(0);
  650. #endif
  651. case XML_WITH_HTML:
  652. #ifdef LIBXML_HTML_ENABLED
  653. return(1);
  654. #else
  655. return(0);
  656. #endif
  657. case XML_WITH_LEGACY:
  658. #ifdef LIBXML_LEGACY_ENABLED
  659. return(1);
  660. #else
  661. return(0);
  662. #endif
  663. case XML_WITH_C14N:
  664. #ifdef LIBXML_C14N_ENABLED
  665. return(1);
  666. #else
  667. return(0);
  668. #endif
  669. case XML_WITH_CATALOG:
  670. #ifdef LIBXML_CATALOG_ENABLED
  671. return(1);
  672. #else
  673. return(0);
  674. #endif
  675. case XML_WITH_XPATH:
  676. #ifdef LIBXML_XPATH_ENABLED
  677. return(1);
  678. #else
  679. return(0);
  680. #endif
  681. case XML_WITH_XPTR:
  682. #ifdef LIBXML_XPTR_ENABLED
  683. return(1);
  684. #else
  685. return(0);
  686. #endif
  687. case XML_WITH_XINCLUDE:
  688. #ifdef LIBXML_XINCLUDE_ENABLED
  689. return(1);
  690. #else
  691. return(0);
  692. #endif
  693. case XML_WITH_ICONV:
  694. #ifdef LIBXML_ICONV_ENABLED
  695. return(1);
  696. #else
  697. return(0);
  698. #endif
  699. case XML_WITH_ISO8859X:
  700. #ifdef LIBXML_ISO8859X_ENABLED
  701. return(1);
  702. #else
  703. return(0);
  704. #endif
  705. case XML_WITH_UNICODE:
  706. #ifdef LIBXML_UNICODE_ENABLED
  707. return(1);
  708. #else
  709. return(0);
  710. #endif
  711. case XML_WITH_REGEXP:
  712. #ifdef LIBXML_REGEXP_ENABLED
  713. return(1);
  714. #else
  715. return(0);
  716. #endif
  717. case XML_WITH_AUTOMATA:
  718. #ifdef LIBXML_AUTOMATA_ENABLED
  719. return(1);
  720. #else
  721. return(0);
  722. #endif
  723. case XML_WITH_EXPR:
  724. #ifdef LIBXML_EXPR_ENABLED
  725. return(1);
  726. #else
  727. return(0);
  728. #endif
  729. case XML_WITH_SCHEMAS:
  730. #ifdef LIBXML_SCHEMAS_ENABLED
  731. return(1);
  732. #else
  733. return(0);
  734. #endif
  735. case XML_WITH_SCHEMATRON:
  736. #ifdef LIBXML_SCHEMATRON_ENABLED
  737. return(1);
  738. #else
  739. return(0);
  740. #endif
  741. case XML_WITH_MODULES:
  742. #ifdef LIBXML_MODULES_ENABLED
  743. return(1);
  744. #else
  745. return(0);
  746. #endif
  747. case XML_WITH_DEBUG:
  748. #ifdef LIBXML_DEBUG_ENABLED
  749. return(1);
  750. #else
  751. return(0);
  752. #endif
  753. case XML_WITH_DEBUG_MEM:
  754. #ifdef DEBUG_MEMORY_LOCATION
  755. return(1);
  756. #else
  757. return(0);
  758. #endif
  759. case XML_WITH_DEBUG_RUN:
  760. return(0);
  761. case XML_WITH_ZLIB:
  762. #ifdef LIBXML_ZLIB_ENABLED
  763. return(1);
  764. #else
  765. return(0);
  766. #endif
  767. case XML_WITH_LZMA:
  768. #ifdef LIBXML_LZMA_ENABLED
  769. return(1);
  770. #else
  771. return(0);
  772. #endif
  773. case XML_WITH_ICU:
  774. #ifdef LIBXML_ICU_ENABLED
  775. return(1);
  776. #else
  777. return(0);
  778. #endif
  779. default:
  780. break;
  781. }
  782. return(0);
  783. }
  784. /************************************************************************
  785. * *
  786. * SAX2 defaulted attributes handling *
  787. * *
  788. ************************************************************************/
  789. /**
  790. * xmlDetectSAX2:
  791. * @ctxt: an XML parser context
  792. *
  793. * Do the SAX2 detection and specific initialization
  794. */
  795. static void
  796. xmlDetectSAX2(xmlParserCtxtPtr ctxt) {
  797. xmlSAXHandlerPtr sax;
  798. /* Avoid unused variable warning if features are disabled. */
  799. (void) sax;
  800. if (ctxt == NULL) return;
  801. sax = ctxt->sax;
  802. #ifdef LIBXML_SAX1_ENABLED
  803. /*
  804. * Only enable SAX2 if there SAX2 element handlers, except when there
  805. * are no element handlers at all.
  806. */
  807. if ((sax) && (sax->initialized == XML_SAX2_MAGIC) &&
  808. ((sax->startElementNs != NULL) ||
  809. (sax->endElementNs != NULL) ||
  810. ((sax->startElement == NULL) && (sax->endElement == NULL))))
  811. ctxt->sax2 = 1;
  812. #else
  813. ctxt->sax2 = 1;
  814. #endif /* LIBXML_SAX1_ENABLED */
  815. ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
  816. ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
  817. ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
  818. if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) ||
  819. (ctxt->str_xml_ns == NULL)) {
  820. xmlErrMemory(ctxt, NULL);
  821. }
  822. }
  823. typedef struct {
  824. xmlHashedString prefix;
  825. xmlHashedString name;
  826. xmlHashedString value;
  827. const xmlChar *valueEnd;
  828. int external;
  829. int expandedSize;
  830. } xmlDefAttr;
  831. typedef struct _xmlDefAttrs xmlDefAttrs;
  832. typedef xmlDefAttrs *xmlDefAttrsPtr;
  833. struct _xmlDefAttrs {
  834. int nbAttrs; /* number of defaulted attributes on that element */
  835. int maxAttrs; /* the size of the array */
  836. #if __STDC_VERSION__ >= 199901L
  837. /* Using a C99 flexible array member avoids UBSan errors. */
  838. xmlDefAttr attrs[]; /* array of localname/prefix/values/external */
  839. #else
  840. xmlDefAttr attrs[1];
  841. #endif
  842. };
  843. /**
  844. * xmlAttrNormalizeSpace:
  845. * @src: the source string
  846. * @dst: the target string
  847. *
  848. * Normalize the space in non CDATA attribute values:
  849. * If the attribute type is not CDATA, then the XML processor MUST further
  850. * process the normalized attribute value by discarding any leading and
  851. * trailing space (#x20) characters, and by replacing sequences of space
  852. * (#x20) characters by a single space (#x20) character.
  853. * Note that the size of dst need to be at least src, and if one doesn't need
  854. * to preserve dst (and it doesn't come from a dictionary or read-only) then
  855. * passing src as dst is just fine.
  856. *
  857. * Returns a pointer to the normalized value (dst) or NULL if no conversion
  858. * is needed.
  859. */
  860. static xmlChar *
  861. xmlAttrNormalizeSpace(const xmlChar *src, xmlChar *dst)
  862. {
  863. if ((src == NULL) || (dst == NULL))
  864. return(NULL);
  865. while (*src == 0x20) src++;
  866. while (*src != 0) {
  867. if (*src == 0x20) {
  868. while (*src == 0x20) src++;
  869. if (*src != 0)
  870. *dst++ = 0x20;
  871. } else {
  872. *dst++ = *src++;
  873. }
  874. }
  875. *dst = 0;
  876. if (dst == src)
  877. return(NULL);
  878. return(dst);
  879. }
  880. /**
  881. * xmlAttrNormalizeSpace2:
  882. * @src: the source string
  883. *
  884. * Normalize the space in non CDATA attribute values, a slightly more complex
  885. * front end to avoid allocation problems when running on attribute values
  886. * coming from the input.
  887. *
  888. * Returns a pointer to the normalized value (dst) or NULL if no conversion
  889. * is needed.
  890. */
  891. static const xmlChar *
  892. xmlAttrNormalizeSpace2(xmlParserCtxtPtr ctxt, xmlChar *src, int *len)
  893. {
  894. int i;
  895. int remove_head = 0;
  896. int need_realloc = 0;
  897. const xmlChar *cur;
  898. if ((ctxt == NULL) || (src == NULL) || (len == NULL))
  899. return(NULL);
  900. i = *len;
  901. if (i <= 0)
  902. return(NULL);
  903. cur = src;
  904. while (*cur == 0x20) {
  905. cur++;
  906. remove_head++;
  907. }
  908. while (*cur != 0) {
  909. if (*cur == 0x20) {
  910. cur++;
  911. if ((*cur == 0x20) || (*cur == 0)) {
  912. need_realloc = 1;
  913. break;
  914. }
  915. } else
  916. cur++;
  917. }
  918. if (need_realloc) {
  919. xmlChar *ret;
  920. ret = xmlStrndup(src + remove_head, i - remove_head + 1);
  921. if (ret == NULL) {
  922. xmlErrMemory(ctxt, NULL);
  923. return(NULL);
  924. }
  925. xmlAttrNormalizeSpace(ret, ret);
  926. *len = strlen((const char *)ret);
  927. return(ret);
  928. } else if (remove_head) {
  929. *len -= remove_head;
  930. memmove(src, src + remove_head, 1 + *len);
  931. return(src);
  932. }
  933. return(NULL);
  934. }
  935. /**
  936. * xmlAddDefAttrs:
  937. * @ctxt: an XML parser context
  938. * @fullname: the element fullname
  939. * @fullattr: the attribute fullname
  940. * @value: the attribute value
  941. *
  942. * Add a defaulted attribute for an element
  943. */
  944. static void
  945. xmlAddDefAttrs(xmlParserCtxtPtr ctxt,
  946. const xmlChar *fullname,
  947. const xmlChar *fullattr,
  948. const xmlChar *value) {
  949. xmlDefAttrsPtr defaults;
  950. xmlDefAttr *attr;
  951. int len, expandedSize;
  952. xmlHashedString name;
  953. xmlHashedString prefix;
  954. xmlHashedString hvalue;
  955. const xmlChar *localname;
  956. /*
  957. * Allows to detect attribute redefinitions
  958. */
  959. if (ctxt->attsSpecial != NULL) {
  960. if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL)
  961. return;
  962. }
  963. if (ctxt->attsDefault == NULL) {
  964. ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict);
  965. if (ctxt->attsDefault == NULL)
  966. goto mem_error;
  967. }
  968. /*
  969. * split the element name into prefix:localname , the string found
  970. * are within the DTD and then not associated to namespace names.
  971. */
  972. localname = xmlSplitQName3(fullname, &len);
  973. if (localname == NULL) {
  974. name = xmlDictLookupHashed(ctxt->dict, fullname, -1);
  975. prefix.name = NULL;
  976. } else {
  977. name = xmlDictLookupHashed(ctxt->dict, localname, -1);
  978. prefix = xmlDictLookupHashed(ctxt->dict, fullname, len);
  979. if (prefix.name == NULL)
  980. goto mem_error;
  981. }
  982. if (name.name == NULL)
  983. goto mem_error;
  984. /*
  985. * make sure there is some storage
  986. */
  987. defaults = xmlHashLookup2(ctxt->attsDefault, name.name, prefix.name);
  988. if ((defaults == NULL) ||
  989. (defaults->nbAttrs >= defaults->maxAttrs)) {
  990. xmlDefAttrsPtr temp;
  991. int newSize;
  992. newSize = (defaults != NULL) ? 2 * defaults->maxAttrs : 4;
  993. temp = xmlRealloc(defaults,
  994. sizeof(*defaults) + newSize * sizeof(xmlDefAttr));
  995. if (temp == NULL)
  996. goto mem_error;
  997. if (defaults == NULL)
  998. temp->nbAttrs = 0;
  999. temp->maxAttrs = newSize;
  1000. defaults = temp;
  1001. if (xmlHashUpdateEntry2(ctxt->attsDefault, name.name, prefix.name,
  1002. defaults, NULL) < 0) {
  1003. xmlFree(defaults);
  1004. goto mem_error;
  1005. }
  1006. }
  1007. /*
  1008. * Split the attribute name into prefix:localname , the string found
  1009. * are within the DTD and hen not associated to namespace names.
  1010. */
  1011. localname = xmlSplitQName3(fullattr, &len);
  1012. if (localname == NULL) {
  1013. name = xmlDictLookupHashed(ctxt->dict, fullattr, -1);
  1014. prefix.name = NULL;
  1015. } else {
  1016. name = xmlDictLookupHashed(ctxt->dict, localname, -1);
  1017. prefix = xmlDictLookupHashed(ctxt->dict, fullattr, len);
  1018. if (prefix.name == NULL)
  1019. goto mem_error;
  1020. }
  1021. if (name.name == NULL)
  1022. goto mem_error;
  1023. /* intern the string and precompute the end */
  1024. len = strlen((const char *) value);
  1025. hvalue = xmlDictLookupHashed(ctxt->dict, value, len);
  1026. if (hvalue.name == NULL)
  1027. goto mem_error;
  1028. expandedSize = strlen((const char *) name.name);
  1029. if (prefix.name != NULL)
  1030. expandedSize += strlen((const char *) prefix.name);
  1031. expandedSize += len;
  1032. attr = &defaults->attrs[defaults->nbAttrs++];
  1033. attr->name = name;
  1034. attr->prefix = prefix;
  1035. attr->value = hvalue;
  1036. attr->valueEnd = hvalue.name + len;
  1037. attr->external = ctxt->external;
  1038. attr->expandedSize = expandedSize;
  1039. return;
  1040. mem_error:
  1041. xmlErrMemory(ctxt, NULL);
  1042. return;
  1043. }
  1044. /**
  1045. * xmlAddSpecialAttr:
  1046. * @ctxt: an XML parser context
  1047. * @fullname: the element fullname
  1048. * @fullattr: the attribute fullname
  1049. * @type: the attribute type
  1050. *
  1051. * Register this attribute type
  1052. */
  1053. static void
  1054. xmlAddSpecialAttr(xmlParserCtxtPtr ctxt,
  1055. const xmlChar *fullname,
  1056. const xmlChar *fullattr,
  1057. int type)
  1058. {
  1059. if (ctxt->attsSpecial == NULL) {
  1060. ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict);
  1061. if (ctxt->attsSpecial == NULL)
  1062. goto mem_error;
  1063. }
  1064. if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL)
  1065. return;
  1066. xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr,
  1067. (void *) (ptrdiff_t) type);
  1068. return;
  1069. mem_error:
  1070. xmlErrMemory(ctxt, NULL);
  1071. return;
  1072. }
  1073. /**
  1074. * xmlCleanSpecialAttrCallback:
  1075. *
  1076. * Removes CDATA attributes from the special attribute table
  1077. */
  1078. static void
  1079. xmlCleanSpecialAttrCallback(void *payload, void *data,
  1080. const xmlChar *fullname, const xmlChar *fullattr,
  1081. const xmlChar *unused ATTRIBUTE_UNUSED) {
  1082. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) data;
  1083. if (((ptrdiff_t) payload) == XML_ATTRIBUTE_CDATA) {
  1084. xmlHashRemoveEntry2(ctxt->attsSpecial, fullname, fullattr, NULL);
  1085. }
  1086. }
  1087. /**
  1088. * xmlCleanSpecialAttr:
  1089. * @ctxt: an XML parser context
  1090. *
  1091. * Trim the list of attributes defined to remove all those of type
  1092. * CDATA as they are not special. This call should be done when finishing
  1093. * to parse the DTD and before starting to parse the document root.
  1094. */
  1095. static void
  1096. xmlCleanSpecialAttr(xmlParserCtxtPtr ctxt)
  1097. {
  1098. if (ctxt->attsSpecial == NULL)
  1099. return;
  1100. xmlHashScanFull(ctxt->attsSpecial, xmlCleanSpecialAttrCallback, ctxt);
  1101. if (xmlHashSize(ctxt->attsSpecial) == 0) {
  1102. xmlHashFree(ctxt->attsSpecial, NULL);
  1103. ctxt->attsSpecial = NULL;
  1104. }
  1105. return;
  1106. }
  1107. /**
  1108. * xmlCheckLanguageID:
  1109. * @lang: pointer to the string value
  1110. *
  1111. * DEPRECATED: Internal function, do not use.
  1112. *
  1113. * Checks that the value conforms to the LanguageID production:
  1114. *
  1115. * NOTE: this is somewhat deprecated, those productions were removed from
  1116. * the XML Second edition.
  1117. *
  1118. * [33] LanguageID ::= Langcode ('-' Subcode)*
  1119. * [34] Langcode ::= ISO639Code | IanaCode | UserCode
  1120. * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
  1121. * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
  1122. * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
  1123. * [38] Subcode ::= ([a-z] | [A-Z])+
  1124. *
  1125. * The current REC reference the successors of RFC 1766, currently 5646
  1126. *
  1127. * http://www.rfc-editor.org/rfc/rfc5646.txt
  1128. * langtag = language
  1129. * ["-" script]
  1130. * ["-" region]
  1131. * *("-" variant)
  1132. * *("-" extension)
  1133. * ["-" privateuse]
  1134. * language = 2*3ALPHA ; shortest ISO 639 code
  1135. * ["-" extlang] ; sometimes followed by
  1136. * ; extended language subtags
  1137. * / 4ALPHA ; or reserved for future use
  1138. * / 5*8ALPHA ; or registered language subtag
  1139. *
  1140. * extlang = 3ALPHA ; selected ISO 639 codes
  1141. * *2("-" 3ALPHA) ; permanently reserved
  1142. *
  1143. * script = 4ALPHA ; ISO 15924 code
  1144. *
  1145. * region = 2ALPHA ; ISO 3166-1 code
  1146. * / 3DIGIT ; UN M.49 code
  1147. *
  1148. * variant = 5*8alphanum ; registered variants
  1149. * / (DIGIT 3alphanum)
  1150. *
  1151. * extension = singleton 1*("-" (2*8alphanum))
  1152. *
  1153. * ; Single alphanumerics
  1154. * ; "x" reserved for private use
  1155. * singleton = DIGIT ; 0 - 9
  1156. * / %x41-57 ; A - W
  1157. * / %x59-5A ; Y - Z
  1158. * / %x61-77 ; a - w
  1159. * / %x79-7A ; y - z
  1160. *
  1161. * it sounds right to still allow Irregular i-xxx IANA and user codes too
  1162. * The parser below doesn't try to cope with extension or privateuse
  1163. * that could be added but that's not interoperable anyway
  1164. *
  1165. * Returns 1 if correct 0 otherwise
  1166. **/
  1167. int
  1168. xmlCheckLanguageID(const xmlChar * lang)
  1169. {
  1170. const xmlChar *cur = lang, *nxt;
  1171. if (cur == NULL)
  1172. return (0);
  1173. if (((cur[0] == 'i') && (cur[1] == '-')) ||
  1174. ((cur[0] == 'I') && (cur[1] == '-')) ||
  1175. ((cur[0] == 'x') && (cur[1] == '-')) ||
  1176. ((cur[0] == 'X') && (cur[1] == '-'))) {
  1177. /*
  1178. * Still allow IANA code and user code which were coming
  1179. * from the previous version of the XML-1.0 specification
  1180. * it's deprecated but we should not fail
  1181. */
  1182. cur += 2;
  1183. while (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
  1184. ((cur[0] >= 'a') && (cur[0] <= 'z')))
  1185. cur++;
  1186. return(cur[0] == 0);
  1187. }
  1188. nxt = cur;
  1189. while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
  1190. ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
  1191. nxt++;
  1192. if (nxt - cur >= 4) {
  1193. /*
  1194. * Reserved
  1195. */
  1196. if ((nxt - cur > 8) || (nxt[0] != 0))
  1197. return(0);
  1198. return(1);
  1199. }
  1200. if (nxt - cur < 2)
  1201. return(0);
  1202. /* we got an ISO 639 code */
  1203. if (nxt[0] == 0)
  1204. return(1);
  1205. if (nxt[0] != '-')
  1206. return(0);
  1207. nxt++;
  1208. cur = nxt;
  1209. /* now we can have extlang or script or region or variant */
  1210. if ((nxt[0] >= '0') && (nxt[0] <= '9'))
  1211. goto region_m49;
  1212. while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
  1213. ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
  1214. nxt++;
  1215. if (nxt - cur == 4)
  1216. goto script;
  1217. if (nxt - cur == 2)
  1218. goto region;
  1219. if ((nxt - cur >= 5) && (nxt - cur <= 8))
  1220. goto variant;
  1221. if (nxt - cur != 3)
  1222. return(0);
  1223. /* we parsed an extlang */
  1224. if (nxt[0] == 0)
  1225. return(1);
  1226. if (nxt[0] != '-')
  1227. return(0);
  1228. nxt++;
  1229. cur = nxt;
  1230. /* now we can have script or region or variant */
  1231. if ((nxt[0] >= '0') && (nxt[0] <= '9'))
  1232. goto region_m49;
  1233. while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
  1234. ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
  1235. nxt++;
  1236. if (nxt - cur == 2)
  1237. goto region;
  1238. if ((nxt - cur >= 5) && (nxt - cur <= 8))
  1239. goto variant;
  1240. if (nxt - cur != 4)
  1241. return(0);
  1242. /* we parsed a script */
  1243. script:
  1244. if (nxt[0] == 0)
  1245. return(1);
  1246. if (nxt[0] != '-')
  1247. return(0);
  1248. nxt++;
  1249. cur = nxt;
  1250. /* now we can have region or variant */
  1251. if ((nxt[0] >= '0') && (nxt[0] <= '9'))
  1252. goto region_m49;
  1253. while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
  1254. ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
  1255. nxt++;
  1256. if ((nxt - cur >= 5) && (nxt - cur <= 8))
  1257. goto variant;
  1258. if (nxt - cur != 2)
  1259. return(0);
  1260. /* we parsed a region */
  1261. region:
  1262. if (nxt[0] == 0)
  1263. return(1);
  1264. if (nxt[0] != '-')
  1265. return(0);
  1266. nxt++;
  1267. cur = nxt;
  1268. /* now we can just have a variant */
  1269. while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
  1270. ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
  1271. nxt++;
  1272. if ((nxt - cur < 5) || (nxt - cur > 8))
  1273. return(0);
  1274. /* we parsed a variant */
  1275. variant:
  1276. if (nxt[0] == 0)
  1277. return(1);
  1278. if (nxt[0] != '-')
  1279. return(0);
  1280. /* extensions and private use subtags not checked */
  1281. return (1);
  1282. region_m49:
  1283. if (((nxt[1] >= '0') && (nxt[1] <= '9')) &&
  1284. ((nxt[2] >= '0') && (nxt[2] <= '9'))) {
  1285. nxt += 3;
  1286. goto region;
  1287. }
  1288. return(0);
  1289. }
  1290. /************************************************************************
  1291. * *
  1292. * Parser stacks related functions and macros *
  1293. * *
  1294. ************************************************************************/
  1295. static xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt,
  1296. const xmlChar ** str);
  1297. /**
  1298. * xmlParserNsCreate:
  1299. *
  1300. * Create a new namespace database.
  1301. *
  1302. * Returns the new obejct.
  1303. */
  1304. xmlParserNsData *
  1305. xmlParserNsCreate(void) {
  1306. xmlParserNsData *nsdb = xmlMalloc(sizeof(*nsdb));
  1307. if (nsdb == NULL)
  1308. return(NULL);
  1309. memset(nsdb, 0, sizeof(*nsdb));
  1310. nsdb->defaultNsIndex = INT_MAX;
  1311. return(nsdb);
  1312. }
  1313. /**
  1314. * xmlParserNsFree:
  1315. * @nsdb: namespace database
  1316. *
  1317. * Free a namespace database.
  1318. */
  1319. void
  1320. xmlParserNsFree(xmlParserNsData *nsdb) {
  1321. if (nsdb == NULL)
  1322. return;
  1323. xmlFree(nsdb->extra);
  1324. xmlFree(nsdb->hash);
  1325. xmlFree(nsdb);
  1326. }
  1327. /**
  1328. * xmlParserNsReset:
  1329. * @nsdb: namespace database
  1330. *
  1331. * Reset a namespace database.
  1332. */
  1333. static void
  1334. xmlParserNsReset(xmlParserNsData *nsdb) {
  1335. if (nsdb == NULL)
  1336. return;
  1337. nsdb->hashElems = 0;
  1338. nsdb->elementId = 0;
  1339. nsdb->defaultNsIndex = INT_MAX;
  1340. if (nsdb->hash)
  1341. memset(nsdb->hash, 0, nsdb->hashSize * sizeof(nsdb->hash[0]));
  1342. }
  1343. /**
  1344. * xmlParserStartElement:
  1345. * @nsdb: namespace database
  1346. *
  1347. * Signal that a new element has started.
  1348. *
  1349. * Returns 0 on success, -1 if the element counter overflowed.
  1350. */
  1351. static int
  1352. xmlParserNsStartElement(xmlParserNsData *nsdb) {
  1353. if (nsdb->elementId == UINT_MAX)
  1354. return(-1);
  1355. nsdb->elementId++;
  1356. return(0);
  1357. }
  1358. /**
  1359. * xmlParserNsLookup:
  1360. * @ctxt: parser context
  1361. * @prefix: namespace prefix
  1362. * @bucketPtr: optional bucket (return value)
  1363. *
  1364. * Lookup namespace with given prefix. If @bucketPtr is non-NULL, it will
  1365. * be set to the matching bucket, or the first empty bucket if no match
  1366. * was found.
  1367. *
  1368. * Returns the namespace index on success, INT_MAX if no namespace was
  1369. * found.
  1370. */
  1371. static int
  1372. xmlParserNsLookup(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix,
  1373. xmlParserNsBucket **bucketPtr) {
  1374. xmlParserNsBucket *bucket, *tombstone;
  1375. unsigned index, hashValue;
  1376. if (prefix->name == NULL)
  1377. return(ctxt->nsdb->defaultNsIndex);
  1378. if (ctxt->nsdb->hashSize == 0)
  1379. return(INT_MAX);
  1380. hashValue = prefix->hashValue;
  1381. index = hashValue & (ctxt->nsdb->hashSize - 1);
  1382. bucket = &ctxt->nsdb->hash[index];
  1383. tombstone = NULL;
  1384. while (bucket->hashValue) {
  1385. if (bucket->index == INT_MAX) {
  1386. if (tombstone == NULL)
  1387. tombstone = bucket;
  1388. } else if (bucket->hashValue == hashValue) {
  1389. if (ctxt->nsTab[bucket->index * 2] == prefix->name) {
  1390. if (bucketPtr != NULL)
  1391. *bucketPtr = bucket;
  1392. return(bucket->index);
  1393. }
  1394. }
  1395. index++;
  1396. bucket++;
  1397. if (index == ctxt->nsdb->hashSize) {
  1398. index = 0;
  1399. bucket = ctxt->nsdb->hash;
  1400. }
  1401. }
  1402. if (bucketPtr != NULL)
  1403. *bucketPtr = tombstone ? tombstone : bucket;
  1404. return(INT_MAX);
  1405. }
  1406. /**
  1407. * xmlParserNsLookupUri:
  1408. * @ctxt: parser context
  1409. * @prefix: namespace prefix
  1410. *
  1411. * Lookup namespace URI with given prefix.
  1412. *
  1413. * Returns the namespace URI on success, NULL if no namespace was found.
  1414. */
  1415. static const xmlChar *
  1416. xmlParserNsLookupUri(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix) {
  1417. const xmlChar *ret;
  1418. int nsIndex;
  1419. if (prefix->name == ctxt->str_xml)
  1420. return(ctxt->str_xml_ns);
  1421. nsIndex = xmlParserNsLookup(ctxt, prefix, NULL);
  1422. if (nsIndex == INT_MAX)
  1423. return(NULL);
  1424. ret = ctxt->nsTab[nsIndex * 2 + 1];
  1425. if (ret[0] == 0)
  1426. ret = NULL;
  1427. return(ret);
  1428. }
  1429. /**
  1430. * xmlParserNsLookupSax:
  1431. * @ctxt: parser context
  1432. * @prefix: namespace prefix
  1433. *
  1434. * Lookup extra data for the given prefix. This returns data stored
  1435. * with xmlParserNsUdpateSax.
  1436. *
  1437. * Returns the data on success, NULL if no namespace was found.
  1438. */
  1439. void *
  1440. xmlParserNsLookupSax(xmlParserCtxtPtr ctxt, const xmlChar *prefix) {
  1441. xmlHashedString hprefix;
  1442. int nsIndex;
  1443. if (prefix == ctxt->str_xml)
  1444. return(NULL);
  1445. hprefix.name = prefix;
  1446. if (prefix != NULL)
  1447. hprefix.hashValue = xmlDictComputeHash(ctxt->dict, prefix);
  1448. else
  1449. hprefix.hashValue = 0;
  1450. nsIndex = xmlParserNsLookup(ctxt, &hprefix, NULL);
  1451. if (nsIndex == INT_MAX)
  1452. return(NULL);
  1453. return(ctxt->nsdb->extra[nsIndex].saxData);
  1454. }
  1455. /**
  1456. * xmlParserNsUpdateSax:
  1457. * @ctxt: parser context
  1458. * @prefix: namespace prefix
  1459. * @saxData: extra data for SAX handler
  1460. *
  1461. * Sets or updates extra data for the given prefix. This value will be
  1462. * returned by xmlParserNsLookupSax as long as the namespace with the
  1463. * given prefix is in scope.
  1464. *
  1465. * Returns the data on success, NULL if no namespace was found.
  1466. */
  1467. int
  1468. xmlParserNsUpdateSax(xmlParserCtxtPtr ctxt, const xmlChar *prefix,
  1469. void *saxData) {
  1470. xmlHashedString hprefix;
  1471. int nsIndex;
  1472. if (prefix == ctxt->str_xml)
  1473. return(-1);
  1474. hprefix.name = prefix;
  1475. if (prefix != NULL)
  1476. hprefix.hashValue = xmlDictComputeHash(ctxt->dict, prefix);
  1477. else
  1478. hprefix.hashValue = 0;
  1479. nsIndex = xmlParserNsLookup(ctxt, &hprefix, NULL);
  1480. if (nsIndex == INT_MAX)
  1481. return(-1);
  1482. ctxt->nsdb->extra[nsIndex].saxData = saxData;
  1483. return(0);
  1484. }
  1485. /**
  1486. * xmlParserNsGrow:
  1487. * @ctxt: parser context
  1488. *
  1489. * Grows the namespace tables.
  1490. *
  1491. * Returns 0 on success, -1 if a memory allocation failed.
  1492. */
  1493. static int
  1494. xmlParserNsGrow(xmlParserCtxtPtr ctxt) {
  1495. const xmlChar **table;
  1496. xmlParserNsExtra *extra;
  1497. int newSize;
  1498. if (ctxt->nsMax > INT_MAX / 2)
  1499. goto error;
  1500. newSize = ctxt->nsMax ? ctxt->nsMax * 2 : 16;
  1501. table = xmlRealloc(ctxt->nsTab, 2 * newSize * sizeof(table[0]));
  1502. if (table == NULL)
  1503. goto error;
  1504. ctxt->nsTab = table;
  1505. extra = xmlRealloc(ctxt->nsdb->extra, newSize * sizeof(extra[0]));
  1506. if (extra == NULL)
  1507. goto error;
  1508. ctxt->nsdb->extra = extra;
  1509. ctxt->nsMax = newSize;
  1510. return(0);
  1511. error:
  1512. xmlErrMemory(ctxt, NULL);
  1513. return(-1);
  1514. }
  1515. /**
  1516. * xmlParserNsPush:
  1517. * @ctxt: parser context
  1518. * @prefix: prefix with hash value
  1519. * @uri: uri with hash value
  1520. * @saxData: extra data for SAX handler
  1521. * @defAttr: whether the namespace comes from a default attribute
  1522. *
  1523. * Push a new namespace on the table.
  1524. *
  1525. * Returns 1 if the namespace was pushed, 0 if the namespace was ignored,
  1526. * -1 if a memory allocation failed.
  1527. */
  1528. static int
  1529. xmlParserNsPush(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix,
  1530. const xmlHashedString *uri, void *saxData, int defAttr) {
  1531. xmlParserNsBucket *bucket = NULL;
  1532. xmlParserNsExtra *extra;
  1533. const xmlChar **ns;
  1534. unsigned hashValue, nsIndex, oldIndex;
  1535. if ((prefix != NULL) && (prefix->name == ctxt->str_xml))
  1536. return(0);
  1537. if ((ctxt->nsNr >= ctxt->nsMax) && (xmlParserNsGrow(ctxt) < 0)) {
  1538. xmlErrMemory(ctxt, NULL);
  1539. return(-1);
  1540. }
  1541. /*
  1542. * Default namespace and 'xml' namespace
  1543. */
  1544. if ((prefix == NULL) || (prefix->name == NULL)) {
  1545. oldIndex = ctxt->nsdb->defaultNsIndex;
  1546. if (oldIndex != INT_MAX) {
  1547. extra = &ctxt->nsdb->extra[oldIndex];
  1548. if (extra->elementId == ctxt->nsdb->elementId) {
  1549. if (defAttr == 0)
  1550. xmlErrAttributeDup(ctxt, NULL, BAD_CAST "xmlns");
  1551. return(0);
  1552. }
  1553. if ((ctxt->options & XML_PARSE_NSCLEAN) &&
  1554. (uri->name == ctxt->nsTab[oldIndex * 2 + 1]))
  1555. return(0);
  1556. }
  1557. ctxt->nsdb->defaultNsIndex = ctxt->nsNr;
  1558. goto populate_entry;
  1559. }
  1560. /*
  1561. * Hash table lookup
  1562. */
  1563. oldIndex = xmlParserNsLookup(ctxt, prefix, &bucket);
  1564. if (oldIndex != INT_MAX) {
  1565. extra = &ctxt->nsdb->extra[oldIndex];
  1566. /*
  1567. * Check for duplicate definitions on the same element.
  1568. */
  1569. if (extra->elementId == ctxt->nsdb->elementId) {
  1570. if (defAttr == 0)
  1571. xmlErrAttributeDup(ctxt, BAD_CAST "xmlns", prefix->name);
  1572. return(0);
  1573. }
  1574. if ((ctxt->options & XML_PARSE_NSCLEAN) &&
  1575. (uri->name == ctxt->nsTab[bucket->index * 2 + 1]))
  1576. return(0);
  1577. bucket->index = ctxt->nsNr;
  1578. goto populate_entry;
  1579. }
  1580. /*
  1581. * Insert new bucket
  1582. */
  1583. hashValue = prefix->hashValue;
  1584. /*
  1585. * Grow hash table, 50% fill factor
  1586. */
  1587. if (ctxt->nsdb->hashElems + 1 > ctxt->nsdb->hashSize / 2) {
  1588. xmlParserNsBucket *newHash;
  1589. unsigned newSize, i, index;
  1590. if (ctxt->nsdb->hashSize > UINT_MAX / 2) {
  1591. xmlErrMemory(ctxt, NULL);
  1592. return(-1);
  1593. }
  1594. newSize = ctxt->nsdb->hashSize ? ctxt->nsdb->hashSize * 2 : 16;
  1595. newHash = xmlMalloc(newSize * sizeof(newHash[0]));
  1596. if (newHash == NULL) {
  1597. xmlErrMemory(ctxt, NULL);
  1598. return(-1);
  1599. }
  1600. memset(newHash, 0, newSize * sizeof(newHash[0]));
  1601. for (i = 0; i < ctxt->nsdb->hashSize; i++) {
  1602. unsigned hv = ctxt->nsdb->hash[i].hashValue;
  1603. unsigned newIndex;
  1604. if ((hv == 0) || (ctxt->nsdb->hash[i].index == INT_MAX))
  1605. continue;
  1606. newIndex = hv & (newSize - 1);
  1607. while (newHash[newIndex].hashValue != 0) {
  1608. newIndex++;
  1609. if (newIndex == newSize)
  1610. newIndex = 0;
  1611. }
  1612. newHash[newIndex] = ctxt->nsdb->hash[i];
  1613. }
  1614. xmlFree(ctxt->nsdb->hash);
  1615. ctxt->nsdb->hash = newHash;
  1616. ctxt->nsdb->hashSize = newSize;
  1617. /*
  1618. * Relookup
  1619. */
  1620. index = hashValue & (newSize - 1);
  1621. while (newHash[index].hashValue != 0) {
  1622. index++;
  1623. if (index == newSize)
  1624. index = 0;
  1625. }
  1626. bucket = &newHash[index];
  1627. }
  1628. bucket->hashValue = hashValue;
  1629. bucket->index = ctxt->nsNr;
  1630. ctxt->nsdb->hashElems++;
  1631. oldIndex = INT_MAX;
  1632. populate_entry:
  1633. nsIndex = ctxt->nsNr;
  1634. ns = &ctxt->nsTab[nsIndex * 2];
  1635. ns[0] = prefix ? prefix->name : NULL;
  1636. ns[1] = uri->name;
  1637. extra = &ctxt->nsdb->extra[nsIndex];
  1638. extra->saxData = saxData;
  1639. extra->prefixHashValue = prefix ? prefix->hashValue : 0;
  1640. extra->uriHashValue = uri->hashValue;
  1641. extra->elementId = ctxt->nsdb->elementId;
  1642. extra->oldIndex = oldIndex;
  1643. ctxt->nsNr++;
  1644. return(1);
  1645. }
  1646. /**
  1647. * xmlParserNsPop:
  1648. * @ctxt: an XML parser context
  1649. * @nr: the number to pop
  1650. *
  1651. * Pops the top @nr namespaces and restores the hash table.
  1652. *
  1653. * Returns the number of namespaces popped.
  1654. */
  1655. static int
  1656. xmlParserNsPop(xmlParserCtxtPtr ctxt, int nr)
  1657. {
  1658. int i;
  1659. /* assert(nr <= ctxt->nsNr); */
  1660. for (i = ctxt->nsNr - 1; i >= ctxt->nsNr - nr; i--) {
  1661. const xmlChar *prefix = ctxt->nsTab[i * 2];
  1662. xmlParserNsExtra *extra = &ctxt->nsdb->extra[i];
  1663. if (prefix == NULL) {
  1664. ctxt->nsdb->defaultNsIndex = extra->oldIndex;
  1665. } else {
  1666. xmlHashedString hprefix;
  1667. xmlParserNsBucket *bucket = NULL;
  1668. hprefix.name = prefix;
  1669. hprefix.hashValue = extra->prefixHashValue;
  1670. xmlParserNsLookup(ctxt, &hprefix, &bucket);
  1671. /* assert(bucket && bucket->hashValue); */
  1672. bucket->index = extra->oldIndex;
  1673. }
  1674. }
  1675. ctxt->nsNr -= nr;
  1676. return(nr);
  1677. }
  1678. static int
  1679. xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) {
  1680. const xmlChar **atts;
  1681. unsigned *attallocs;
  1682. int maxatts;
  1683. if (nr + 5 > ctxt->maxatts) {
  1684. maxatts = ctxt->maxatts == 0 ? 55 : (nr + 5) * 2;
  1685. atts = (const xmlChar **) xmlMalloc(
  1686. maxatts * sizeof(const xmlChar *));
  1687. if (atts == NULL) goto mem_error;
  1688. attallocs = xmlRealloc(ctxt->attallocs,
  1689. (maxatts / 5) * sizeof(attallocs[0]));
  1690. if (attallocs == NULL) {
  1691. xmlFree(atts);
  1692. goto mem_error;
  1693. }
  1694. if (ctxt->maxatts > 0)
  1695. memcpy(atts, ctxt->atts, ctxt->maxatts * sizeof(const xmlChar *));
  1696. xmlFree(ctxt->atts);
  1697. ctxt->atts = atts;
  1698. ctxt->attallocs = attallocs;
  1699. ctxt->maxatts = maxatts;
  1700. }
  1701. return(ctxt->maxatts);
  1702. mem_error:
  1703. xmlErrMemory(ctxt, NULL);
  1704. return(-1);
  1705. }
  1706. /**
  1707. * inputPush:
  1708. * @ctxt: an XML parser context
  1709. * @value: the parser input
  1710. *
  1711. * Pushes a new parser input on top of the input stack
  1712. *
  1713. * Returns -1 in case of error, the index in the stack otherwise
  1714. */
  1715. int
  1716. inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value)
  1717. {
  1718. if ((ctxt == NULL) || (value == NULL))
  1719. return(-1);
  1720. if (ctxt->inputNr >= ctxt->inputMax) {
  1721. size_t newSize = ctxt->inputMax * 2;
  1722. xmlParserInputPtr *tmp;
  1723. tmp = (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab,
  1724. newSize * sizeof(*tmp));
  1725. if (tmp == NULL) {
  1726. xmlErrMemory(ctxt, NULL);
  1727. return (-1);
  1728. }
  1729. ctxt->inputTab = tmp;
  1730. ctxt->inputMax = newSize;
  1731. }
  1732. ctxt->inputTab[ctxt->inputNr] = value;
  1733. ctxt->input = value;
  1734. return (ctxt->inputNr++);
  1735. }
  1736. /**
  1737. * inputPop:
  1738. * @ctxt: an XML parser context
  1739. *
  1740. * Pops the top parser input from the input stack
  1741. *
  1742. * Returns the input just removed
  1743. */
  1744. xmlParserInputPtr
  1745. inputPop(xmlParserCtxtPtr ctxt)
  1746. {
  1747. xmlParserInputPtr ret;
  1748. if (ctxt == NULL)
  1749. return(NULL);
  1750. if (ctxt->inputNr <= 0)
  1751. return (NULL);
  1752. ctxt->inputNr--;
  1753. if (ctxt->inputNr > 0)
  1754. ctxt->input = ctxt->inputTab[ctxt->inputNr - 1];
  1755. else
  1756. ctxt->input = NULL;
  1757. ret = ctxt->inputTab[ctxt->inputNr];
  1758. ctxt->inputTab[ctxt->inputNr] = NULL;
  1759. return (ret);
  1760. }
  1761. /**
  1762. * nodePush:
  1763. * @ctxt: an XML parser context
  1764. * @value: the element node
  1765. *
  1766. * DEPRECATED: Internal function, do not use.
  1767. *
  1768. * Pushes a new element node on top of the node stack
  1769. *
  1770. * Returns -1 in case of error, the index in the stack otherwise
  1771. */
  1772. int
  1773. nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value)
  1774. {
  1775. if (ctxt == NULL) return(0);
  1776. if (ctxt->nodeNr >= ctxt->nodeMax) {
  1777. xmlNodePtr *tmp;
  1778. tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
  1779. ctxt->nodeMax * 2 *
  1780. sizeof(ctxt->nodeTab[0]));
  1781. if (tmp == NULL) {
  1782. xmlErrMemory(ctxt, NULL);
  1783. return (-1);
  1784. }
  1785. ctxt->nodeTab = tmp;
  1786. ctxt->nodeMax *= 2;
  1787. }
  1788. if ((((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) &&
  1789. ((ctxt->options & XML_PARSE_HUGE) == 0)) {
  1790. xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR,
  1791. "Excessive depth in document: %d use XML_PARSE_HUGE option\n",
  1792. xmlParserMaxDepth);
  1793. xmlHaltParser(ctxt);
  1794. return(-1);
  1795. }
  1796. ctxt->nodeTab[ctxt->nodeNr] = value;
  1797. ctxt->node = value;
  1798. return (ctxt->nodeNr++);
  1799. }
  1800. /**
  1801. * nodePop:
  1802. * @ctxt: an XML parser context
  1803. *
  1804. * DEPRECATED: Internal function, do not use.
  1805. *
  1806. * Pops the top element node from the node stack
  1807. *
  1808. * Returns the node just removed
  1809. */
  1810. xmlNodePtr
  1811. nodePop(xmlParserCtxtPtr ctxt)
  1812. {
  1813. xmlNodePtr ret;
  1814. if (ctxt == NULL) return(NULL);
  1815. if (ctxt->nodeNr <= 0)
  1816. return (NULL);
  1817. ctxt->nodeNr--;
  1818. if (ctxt->nodeNr > 0)
  1819. ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
  1820. else
  1821. ctxt->node = NULL;
  1822. ret = ctxt->nodeTab[ctxt->nodeNr];
  1823. ctxt->nodeTab[ctxt->nodeNr] = NULL;
  1824. return (ret);
  1825. }
  1826. /**
  1827. * nameNsPush:
  1828. * @ctxt: an XML parser context
  1829. * @value: the element name
  1830. * @prefix: the element prefix
  1831. * @URI: the element namespace name
  1832. * @line: the current line number for error messages
  1833. * @nsNr: the number of namespaces pushed on the namespace table
  1834. *
  1835. * Pushes a new element name/prefix/URL on top of the name stack
  1836. *
  1837. * Returns -1 in case of error, the index in the stack otherwise
  1838. */
  1839. static int
  1840. nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value,
  1841. const xmlChar *prefix, const xmlChar *URI, int line, int nsNr)
  1842. {
  1843. xmlStartTag *tag;
  1844. if (ctxt->nameNr >= ctxt->nameMax) {
  1845. const xmlChar * *tmp;
  1846. xmlStartTag *tmp2;
  1847. ctxt->nameMax *= 2;
  1848. tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
  1849. ctxt->nameMax *
  1850. sizeof(ctxt->nameTab[0]));
  1851. if (tmp == NULL) {
  1852. ctxt->nameMax /= 2;
  1853. goto mem_error;
  1854. }
  1855. ctxt->nameTab = tmp;
  1856. tmp2 = (xmlStartTag *) xmlRealloc((void * *)ctxt->pushTab,
  1857. ctxt->nameMax *
  1858. sizeof(ctxt->pushTab[0]));
  1859. if (tmp2 == NULL) {
  1860. ctxt->nameMax /= 2;
  1861. goto mem_error;
  1862. }
  1863. ctxt->pushTab = tmp2;
  1864. } else if (ctxt->pushTab == NULL) {
  1865. ctxt->pushTab = (xmlStartTag *) xmlMalloc(ctxt->nameMax *
  1866. sizeof(ctxt->pushTab[0]));
  1867. if (ctxt->pushTab == NULL)
  1868. goto mem_error;
  1869. }
  1870. ctxt->nameTab[ctxt->nameNr] = value;
  1871. ctxt->name = value;
  1872. tag = &ctxt->pushTab[ctxt->nameNr];
  1873. tag->prefix = prefix;
  1874. tag->URI = URI;
  1875. tag->line = line;
  1876. tag->nsNr = nsNr;
  1877. return (ctxt->nameNr++);
  1878. mem_error:
  1879. xmlErrMemory(ctxt, NULL);
  1880. return (-1);
  1881. }
  1882. #ifdef LIBXML_PUSH_ENABLED
  1883. /**
  1884. * nameNsPop:
  1885. * @ctxt: an XML parser context
  1886. *
  1887. * Pops the top element/prefix/URI name from the name stack
  1888. *
  1889. * Returns the name just removed
  1890. */
  1891. static const xmlChar *
  1892. nameNsPop(xmlParserCtxtPtr ctxt)
  1893. {
  1894. const xmlChar *ret;
  1895. if (ctxt->nameNr <= 0)
  1896. return (NULL);
  1897. ctxt->nameNr--;
  1898. if (ctxt->nameNr > 0)
  1899. ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
  1900. else
  1901. ctxt->name = NULL;
  1902. ret = ctxt->nameTab[ctxt->nameNr];
  1903. ctxt->nameTab[ctxt->nameNr] = NULL;
  1904. return (ret);
  1905. }
  1906. #endif /* LIBXML_PUSH_ENABLED */
  1907. /**
  1908. * namePush:
  1909. * @ctxt: an XML parser context
  1910. * @value: the element name
  1911. *
  1912. * DEPRECATED: Internal function, do not use.
  1913. *
  1914. * Pushes a new element name on top of the name stack
  1915. *
  1916. * Returns -1 in case of error, the index in the stack otherwise
  1917. */
  1918. int
  1919. namePush(xmlParserCtxtPtr ctxt, const xmlChar * value)
  1920. {
  1921. if (ctxt == NULL) return (-1);
  1922. if (ctxt->nameNr >= ctxt->nameMax) {
  1923. const xmlChar * *tmp;
  1924. tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
  1925. ctxt->nameMax * 2 *
  1926. sizeof(ctxt->nameTab[0]));
  1927. if (tmp == NULL) {
  1928. goto mem_error;
  1929. }
  1930. ctxt->nameTab = tmp;
  1931. ctxt->nameMax *= 2;
  1932. }
  1933. ctxt->nameTab[ctxt->nameNr] = value;
  1934. ctxt->name = value;
  1935. return (ctxt->nameNr++);
  1936. mem_error:
  1937. xmlErrMemory(ctxt, NULL);
  1938. return (-1);
  1939. }
  1940. /**
  1941. * namePop:
  1942. * @ctxt: an XML parser context
  1943. *
  1944. * DEPRECATED: Internal function, do not use.
  1945. *
  1946. * Pops the top element name from the name stack
  1947. *
  1948. * Returns the name just removed
  1949. */
  1950. const xmlChar *
  1951. namePop(xmlParserCtxtPtr ctxt)
  1952. {
  1953. const xmlChar *ret;
  1954. if ((ctxt == NULL) || (ctxt->nameNr <= 0))
  1955. return (NULL);
  1956. ctxt->nameNr--;
  1957. if (ctxt->nameNr > 0)
  1958. ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
  1959. else
  1960. ctxt->name = NULL;
  1961. ret = ctxt->nameTab[ctxt->nameNr];
  1962. ctxt->nameTab[ctxt->nameNr] = NULL;
  1963. return (ret);
  1964. }
  1965. static int spacePush(xmlParserCtxtPtr ctxt, int val) {
  1966. if (ctxt->spaceNr >= ctxt->spaceMax) {
  1967. int *tmp;
  1968. ctxt->spaceMax *= 2;
  1969. tmp = (int *) xmlRealloc(ctxt->spaceTab,
  1970. ctxt->spaceMax * sizeof(ctxt->spaceTab[0]));
  1971. if (tmp == NULL) {
  1972. xmlErrMemory(ctxt, NULL);
  1973. ctxt->spaceMax /=2;
  1974. return(-1);
  1975. }
  1976. ctxt->spaceTab = tmp;
  1977. }
  1978. ctxt->spaceTab[ctxt->spaceNr] = val;
  1979. ctxt->space = &ctxt->spaceTab[ctxt->spaceNr];
  1980. return(ctxt->spaceNr++);
  1981. }
  1982. static int spacePop(xmlParserCtxtPtr ctxt) {
  1983. int ret;
  1984. if (ctxt->spaceNr <= 0) return(0);
  1985. ctxt->spaceNr--;
  1986. if (ctxt->spaceNr > 0)
  1987. ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1];
  1988. else
  1989. ctxt->space = &ctxt->spaceTab[0];
  1990. ret = ctxt->spaceTab[ctxt->spaceNr];
  1991. ctxt->spaceTab[ctxt->spaceNr] = -1;
  1992. return(ret);
  1993. }
  1994. /*
  1995. * Macros for accessing the content. Those should be used only by the parser,
  1996. * and not exported.
  1997. *
  1998. * Dirty macros, i.e. one often need to make assumption on the context to
  1999. * use them
  2000. *
  2001. * CUR_PTR return the current pointer to the xmlChar to be parsed.
  2002. * To be used with extreme caution since operations consuming
  2003. * characters may move the input buffer to a different location !
  2004. * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
  2005. * This should be used internally by the parser
  2006. * only to compare to ASCII values otherwise it would break when
  2007. * running with UTF-8 encoding.
  2008. * RAW same as CUR but in the input buffer, bypass any token
  2009. * extraction that may have been done
  2010. * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
  2011. * to compare on ASCII based substring.
  2012. * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
  2013. * strings without newlines within the parser.
  2014. * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII
  2015. * defined char within the parser.
  2016. * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
  2017. *
  2018. * NEXT Skip to the next character, this does the proper decoding
  2019. * in UTF-8 mode. It also pop-up unfinished entities on the fly.
  2020. * NEXTL(l) Skip the current unicode character of l xmlChars long.
  2021. * CUR_CHAR(l) returns the current unicode character (int), set l
  2022. * to the number of xmlChars used for the encoding [0-5].
  2023. * CUR_SCHAR same but operate on a string instead of the context
  2024. * COPY_BUF copy the current unicode char to the target buffer, increment
  2025. * the index
  2026. * GROW, SHRINK handling of input buffers
  2027. */
  2028. #define RAW (*ctxt->input->cur)
  2029. #define CUR (*ctxt->input->cur)
  2030. #define NXT(val) ctxt->input->cur[(val)]
  2031. #define CUR_PTR ctxt->input->cur
  2032. #define BASE_PTR ctxt->input->base
  2033. #define CMP4( s, c1, c2, c3, c4 ) \
  2034. ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \
  2035. ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 )
  2036. #define CMP5( s, c1, c2, c3, c4, c5 ) \
  2037. ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 )
  2038. #define CMP6( s, c1, c2, c3, c4, c5, c6 ) \
  2039. ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 )
  2040. #define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \
  2041. ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 )
  2042. #define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \
  2043. ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 )
  2044. #define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \
  2045. ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \
  2046. ((unsigned char *) s)[ 8 ] == c9 )
  2047. #define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \
  2048. ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \
  2049. ((unsigned char *) s)[ 9 ] == c10 )
  2050. #define SKIP(val) do { \
  2051. ctxt->input->cur += (val),ctxt->input->col+=(val); \
  2052. if (*ctxt->input->cur == 0) \
  2053. xmlParserGrow(ctxt); \
  2054. } while (0)
  2055. #define SKIPL(val) do { \
  2056. int skipl; \
  2057. for(skipl=0; skipl<val; skipl++) { \
  2058. if (*(ctxt->input->cur) == '\n') { \
  2059. ctxt->input->line++; ctxt->input->col = 1; \
  2060. } else ctxt->input->col++; \
  2061. ctxt->input->cur++; \
  2062. } \
  2063. if (*ctxt->input->cur == 0) \
  2064. xmlParserGrow(ctxt); \
  2065. } while (0)
  2066. /* Don't shrink push parser buffer. */
  2067. #define SHRINK \
  2068. if (((ctxt->progressive == 0) || (ctxt->inputNr > 1)) && \
  2069. (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
  2070. (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
  2071. xmlParserShrink(ctxt);
  2072. #define GROW if (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK) \
  2073. xmlParserGrow(ctxt);
  2074. #define SKIP_BLANKS xmlSkipBlankChars(ctxt)
  2075. #define NEXT xmlNextChar(ctxt)
  2076. #define NEXT1 { \
  2077. ctxt->input->col++; \
  2078. ctxt->input->cur++; \
  2079. if (*ctxt->input->cur == 0) \
  2080. xmlParserGrow(ctxt); \
  2081. }
  2082. #define NEXTL(l) do { \
  2083. if (*(ctxt->input->cur) == '\n') { \
  2084. ctxt->input->line++; ctxt->input->col = 1; \
  2085. } else ctxt->input->col++; \
  2086. ctxt->input->cur += l; \
  2087. } while (0)
  2088. #define CUR_CHAR(l) xmlCurrentChar(ctxt, &l)
  2089. #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
  2090. #define COPY_BUF(b, i, v) \
  2091. if (v < 0x80) b[i++] = v; \
  2092. else i += xmlCopyCharMultiByte(&b[i],v)
  2093. /**
  2094. * xmlSkipBlankChars:
  2095. * @ctxt: the XML parser context
  2096. *
  2097. * DEPRECATED: Internal function, do not use.
  2098. *
  2099. * skip all blanks character found at that point in the input streams.
  2100. * It pops up finished entities in the process if allowable at that point.
  2101. *
  2102. * Returns the number of space chars skipped
  2103. */
  2104. int
  2105. xmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
  2106. int res = 0;
  2107. /*
  2108. * It's Okay to use CUR/NEXT here since all the blanks are on
  2109. * the ASCII range.
  2110. */
  2111. if (((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) ||
  2112. (ctxt->instate == XML_PARSER_START)) {
  2113. const xmlChar *cur;
  2114. /*
  2115. * if we are in the document content, go really fast
  2116. */
  2117. cur = ctxt->input->cur;
  2118. while (IS_BLANK_CH(*cur)) {
  2119. if (*cur == '\n') {
  2120. ctxt->input->line++; ctxt->input->col = 1;
  2121. } else {
  2122. ctxt->input->col++;
  2123. }
  2124. cur++;
  2125. if (res < INT_MAX)
  2126. res++;
  2127. if (*cur == 0) {
  2128. ctxt->input->cur = cur;
  2129. xmlParserGrow(ctxt);
  2130. cur = ctxt->input->cur;
  2131. }
  2132. }
  2133. ctxt->input->cur = cur;
  2134. } else {
  2135. int expandPE = ((ctxt->external != 0) || (ctxt->inputNr != 1));
  2136. while (ctxt->instate != XML_PARSER_EOF) {
  2137. if (IS_BLANK_CH(CUR)) { /* CHECKED tstblanks.xml */
  2138. NEXT;
  2139. } else if (CUR == '%') {
  2140. /*
  2141. * Need to handle support of entities branching here
  2142. */
  2143. if ((expandPE == 0) || (IS_BLANK_CH(NXT(1))) || (NXT(1) == 0))
  2144. break;
  2145. xmlParsePEReference(ctxt);
  2146. } else if (CUR == 0) {
  2147. unsigned long consumed;
  2148. xmlEntityPtr ent;
  2149. if (ctxt->inputNr <= 1)
  2150. break;
  2151. consumed = ctxt->input->consumed;
  2152. xmlSaturatedAddSizeT(&consumed,
  2153. ctxt->input->cur - ctxt->input->base);
  2154. /*
  2155. * Add to sizeentities when parsing an external entity
  2156. * for the first time.
  2157. */
  2158. ent = ctxt->input->entity;
  2159. if ((ent->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
  2160. ((ent->flags & XML_ENT_PARSED) == 0)) {
  2161. ent->flags |= XML_ENT_PARSED;
  2162. xmlSaturatedAdd(&ctxt->sizeentities, consumed);
  2163. }
  2164. xmlParserEntityCheck(ctxt, consumed);
  2165. xmlPopInput(ctxt);
  2166. } else {
  2167. break;
  2168. }
  2169. /*
  2170. * Also increase the counter when entering or exiting a PERef.
  2171. * The spec says: "When a parameter-entity reference is recognized
  2172. * in the DTD and included, its replacement text MUST be enlarged
  2173. * by the attachment of one leading and one following space (#x20)
  2174. * character."
  2175. */
  2176. if (res < INT_MAX)
  2177. res++;
  2178. }
  2179. }
  2180. return(res);
  2181. }
  2182. /************************************************************************
  2183. * *
  2184. * Commodity functions to handle entities *
  2185. * *
  2186. ************************************************************************/
  2187. /**
  2188. * xmlPopInput:
  2189. * @ctxt: an XML parser context
  2190. *
  2191. * xmlPopInput: the current input pointed by ctxt->input came to an end
  2192. * pop it and return the next char.
  2193. *
  2194. * Returns the current xmlChar in the parser context
  2195. */
  2196. xmlChar
  2197. xmlPopInput(xmlParserCtxtPtr ctxt) {
  2198. xmlParserInputPtr input;
  2199. if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0);
  2200. if (xmlParserDebugEntities)
  2201. xmlGenericError(xmlGenericErrorContext,
  2202. "Popping input %d\n", ctxt->inputNr);
  2203. if ((ctxt->inputNr > 1) && (ctxt->inSubset == 0) &&
  2204. (ctxt->instate != XML_PARSER_EOF))
  2205. xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
  2206. "Unfinished entity outside the DTD");
  2207. input = inputPop(ctxt);
  2208. if (input->entity != NULL)
  2209. input->entity->flags &= ~XML_ENT_EXPANDING;
  2210. xmlFreeInputStream(input);
  2211. if (*ctxt->input->cur == 0)
  2212. xmlParserGrow(ctxt);
  2213. return(CUR);
  2214. }
  2215. /**
  2216. * xmlPushInput:
  2217. * @ctxt: an XML parser context
  2218. * @input: an XML parser input fragment (entity, XML fragment ...).
  2219. *
  2220. * xmlPushInput: switch to a new input stream which is stacked on top
  2221. * of the previous one(s).
  2222. * Returns -1 in case of error or the index in the input stack
  2223. */
  2224. int
  2225. xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) {
  2226. int ret;
  2227. if (input == NULL) return(-1);
  2228. if (xmlParserDebugEntities) {
  2229. if ((ctxt->input != NULL) && (ctxt->input->filename))
  2230. xmlGenericError(xmlGenericErrorContext,
  2231. "%s(%d): ", ctxt->input->filename,
  2232. ctxt->input->line);
  2233. xmlGenericError(xmlGenericErrorContext,
  2234. "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur);
  2235. }
  2236. if (((ctxt->inputNr > 40) && ((ctxt->options & XML_PARSE_HUGE) == 0)) ||
  2237. (ctxt->inputNr > 100)) {
  2238. xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
  2239. while (ctxt->inputNr > 1)
  2240. xmlFreeInputStream(inputPop(ctxt));
  2241. return(-1);
  2242. }
  2243. ret = inputPush(ctxt, input);
  2244. if (ctxt->instate == XML_PARSER_EOF)
  2245. return(-1);
  2246. GROW;
  2247. return(ret);
  2248. }
  2249. /**
  2250. * xmlParseCharRef:
  2251. * @ctxt: an XML parser context
  2252. *
  2253. * DEPRECATED: Internal function, don't use.
  2254. *
  2255. * Parse a numeric character reference. Always consumes '&'.
  2256. *
  2257. * [66] CharRef ::= '&#' [0-9]+ ';' |
  2258. * '&#x' [0-9a-fA-F]+ ';'
  2259. *
  2260. * [ WFC: Legal Character ]
  2261. * Characters referred to using character references must match the
  2262. * production for Char.
  2263. *
  2264. * Returns the value parsed (as an int), 0 in case of error
  2265. */
  2266. int
  2267. xmlParseCharRef(xmlParserCtxtPtr ctxt) {
  2268. int val = 0;
  2269. int count = 0;
  2270. /*
  2271. * Using RAW/CUR/NEXT is okay since we are working on ASCII range here
  2272. */
  2273. if ((RAW == '&') && (NXT(1) == '#') &&
  2274. (NXT(2) == 'x')) {
  2275. SKIP(3);
  2276. GROW;
  2277. while (RAW != ';') { /* loop blocked by count */
  2278. if (count++ > 20) {
  2279. count = 0;
  2280. GROW;
  2281. if (ctxt->instate == XML_PARSER_EOF)
  2282. return(0);
  2283. }
  2284. if ((RAW >= '0') && (RAW <= '9'))
  2285. val = val * 16 + (CUR - '0');
  2286. else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20))
  2287. val = val * 16 + (CUR - 'a') + 10;
  2288. else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20))
  2289. val = val * 16 + (CUR - 'A') + 10;
  2290. else {
  2291. xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
  2292. val = 0;
  2293. break;
  2294. }
  2295. if (val > 0x110000)
  2296. val = 0x110000;
  2297. NEXT;
  2298. count++;
  2299. }
  2300. if (RAW == ';') {
  2301. /* on purpose to avoid reentrancy problems with NEXT and SKIP */
  2302. ctxt->input->col++;
  2303. ctxt->input->cur++;
  2304. }
  2305. } else if ((RAW == '&') && (NXT(1) == '#')) {
  2306. SKIP(2);
  2307. GROW;
  2308. while (RAW != ';') { /* loop blocked by count */
  2309. if (count++ > 20) {
  2310. count = 0;
  2311. GROW;
  2312. if (ctxt->instate == XML_PARSER_EOF)
  2313. return(0);
  2314. }
  2315. if ((RAW >= '0') && (RAW <= '9'))
  2316. val = val * 10 + (CUR - '0');
  2317. else {
  2318. xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
  2319. val = 0;
  2320. break;
  2321. }
  2322. if (val > 0x110000)
  2323. val = 0x110000;
  2324. NEXT;
  2325. count++;
  2326. }
  2327. if (RAW == ';') {
  2328. /* on purpose to avoid reentrancy problems with NEXT and SKIP */
  2329. ctxt->input->col++;
  2330. ctxt->input->cur++;
  2331. }
  2332. } else {
  2333. if (RAW == '&')
  2334. SKIP(1);
  2335. xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
  2336. }
  2337. /*
  2338. * [ WFC: Legal Character ]
  2339. * Characters referred to using character references must match the
  2340. * production for Char.
  2341. */
  2342. if (val >= 0x110000) {
  2343. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  2344. "xmlParseCharRef: character reference out of bounds\n",
  2345. val);
  2346. } else if (IS_CHAR(val)) {
  2347. return(val);
  2348. } else {
  2349. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  2350. "xmlParseCharRef: invalid xmlChar value %d\n",
  2351. val);
  2352. }
  2353. return(0);
  2354. }
  2355. /**
  2356. * xmlParseStringCharRef:
  2357. * @ctxt: an XML parser context
  2358. * @str: a pointer to an index in the string
  2359. *
  2360. * parse Reference declarations, variant parsing from a string rather
  2361. * than an an input flow.
  2362. *
  2363. * [66] CharRef ::= '&#' [0-9]+ ';' |
  2364. * '&#x' [0-9a-fA-F]+ ';'
  2365. *
  2366. * [ WFC: Legal Character ]
  2367. * Characters referred to using character references must match the
  2368. * production for Char.
  2369. *
  2370. * Returns the value parsed (as an int), 0 in case of error, str will be
  2371. * updated to the current value of the index
  2372. */
  2373. static int
  2374. xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) {
  2375. const xmlChar *ptr;
  2376. xmlChar cur;
  2377. int val = 0;
  2378. if ((str == NULL) || (*str == NULL)) return(0);
  2379. ptr = *str;
  2380. cur = *ptr;
  2381. if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) {
  2382. ptr += 3;
  2383. cur = *ptr;
  2384. while (cur != ';') { /* Non input consuming loop */
  2385. if ((cur >= '0') && (cur <= '9'))
  2386. val = val * 16 + (cur - '0');
  2387. else if ((cur >= 'a') && (cur <= 'f'))
  2388. val = val * 16 + (cur - 'a') + 10;
  2389. else if ((cur >= 'A') && (cur <= 'F'))
  2390. val = val * 16 + (cur - 'A') + 10;
  2391. else {
  2392. xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
  2393. val = 0;
  2394. break;
  2395. }
  2396. if (val > 0x110000)
  2397. val = 0x110000;
  2398. ptr++;
  2399. cur = *ptr;
  2400. }
  2401. if (cur == ';')
  2402. ptr++;
  2403. } else if ((cur == '&') && (ptr[1] == '#')){
  2404. ptr += 2;
  2405. cur = *ptr;
  2406. while (cur != ';') { /* Non input consuming loops */
  2407. if ((cur >= '0') && (cur <= '9'))
  2408. val = val * 10 + (cur - '0');
  2409. else {
  2410. xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
  2411. val = 0;
  2412. break;
  2413. }
  2414. if (val > 0x110000)
  2415. val = 0x110000;
  2416. ptr++;
  2417. cur = *ptr;
  2418. }
  2419. if (cur == ';')
  2420. ptr++;
  2421. } else {
  2422. xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
  2423. return(0);
  2424. }
  2425. *str = ptr;
  2426. /*
  2427. * [ WFC: Legal Character ]
  2428. * Characters referred to using character references must match the
  2429. * production for Char.
  2430. */
  2431. if (val >= 0x110000) {
  2432. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  2433. "xmlParseStringCharRef: character reference out of bounds\n",
  2434. val);
  2435. } else if (IS_CHAR(val)) {
  2436. return(val);
  2437. } else {
  2438. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  2439. "xmlParseStringCharRef: invalid xmlChar value %d\n",
  2440. val);
  2441. }
  2442. return(0);
  2443. }
  2444. /**
  2445. * xmlParserHandlePEReference:
  2446. * @ctxt: the parser context
  2447. *
  2448. * DEPRECATED: Internal function, do not use.
  2449. *
  2450. * [69] PEReference ::= '%' Name ';'
  2451. *
  2452. * [ WFC: No Recursion ]
  2453. * A parsed entity must not contain a recursive
  2454. * reference to itself, either directly or indirectly.
  2455. *
  2456. * [ WFC: Entity Declared ]
  2457. * In a document without any DTD, a document with only an internal DTD
  2458. * subset which contains no parameter entity references, or a document
  2459. * with "standalone='yes'", ... ... The declaration of a parameter
  2460. * entity must precede any reference to it...
  2461. *
  2462. * [ VC: Entity Declared ]
  2463. * In a document with an external subset or external parameter entities
  2464. * with "standalone='no'", ... ... The declaration of a parameter entity
  2465. * must precede any reference to it...
  2466. *
  2467. * [ WFC: In DTD ]
  2468. * Parameter-entity references may only appear in the DTD.
  2469. * NOTE: misleading but this is handled.
  2470. *
  2471. * A PEReference may have been detected in the current input stream
  2472. * the handling is done accordingly to
  2473. * http://www.w3.org/TR/REC-xml#entproc
  2474. * i.e.
  2475. * - Included in literal in entity values
  2476. * - Included as Parameter Entity reference within DTDs
  2477. */
  2478. void
  2479. xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) {
  2480. switch(ctxt->instate) {
  2481. case XML_PARSER_CDATA_SECTION:
  2482. return;
  2483. case XML_PARSER_COMMENT:
  2484. return;
  2485. case XML_PARSER_START_TAG:
  2486. return;
  2487. case XML_PARSER_END_TAG:
  2488. return;
  2489. case XML_PARSER_EOF:
  2490. xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL);
  2491. return;
  2492. case XML_PARSER_PROLOG:
  2493. case XML_PARSER_START:
  2494. case XML_PARSER_XML_DECL:
  2495. case XML_PARSER_MISC:
  2496. xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL);
  2497. return;
  2498. case XML_PARSER_ENTITY_DECL:
  2499. case XML_PARSER_CONTENT:
  2500. case XML_PARSER_ATTRIBUTE_VALUE:
  2501. case XML_PARSER_PI:
  2502. case XML_PARSER_SYSTEM_LITERAL:
  2503. case XML_PARSER_PUBLIC_LITERAL:
  2504. /* we just ignore it there */
  2505. return;
  2506. case XML_PARSER_EPILOG:
  2507. xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL);
  2508. return;
  2509. case XML_PARSER_ENTITY_VALUE:
  2510. /*
  2511. * NOTE: in the case of entity values, we don't do the
  2512. * substitution here since we need the literal
  2513. * entity value to be able to save the internal
  2514. * subset of the document.
  2515. * This will be handled by xmlStringDecodeEntities
  2516. */
  2517. return;
  2518. case XML_PARSER_DTD:
  2519. /*
  2520. * [WFC: Well-Formedness Constraint: PEs in Internal Subset]
  2521. * In the internal DTD subset, parameter-entity references
  2522. * can occur only where markup declarations can occur, not
  2523. * within markup declarations.
  2524. * In that case this is handled in xmlParseMarkupDecl
  2525. */
  2526. if ((ctxt->external == 0) && (ctxt->inputNr == 1))
  2527. return;
  2528. if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0)
  2529. return;
  2530. break;
  2531. case XML_PARSER_IGNORE:
  2532. return;
  2533. }
  2534. xmlParsePEReference(ctxt);
  2535. }
  2536. /*
  2537. * Macro used to grow the current buffer.
  2538. * buffer##_size is expected to be a size_t
  2539. * mem_error: is expected to handle memory allocation failures
  2540. */
  2541. #define growBuffer(buffer, n) { \
  2542. xmlChar *tmp; \
  2543. size_t new_size = buffer##_size * 2 + n; \
  2544. if (new_size < buffer##_size) goto mem_error; \
  2545. tmp = (xmlChar *) xmlRealloc(buffer, new_size); \
  2546. if (tmp == NULL) goto mem_error; \
  2547. buffer = tmp; \
  2548. buffer##_size = new_size; \
  2549. }
  2550. /**
  2551. * xmlStringDecodeEntitiesInt:
  2552. * @ctxt: the parser context
  2553. * @str: the input string
  2554. * @len: the string length
  2555. * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
  2556. * @end: an end marker xmlChar, 0 if none
  2557. * @end2: an end marker xmlChar, 0 if none
  2558. * @end3: an end marker xmlChar, 0 if none
  2559. * @check: whether to perform entity checks
  2560. */
  2561. static xmlChar *
  2562. xmlStringDecodeEntitiesInt(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
  2563. int what, xmlChar end, xmlChar end2, xmlChar end3,
  2564. int check) {
  2565. xmlChar *buffer = NULL;
  2566. size_t buffer_size = 0;
  2567. size_t nbchars = 0;
  2568. xmlChar *current = NULL;
  2569. xmlChar *rep = NULL;
  2570. const xmlChar *last;
  2571. xmlEntityPtr ent;
  2572. int c,l;
  2573. if (str == NULL)
  2574. return(NULL);
  2575. last = str + len;
  2576. if (((ctxt->depth > 40) &&
  2577. ((ctxt->options & XML_PARSE_HUGE) == 0)) ||
  2578. (ctxt->depth > 100)) {
  2579. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_LOOP,
  2580. "Maximum entity nesting depth exceeded");
  2581. return(NULL);
  2582. }
  2583. /*
  2584. * allocate a translation buffer.
  2585. */
  2586. buffer_size = XML_PARSER_BIG_BUFFER_SIZE;
  2587. buffer = (xmlChar *) xmlMallocAtomic(buffer_size);
  2588. if (buffer == NULL) goto mem_error;
  2589. /*
  2590. * OK loop until we reach one of the ending char or a size limit.
  2591. * we are operating on already parsed values.
  2592. */
  2593. if (str < last)
  2594. c = CUR_SCHAR(str, l);
  2595. else
  2596. c = 0;
  2597. while ((c != 0) && (c != end) && /* non input consuming loop */
  2598. (c != end2) && (c != end3) &&
  2599. (ctxt->instate != XML_PARSER_EOF)) {
  2600. if (c == 0) break;
  2601. if ((c == '&') && (str[1] == '#')) {
  2602. int val = xmlParseStringCharRef(ctxt, &str);
  2603. if (val == 0)
  2604. goto int_error;
  2605. COPY_BUF(buffer, nbchars, val);
  2606. if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) {
  2607. growBuffer(buffer, XML_PARSER_BUFFER_SIZE);
  2608. }
  2609. } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) {
  2610. if (xmlParserDebugEntities)
  2611. xmlGenericError(xmlGenericErrorContext,
  2612. "String decoding Entity Reference: %.30s\n",
  2613. str);
  2614. ent = xmlParseStringEntityRef(ctxt, &str);
  2615. if ((ent != NULL) &&
  2616. (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
  2617. if (ent->content != NULL) {
  2618. COPY_BUF(buffer, nbchars, ent->content[0]);
  2619. if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) {
  2620. growBuffer(buffer, XML_PARSER_BUFFER_SIZE);
  2621. }
  2622. } else {
  2623. xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
  2624. "predefined entity has no content\n");
  2625. goto int_error;
  2626. }
  2627. } else if ((ent != NULL) && (ent->content != NULL)) {
  2628. if ((check) && (xmlParserEntityCheck(ctxt, ent->length)))
  2629. goto int_error;
  2630. if (ent->flags & XML_ENT_EXPANDING) {
  2631. xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
  2632. xmlHaltParser(ctxt);
  2633. ent->content[0] = 0;
  2634. goto int_error;
  2635. }
  2636. ent->flags |= XML_ENT_EXPANDING;
  2637. ctxt->depth++;
  2638. rep = xmlStringDecodeEntitiesInt(ctxt, ent->content,
  2639. ent->length, what, 0, 0, 0, check);
  2640. ctxt->depth--;
  2641. ent->flags &= ~XML_ENT_EXPANDING;
  2642. if (rep == NULL) {
  2643. ent->content[0] = 0;
  2644. goto int_error;
  2645. }
  2646. current = rep;
  2647. while (*current != 0) { /* non input consuming loop */
  2648. buffer[nbchars++] = *current++;
  2649. if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) {
  2650. growBuffer(buffer, XML_PARSER_BUFFER_SIZE);
  2651. }
  2652. }
  2653. xmlFree(rep);
  2654. rep = NULL;
  2655. } else if (ent != NULL) {
  2656. int i = xmlStrlen(ent->name);
  2657. const xmlChar *cur = ent->name;
  2658. buffer[nbchars++] = '&';
  2659. if (nbchars + i + XML_PARSER_BUFFER_SIZE > buffer_size) {
  2660. growBuffer(buffer, i + XML_PARSER_BUFFER_SIZE);
  2661. }
  2662. for (;i > 0;i--)
  2663. buffer[nbchars++] = *cur++;
  2664. buffer[nbchars++] = ';';
  2665. }
  2666. } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) {
  2667. if (xmlParserDebugEntities)
  2668. xmlGenericError(xmlGenericErrorContext,
  2669. "String decoding PE Reference: %.30s\n", str);
  2670. ent = xmlParseStringPEReference(ctxt, &str);
  2671. if (ent != NULL) {
  2672. if (ent->content == NULL) {
  2673. /*
  2674. * Note: external parsed entities will not be loaded,
  2675. * it is not required for a non-validating parser to
  2676. * complete external PEReferences coming from the
  2677. * internal subset
  2678. */
  2679. if (((ctxt->options & XML_PARSE_NOENT) != 0) ||
  2680. ((ctxt->options & XML_PARSE_DTDVALID) != 0) ||
  2681. (ctxt->validate != 0)) {
  2682. xmlLoadEntityContent(ctxt, ent);
  2683. } else {
  2684. xmlWarningMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
  2685. "not validating will not read content for PE entity %s\n",
  2686. ent->name, NULL);
  2687. }
  2688. }
  2689. if ((check) && (xmlParserEntityCheck(ctxt, ent->length)))
  2690. goto int_error;
  2691. if (ent->flags & XML_ENT_EXPANDING) {
  2692. xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
  2693. xmlHaltParser(ctxt);
  2694. if (ent->content != NULL)
  2695. ent->content[0] = 0;
  2696. goto int_error;
  2697. }
  2698. ent->flags |= XML_ENT_EXPANDING;
  2699. ctxt->depth++;
  2700. rep = xmlStringDecodeEntitiesInt(ctxt, ent->content,
  2701. ent->length, what, 0, 0, 0, check);
  2702. ctxt->depth--;
  2703. ent->flags &= ~XML_ENT_EXPANDING;
  2704. if (rep == NULL) {
  2705. if (ent->content != NULL)
  2706. ent->content[0] = 0;
  2707. goto int_error;
  2708. }
  2709. current = rep;
  2710. while (*current != 0) { /* non input consuming loop */
  2711. buffer[nbchars++] = *current++;
  2712. if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) {
  2713. growBuffer(buffer, XML_PARSER_BUFFER_SIZE);
  2714. }
  2715. }
  2716. xmlFree(rep);
  2717. rep = NULL;
  2718. }
  2719. } else {
  2720. COPY_BUF(buffer, nbchars, c);
  2721. str += l;
  2722. if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) {
  2723. growBuffer(buffer, XML_PARSER_BUFFER_SIZE);
  2724. }
  2725. }
  2726. if (str < last)
  2727. c = CUR_SCHAR(str, l);
  2728. else
  2729. c = 0;
  2730. }
  2731. buffer[nbchars] = 0;
  2732. return(buffer);
  2733. mem_error:
  2734. xmlErrMemory(ctxt, NULL);
  2735. int_error:
  2736. if (rep != NULL)
  2737. xmlFree(rep);
  2738. if (buffer != NULL)
  2739. xmlFree(buffer);
  2740. return(NULL);
  2741. }
  2742. /**
  2743. * xmlStringLenDecodeEntities:
  2744. * @ctxt: the parser context
  2745. * @str: the input string
  2746. * @len: the string length
  2747. * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
  2748. * @end: an end marker xmlChar, 0 if none
  2749. * @end2: an end marker xmlChar, 0 if none
  2750. * @end3: an end marker xmlChar, 0 if none
  2751. *
  2752. * DEPRECATED: Internal function, don't use.
  2753. *
  2754. * Takes a entity string content and process to do the adequate substitutions.
  2755. *
  2756. * [67] Reference ::= EntityRef | CharRef
  2757. *
  2758. * [69] PEReference ::= '%' Name ';'
  2759. *
  2760. * Returns A newly allocated string with the substitution done. The caller
  2761. * must deallocate it !
  2762. */
  2763. xmlChar *
  2764. xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
  2765. int what, xmlChar end, xmlChar end2,
  2766. xmlChar end3) {
  2767. if ((ctxt == NULL) || (str == NULL) || (len < 0))
  2768. return(NULL);
  2769. return(xmlStringDecodeEntitiesInt(ctxt, str, len, what,
  2770. end, end2, end3, 0));
  2771. }
  2772. /**
  2773. * xmlStringDecodeEntities:
  2774. * @ctxt: the parser context
  2775. * @str: the input string
  2776. * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
  2777. * @end: an end marker xmlChar, 0 if none
  2778. * @end2: an end marker xmlChar, 0 if none
  2779. * @end3: an end marker xmlChar, 0 if none
  2780. *
  2781. * DEPRECATED: Internal function, don't use.
  2782. *
  2783. * Takes a entity string content and process to do the adequate substitutions.
  2784. *
  2785. * [67] Reference ::= EntityRef | CharRef
  2786. *
  2787. * [69] PEReference ::= '%' Name ';'
  2788. *
  2789. * Returns A newly allocated string with the substitution done. The caller
  2790. * must deallocate it !
  2791. */
  2792. xmlChar *
  2793. xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what,
  2794. xmlChar end, xmlChar end2, xmlChar end3) {
  2795. if ((ctxt == NULL) || (str == NULL)) return(NULL);
  2796. return(xmlStringDecodeEntitiesInt(ctxt, str, xmlStrlen(str), what,
  2797. end, end2, end3, 0));
  2798. }
  2799. /************************************************************************
  2800. * *
  2801. * Commodity functions, cleanup needed ? *
  2802. * *
  2803. ************************************************************************/
  2804. /**
  2805. * areBlanks:
  2806. * @ctxt: an XML parser context
  2807. * @str: a xmlChar *
  2808. * @len: the size of @str
  2809. * @blank_chars: we know the chars are blanks
  2810. *
  2811. * Is this a sequence of blank chars that one can ignore ?
  2812. *
  2813. * Returns 1 if ignorable 0 otherwise.
  2814. */
  2815. static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
  2816. int blank_chars) {
  2817. int i, ret;
  2818. xmlNodePtr lastChild;
  2819. /*
  2820. * Don't spend time trying to differentiate them, the same callback is
  2821. * used !
  2822. */
  2823. if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters)
  2824. return(0);
  2825. /*
  2826. * Check for xml:space value.
  2827. */
  2828. if ((ctxt->space == NULL) || (*(ctxt->space) == 1) ||
  2829. (*(ctxt->space) == -2))
  2830. return(0);
  2831. /*
  2832. * Check that the string is made of blanks
  2833. */
  2834. if (blank_chars == 0) {
  2835. for (i = 0;i < len;i++)
  2836. if (!(IS_BLANK_CH(str[i]))) return(0);
  2837. }
  2838. /*
  2839. * Look if the element is mixed content in the DTD if available
  2840. */
  2841. if (ctxt->node == NULL) return(0);
  2842. if (ctxt->myDoc != NULL) {
  2843. ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name);
  2844. if (ret == 0) return(1);
  2845. if (ret == 1) return(0);
  2846. }
  2847. /*
  2848. * Otherwise, heuristic :-\
  2849. */
  2850. if ((RAW != '<') && (RAW != 0xD)) return(0);
  2851. if ((ctxt->node->children == NULL) &&
  2852. (RAW == '<') && (NXT(1) == '/')) return(0);
  2853. lastChild = xmlGetLastChild(ctxt->node);
  2854. if (lastChild == NULL) {
  2855. if ((ctxt->node->type != XML_ELEMENT_NODE) &&
  2856. (ctxt->node->content != NULL)) return(0);
  2857. } else if (xmlNodeIsText(lastChild))
  2858. return(0);
  2859. else if ((ctxt->node->children != NULL) &&
  2860. (xmlNodeIsText(ctxt->node->children)))
  2861. return(0);
  2862. return(1);
  2863. }
  2864. /************************************************************************
  2865. * *
  2866. * Extra stuff for namespace support *
  2867. * Relates to http://www.w3.org/TR/WD-xml-names *
  2868. * *
  2869. ************************************************************************/
  2870. /**
  2871. * xmlSplitQName:
  2872. * @ctxt: an XML parser context
  2873. * @name: an XML parser context
  2874. * @prefix: a xmlChar **
  2875. *
  2876. * parse an UTF8 encoded XML qualified name string
  2877. *
  2878. * [NS 5] QName ::= (Prefix ':')? LocalPart
  2879. *
  2880. * [NS 6] Prefix ::= NCName
  2881. *
  2882. * [NS 7] LocalPart ::= NCName
  2883. *
  2884. * Returns the local part, and prefix is updated
  2885. * to get the Prefix if any.
  2886. */
  2887. xmlChar *
  2888. xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) {
  2889. xmlChar buf[XML_MAX_NAMELEN + 5];
  2890. xmlChar *buffer = NULL;
  2891. int len = 0;
  2892. int max = XML_MAX_NAMELEN;
  2893. xmlChar *ret = NULL;
  2894. const xmlChar *cur = name;
  2895. int c;
  2896. if (prefix == NULL) return(NULL);
  2897. *prefix = NULL;
  2898. if (cur == NULL) return(NULL);
  2899. #ifndef XML_XML_NAMESPACE
  2900. /* xml: prefix is not really a namespace */
  2901. if ((cur[0] == 'x') && (cur[1] == 'm') &&
  2902. (cur[2] == 'l') && (cur[3] == ':'))
  2903. return(xmlStrdup(name));
  2904. #endif
  2905. /* nasty but well=formed */
  2906. if (cur[0] == ':')
  2907. return(xmlStrdup(name));
  2908. c = *cur++;
  2909. while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */
  2910. buf[len++] = c;
  2911. c = *cur++;
  2912. }
  2913. if (len >= max) {
  2914. /*
  2915. * Okay someone managed to make a huge name, so he's ready to pay
  2916. * for the processing speed.
  2917. */
  2918. max = len * 2;
  2919. buffer = (xmlChar *) xmlMallocAtomic(max);
  2920. if (buffer == NULL) {
  2921. xmlErrMemory(ctxt, NULL);
  2922. return(NULL);
  2923. }
  2924. memcpy(buffer, buf, len);
  2925. while ((c != 0) && (c != ':')) { /* tested bigname.xml */
  2926. if (len + 10 > max) {
  2927. xmlChar *tmp;
  2928. max *= 2;
  2929. tmp = (xmlChar *) xmlRealloc(buffer, max);
  2930. if (tmp == NULL) {
  2931. xmlFree(buffer);
  2932. xmlErrMemory(ctxt, NULL);
  2933. return(NULL);
  2934. }
  2935. buffer = tmp;
  2936. }
  2937. buffer[len++] = c;
  2938. c = *cur++;
  2939. }
  2940. buffer[len] = 0;
  2941. }
  2942. if ((c == ':') && (*cur == 0)) {
  2943. if (buffer != NULL)
  2944. xmlFree(buffer);
  2945. *prefix = NULL;
  2946. return(xmlStrdup(name));
  2947. }
  2948. if (buffer == NULL)
  2949. ret = xmlStrndup(buf, len);
  2950. else {
  2951. ret = buffer;
  2952. buffer = NULL;
  2953. max = XML_MAX_NAMELEN;
  2954. }
  2955. if (c == ':') {
  2956. c = *cur;
  2957. *prefix = ret;
  2958. if (c == 0) {
  2959. return(xmlStrndup(BAD_CAST "", 0));
  2960. }
  2961. len = 0;
  2962. /*
  2963. * Check that the first character is proper to start
  2964. * a new name
  2965. */
  2966. if (!(((c >= 0x61) && (c <= 0x7A)) ||
  2967. ((c >= 0x41) && (c <= 0x5A)) ||
  2968. (c == '_') || (c == ':'))) {
  2969. int l;
  2970. int first = CUR_SCHAR(cur, l);
  2971. if (!IS_LETTER(first) && (first != '_')) {
  2972. xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME,
  2973. "Name %s is not XML Namespace compliant\n",
  2974. name);
  2975. }
  2976. }
  2977. cur++;
  2978. while ((c != 0) && (len < max)) { /* tested bigname2.xml */
  2979. buf[len++] = c;
  2980. c = *cur++;
  2981. }
  2982. if (len >= max) {
  2983. /*
  2984. * Okay someone managed to make a huge name, so he's ready to pay
  2985. * for the processing speed.
  2986. */
  2987. max = len * 2;
  2988. buffer = (xmlChar *) xmlMallocAtomic(max);
  2989. if (buffer == NULL) {
  2990. xmlErrMemory(ctxt, NULL);
  2991. return(NULL);
  2992. }
  2993. memcpy(buffer, buf, len);
  2994. while (c != 0) { /* tested bigname2.xml */
  2995. if (len + 10 > max) {
  2996. xmlChar *tmp;
  2997. max *= 2;
  2998. tmp = (xmlChar *) xmlRealloc(buffer, max);
  2999. if (tmp == NULL) {
  3000. xmlErrMemory(ctxt, NULL);
  3001. xmlFree(buffer);
  3002. return(NULL);
  3003. }
  3004. buffer = tmp;
  3005. }
  3006. buffer[len++] = c;
  3007. c = *cur++;
  3008. }
  3009. buffer[len] = 0;
  3010. }
  3011. if (buffer == NULL)
  3012. ret = xmlStrndup(buf, len);
  3013. else {
  3014. ret = buffer;
  3015. }
  3016. }
  3017. return(ret);
  3018. }
  3019. /************************************************************************
  3020. * *
  3021. * The parser itself *
  3022. * Relates to http://www.w3.org/TR/REC-xml *
  3023. * *
  3024. ************************************************************************/
  3025. /************************************************************************
  3026. * *
  3027. * Routines to parse Name, NCName and NmToken *
  3028. * *
  3029. ************************************************************************/
  3030. /*
  3031. * The two following functions are related to the change of accepted
  3032. * characters for Name and NmToken in the Revision 5 of XML-1.0
  3033. * They correspond to the modified production [4] and the new production [4a]
  3034. * changes in that revision. Also note that the macros used for the
  3035. * productions Letter, Digit, CombiningChar and Extender are not needed
  3036. * anymore.
  3037. * We still keep compatibility to pre-revision5 parsing semantic if the
  3038. * new XML_PARSE_OLD10 option is given to the parser.
  3039. */
  3040. static int
  3041. xmlIsNameStartChar(xmlParserCtxtPtr ctxt, int c) {
  3042. if ((ctxt->options & XML_PARSE_OLD10) == 0) {
  3043. /*
  3044. * Use the new checks of production [4] [4a] amd [5] of the
  3045. * Update 5 of XML-1.0
  3046. */
  3047. if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
  3048. (((c >= 'a') && (c <= 'z')) ||
  3049. ((c >= 'A') && (c <= 'Z')) ||
  3050. (c == '_') || (c == ':') ||
  3051. ((c >= 0xC0) && (c <= 0xD6)) ||
  3052. ((c >= 0xD8) && (c <= 0xF6)) ||
  3053. ((c >= 0xF8) && (c <= 0x2FF)) ||
  3054. ((c >= 0x370) && (c <= 0x37D)) ||
  3055. ((c >= 0x37F) && (c <= 0x1FFF)) ||
  3056. ((c >= 0x200C) && (c <= 0x200D)) ||
  3057. ((c >= 0x2070) && (c <= 0x218F)) ||
  3058. ((c >= 0x2C00) && (c <= 0x2FEF)) ||
  3059. ((c >= 0x3001) && (c <= 0xD7FF)) ||
  3060. ((c >= 0xF900) && (c <= 0xFDCF)) ||
  3061. ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
  3062. ((c >= 0x10000) && (c <= 0xEFFFF))))
  3063. return(1);
  3064. } else {
  3065. if (IS_LETTER(c) || (c == '_') || (c == ':'))
  3066. return(1);
  3067. }
  3068. return(0);
  3069. }
  3070. static int
  3071. xmlIsNameChar(xmlParserCtxtPtr ctxt, int c) {
  3072. if ((ctxt->options & XML_PARSE_OLD10) == 0) {
  3073. /*
  3074. * Use the new checks of production [4] [4a] amd [5] of the
  3075. * Update 5 of XML-1.0
  3076. */
  3077. if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
  3078. (((c >= 'a') && (c <= 'z')) ||
  3079. ((c >= 'A') && (c <= 'Z')) ||
  3080. ((c >= '0') && (c <= '9')) || /* !start */
  3081. (c == '_') || (c == ':') ||
  3082. (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
  3083. ((c >= 0xC0) && (c <= 0xD6)) ||
  3084. ((c >= 0xD8) && (c <= 0xF6)) ||
  3085. ((c >= 0xF8) && (c <= 0x2FF)) ||
  3086. ((c >= 0x300) && (c <= 0x36F)) || /* !start */
  3087. ((c >= 0x370) && (c <= 0x37D)) ||
  3088. ((c >= 0x37F) && (c <= 0x1FFF)) ||
  3089. ((c >= 0x200C) && (c <= 0x200D)) ||
  3090. ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
  3091. ((c >= 0x2070) && (c <= 0x218F)) ||
  3092. ((c >= 0x2C00) && (c <= 0x2FEF)) ||
  3093. ((c >= 0x3001) && (c <= 0xD7FF)) ||
  3094. ((c >= 0xF900) && (c <= 0xFDCF)) ||
  3095. ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
  3096. ((c >= 0x10000) && (c <= 0xEFFFF))))
  3097. return(1);
  3098. } else {
  3099. if ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
  3100. (c == '.') || (c == '-') ||
  3101. (c == '_') || (c == ':') ||
  3102. (IS_COMBINING(c)) ||
  3103. (IS_EXTENDER(c)))
  3104. return(1);
  3105. }
  3106. return(0);
  3107. }
  3108. static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt,
  3109. int *len, int *alloc, int normalize);
  3110. static const xmlChar *
  3111. xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
  3112. int len = 0, l;
  3113. int c;
  3114. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3115. XML_MAX_TEXT_LENGTH :
  3116. XML_MAX_NAME_LENGTH;
  3117. /*
  3118. * Handler for more complex cases
  3119. */
  3120. c = CUR_CHAR(l);
  3121. if ((ctxt->options & XML_PARSE_OLD10) == 0) {
  3122. /*
  3123. * Use the new checks of production [4] [4a] amd [5] of the
  3124. * Update 5 of XML-1.0
  3125. */
  3126. if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
  3127. (!(((c >= 'a') && (c <= 'z')) ||
  3128. ((c >= 'A') && (c <= 'Z')) ||
  3129. (c == '_') || (c == ':') ||
  3130. ((c >= 0xC0) && (c <= 0xD6)) ||
  3131. ((c >= 0xD8) && (c <= 0xF6)) ||
  3132. ((c >= 0xF8) && (c <= 0x2FF)) ||
  3133. ((c >= 0x370) && (c <= 0x37D)) ||
  3134. ((c >= 0x37F) && (c <= 0x1FFF)) ||
  3135. ((c >= 0x200C) && (c <= 0x200D)) ||
  3136. ((c >= 0x2070) && (c <= 0x218F)) ||
  3137. ((c >= 0x2C00) && (c <= 0x2FEF)) ||
  3138. ((c >= 0x3001) && (c <= 0xD7FF)) ||
  3139. ((c >= 0xF900) && (c <= 0xFDCF)) ||
  3140. ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
  3141. ((c >= 0x10000) && (c <= 0xEFFFF))))) {
  3142. return(NULL);
  3143. }
  3144. len += l;
  3145. NEXTL(l);
  3146. c = CUR_CHAR(l);
  3147. while ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
  3148. (((c >= 'a') && (c <= 'z')) ||
  3149. ((c >= 'A') && (c <= 'Z')) ||
  3150. ((c >= '0') && (c <= '9')) || /* !start */
  3151. (c == '_') || (c == ':') ||
  3152. (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
  3153. ((c >= 0xC0) && (c <= 0xD6)) ||
  3154. ((c >= 0xD8) && (c <= 0xF6)) ||
  3155. ((c >= 0xF8) && (c <= 0x2FF)) ||
  3156. ((c >= 0x300) && (c <= 0x36F)) || /* !start */
  3157. ((c >= 0x370) && (c <= 0x37D)) ||
  3158. ((c >= 0x37F) && (c <= 0x1FFF)) ||
  3159. ((c >= 0x200C) && (c <= 0x200D)) ||
  3160. ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
  3161. ((c >= 0x2070) && (c <= 0x218F)) ||
  3162. ((c >= 0x2C00) && (c <= 0x2FEF)) ||
  3163. ((c >= 0x3001) && (c <= 0xD7FF)) ||
  3164. ((c >= 0xF900) && (c <= 0xFDCF)) ||
  3165. ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
  3166. ((c >= 0x10000) && (c <= 0xEFFFF))
  3167. )) {
  3168. if (len <= INT_MAX - l)
  3169. len += l;
  3170. NEXTL(l);
  3171. c = CUR_CHAR(l);
  3172. }
  3173. } else {
  3174. if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
  3175. (!IS_LETTER(c) && (c != '_') &&
  3176. (c != ':'))) {
  3177. return(NULL);
  3178. }
  3179. len += l;
  3180. NEXTL(l);
  3181. c = CUR_CHAR(l);
  3182. while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
  3183. ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
  3184. (c == '.') || (c == '-') ||
  3185. (c == '_') || (c == ':') ||
  3186. (IS_COMBINING(c)) ||
  3187. (IS_EXTENDER(c)))) {
  3188. if (len <= INT_MAX - l)
  3189. len += l;
  3190. NEXTL(l);
  3191. c = CUR_CHAR(l);
  3192. }
  3193. }
  3194. if (ctxt->instate == XML_PARSER_EOF)
  3195. return(NULL);
  3196. if (len > maxLength) {
  3197. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
  3198. return(NULL);
  3199. }
  3200. if (ctxt->input->cur - ctxt->input->base < len) {
  3201. /*
  3202. * There were a couple of bugs where PERefs lead to to a change
  3203. * of the buffer. Check the buffer size to avoid passing an invalid
  3204. * pointer to xmlDictLookup.
  3205. */
  3206. xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
  3207. "unexpected change of input buffer");
  3208. return (NULL);
  3209. }
  3210. if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r'))
  3211. return(xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len));
  3212. return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
  3213. }
  3214. /**
  3215. * xmlParseName:
  3216. * @ctxt: an XML parser context
  3217. *
  3218. * DEPRECATED: Internal function, don't use.
  3219. *
  3220. * parse an XML name.
  3221. *
  3222. * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
  3223. * CombiningChar | Extender
  3224. *
  3225. * [5] Name ::= (Letter | '_' | ':') (NameChar)*
  3226. *
  3227. * [6] Names ::= Name (#x20 Name)*
  3228. *
  3229. * Returns the Name parsed or NULL
  3230. */
  3231. const xmlChar *
  3232. xmlParseName(xmlParserCtxtPtr ctxt) {
  3233. const xmlChar *in;
  3234. const xmlChar *ret;
  3235. size_t count = 0;
  3236. size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3237. XML_MAX_TEXT_LENGTH :
  3238. XML_MAX_NAME_LENGTH;
  3239. GROW;
  3240. if (ctxt->instate == XML_PARSER_EOF)
  3241. return(NULL);
  3242. /*
  3243. * Accelerator for simple ASCII names
  3244. */
  3245. in = ctxt->input->cur;
  3246. if (((*in >= 0x61) && (*in <= 0x7A)) ||
  3247. ((*in >= 0x41) && (*in <= 0x5A)) ||
  3248. (*in == '_') || (*in == ':')) {
  3249. in++;
  3250. while (((*in >= 0x61) && (*in <= 0x7A)) ||
  3251. ((*in >= 0x41) && (*in <= 0x5A)) ||
  3252. ((*in >= 0x30) && (*in <= 0x39)) ||
  3253. (*in == '_') || (*in == '-') ||
  3254. (*in == ':') || (*in == '.'))
  3255. in++;
  3256. if ((*in > 0) && (*in < 0x80)) {
  3257. count = in - ctxt->input->cur;
  3258. if (count > maxLength) {
  3259. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
  3260. return(NULL);
  3261. }
  3262. ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
  3263. ctxt->input->cur = in;
  3264. ctxt->input->col += count;
  3265. if (ret == NULL)
  3266. xmlErrMemory(ctxt, NULL);
  3267. return(ret);
  3268. }
  3269. }
  3270. /* accelerator for special cases */
  3271. return(xmlParseNameComplex(ctxt));
  3272. }
  3273. static xmlHashedString
  3274. xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
  3275. xmlHashedString ret;
  3276. int len = 0, l;
  3277. int c;
  3278. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3279. XML_MAX_TEXT_LENGTH :
  3280. XML_MAX_NAME_LENGTH;
  3281. size_t startPosition = 0;
  3282. ret.name = NULL;
  3283. ret.hashValue = 0;
  3284. /*
  3285. * Handler for more complex cases
  3286. */
  3287. startPosition = CUR_PTR - BASE_PTR;
  3288. c = CUR_CHAR(l);
  3289. if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
  3290. (!xmlIsNameStartChar(ctxt, c) || (c == ':'))) {
  3291. return(ret);
  3292. }
  3293. while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
  3294. (xmlIsNameChar(ctxt, c) && (c != ':'))) {
  3295. if (len <= INT_MAX - l)
  3296. len += l;
  3297. NEXTL(l);
  3298. c = CUR_CHAR(l);
  3299. }
  3300. if (ctxt->instate == XML_PARSER_EOF)
  3301. return(ret);
  3302. if (len > maxLength) {
  3303. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
  3304. return(ret);
  3305. }
  3306. ret = xmlDictLookupHashed(ctxt->dict, (BASE_PTR + startPosition), len);
  3307. return(ret);
  3308. }
  3309. /**
  3310. * xmlParseNCName:
  3311. * @ctxt: an XML parser context
  3312. * @len: length of the string parsed
  3313. *
  3314. * parse an XML name.
  3315. *
  3316. * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
  3317. * CombiningChar | Extender
  3318. *
  3319. * [5NS] NCName ::= (Letter | '_') (NCNameChar)*
  3320. *
  3321. * Returns the Name parsed or NULL
  3322. */
  3323. static xmlHashedString
  3324. xmlParseNCName(xmlParserCtxtPtr ctxt) {
  3325. const xmlChar *in, *e;
  3326. xmlHashedString ret;
  3327. size_t count = 0;
  3328. size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3329. XML_MAX_TEXT_LENGTH :
  3330. XML_MAX_NAME_LENGTH;
  3331. ret.name = NULL;
  3332. /*
  3333. * Accelerator for simple ASCII names
  3334. */
  3335. in = ctxt->input->cur;
  3336. e = ctxt->input->end;
  3337. if ((((*in >= 0x61) && (*in <= 0x7A)) ||
  3338. ((*in >= 0x41) && (*in <= 0x5A)) ||
  3339. (*in == '_')) && (in < e)) {
  3340. in++;
  3341. while ((((*in >= 0x61) && (*in <= 0x7A)) ||
  3342. ((*in >= 0x41) && (*in <= 0x5A)) ||
  3343. ((*in >= 0x30) && (*in <= 0x39)) ||
  3344. (*in == '_') || (*in == '-') ||
  3345. (*in == '.')) && (in < e))
  3346. in++;
  3347. if (in >= e)
  3348. goto complex;
  3349. if ((*in > 0) && (*in < 0x80)) {
  3350. count = in - ctxt->input->cur;
  3351. if (count > maxLength) {
  3352. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
  3353. return(ret);
  3354. }
  3355. ret = xmlDictLookupHashed(ctxt->dict, ctxt->input->cur, count);
  3356. ctxt->input->cur = in;
  3357. ctxt->input->col += count;
  3358. if (ret.name == NULL) {
  3359. xmlErrMemory(ctxt, NULL);
  3360. }
  3361. return(ret);
  3362. }
  3363. }
  3364. complex:
  3365. return(xmlParseNCNameComplex(ctxt));
  3366. }
  3367. /**
  3368. * xmlParseNameAndCompare:
  3369. * @ctxt: an XML parser context
  3370. *
  3371. * parse an XML name and compares for match
  3372. * (specialized for endtag parsing)
  3373. *
  3374. * Returns NULL for an illegal name, (xmlChar*) 1 for success
  3375. * and the name for mismatch
  3376. */
  3377. static const xmlChar *
  3378. xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) {
  3379. register const xmlChar *cmp = other;
  3380. register const xmlChar *in;
  3381. const xmlChar *ret;
  3382. GROW;
  3383. if (ctxt->instate == XML_PARSER_EOF)
  3384. return(NULL);
  3385. in = ctxt->input->cur;
  3386. while (*in != 0 && *in == *cmp) {
  3387. ++in;
  3388. ++cmp;
  3389. }
  3390. if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
  3391. /* success */
  3392. ctxt->input->col += in - ctxt->input->cur;
  3393. ctxt->input->cur = in;
  3394. return (const xmlChar*) 1;
  3395. }
  3396. /* failure (or end of input buffer), check with full function */
  3397. ret = xmlParseName (ctxt);
  3398. /* strings coming from the dictionary direct compare possible */
  3399. if (ret == other) {
  3400. return (const xmlChar*) 1;
  3401. }
  3402. return ret;
  3403. }
  3404. /**
  3405. * xmlParseStringName:
  3406. * @ctxt: an XML parser context
  3407. * @str: a pointer to the string pointer (IN/OUT)
  3408. *
  3409. * parse an XML name.
  3410. *
  3411. * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
  3412. * CombiningChar | Extender
  3413. *
  3414. * [5] Name ::= (Letter | '_' | ':') (NameChar)*
  3415. *
  3416. * [6] Names ::= Name (#x20 Name)*
  3417. *
  3418. * Returns the Name parsed or NULL. The @str pointer
  3419. * is updated to the current location in the string.
  3420. */
  3421. static xmlChar *
  3422. xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
  3423. xmlChar buf[XML_MAX_NAMELEN + 5];
  3424. const xmlChar *cur = *str;
  3425. int len = 0, l;
  3426. int c;
  3427. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3428. XML_MAX_TEXT_LENGTH :
  3429. XML_MAX_NAME_LENGTH;
  3430. c = CUR_SCHAR(cur, l);
  3431. if (!xmlIsNameStartChar(ctxt, c)) {
  3432. return(NULL);
  3433. }
  3434. COPY_BUF(buf, len, c);
  3435. cur += l;
  3436. c = CUR_SCHAR(cur, l);
  3437. while (xmlIsNameChar(ctxt, c)) {
  3438. COPY_BUF(buf, len, c);
  3439. cur += l;
  3440. c = CUR_SCHAR(cur, l);
  3441. if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */
  3442. /*
  3443. * Okay someone managed to make a huge name, so he's ready to pay
  3444. * for the processing speed.
  3445. */
  3446. xmlChar *buffer;
  3447. int max = len * 2;
  3448. buffer = (xmlChar *) xmlMallocAtomic(max);
  3449. if (buffer == NULL) {
  3450. xmlErrMemory(ctxt, NULL);
  3451. return(NULL);
  3452. }
  3453. memcpy(buffer, buf, len);
  3454. while (xmlIsNameChar(ctxt, c)) {
  3455. if (len + 10 > max) {
  3456. xmlChar *tmp;
  3457. max *= 2;
  3458. tmp = (xmlChar *) xmlRealloc(buffer, max);
  3459. if (tmp == NULL) {
  3460. xmlErrMemory(ctxt, NULL);
  3461. xmlFree(buffer);
  3462. return(NULL);
  3463. }
  3464. buffer = tmp;
  3465. }
  3466. COPY_BUF(buffer, len, c);
  3467. cur += l;
  3468. c = CUR_SCHAR(cur, l);
  3469. if (len > maxLength) {
  3470. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
  3471. xmlFree(buffer);
  3472. return(NULL);
  3473. }
  3474. }
  3475. buffer[len] = 0;
  3476. *str = cur;
  3477. return(buffer);
  3478. }
  3479. }
  3480. if (len > maxLength) {
  3481. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
  3482. return(NULL);
  3483. }
  3484. *str = cur;
  3485. return(xmlStrndup(buf, len));
  3486. }
  3487. /**
  3488. * xmlParseNmtoken:
  3489. * @ctxt: an XML parser context
  3490. *
  3491. * DEPRECATED: Internal function, don't use.
  3492. *
  3493. * parse an XML Nmtoken.
  3494. *
  3495. * [7] Nmtoken ::= (NameChar)+
  3496. *
  3497. * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
  3498. *
  3499. * Returns the Nmtoken parsed or NULL
  3500. */
  3501. xmlChar *
  3502. xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
  3503. xmlChar buf[XML_MAX_NAMELEN + 5];
  3504. int len = 0, l;
  3505. int c;
  3506. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3507. XML_MAX_TEXT_LENGTH :
  3508. XML_MAX_NAME_LENGTH;
  3509. c = CUR_CHAR(l);
  3510. while (xmlIsNameChar(ctxt, c)) {
  3511. COPY_BUF(buf, len, c);
  3512. NEXTL(l);
  3513. c = CUR_CHAR(l);
  3514. if (len >= XML_MAX_NAMELEN) {
  3515. /*
  3516. * Okay someone managed to make a huge token, so he's ready to pay
  3517. * for the processing speed.
  3518. */
  3519. xmlChar *buffer;
  3520. int max = len * 2;
  3521. buffer = (xmlChar *) xmlMallocAtomic(max);
  3522. if (buffer == NULL) {
  3523. xmlErrMemory(ctxt, NULL);
  3524. return(NULL);
  3525. }
  3526. memcpy(buffer, buf, len);
  3527. while (xmlIsNameChar(ctxt, c)) {
  3528. if (len + 10 > max) {
  3529. xmlChar *tmp;
  3530. max *= 2;
  3531. tmp = (xmlChar *) xmlRealloc(buffer, max);
  3532. if (tmp == NULL) {
  3533. xmlErrMemory(ctxt, NULL);
  3534. xmlFree(buffer);
  3535. return(NULL);
  3536. }
  3537. buffer = tmp;
  3538. }
  3539. COPY_BUF(buffer, len, c);
  3540. if (len > maxLength) {
  3541. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
  3542. xmlFree(buffer);
  3543. return(NULL);
  3544. }
  3545. NEXTL(l);
  3546. c = CUR_CHAR(l);
  3547. }
  3548. buffer[len] = 0;
  3549. if (ctxt->instate == XML_PARSER_EOF) {
  3550. xmlFree(buffer);
  3551. return(NULL);
  3552. }
  3553. return(buffer);
  3554. }
  3555. }
  3556. if (ctxt->instate == XML_PARSER_EOF)
  3557. return(NULL);
  3558. if (len == 0)
  3559. return(NULL);
  3560. if (len > maxLength) {
  3561. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
  3562. return(NULL);
  3563. }
  3564. return(xmlStrndup(buf, len));
  3565. }
  3566. /**
  3567. * xmlParseEntityValue:
  3568. * @ctxt: an XML parser context
  3569. * @orig: if non-NULL store a copy of the original entity value
  3570. *
  3571. * DEPRECATED: Internal function, don't use.
  3572. *
  3573. * parse a value for ENTITY declarations
  3574. *
  3575. * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' |
  3576. * "'" ([^%&'] | PEReference | Reference)* "'"
  3577. *
  3578. * Returns the EntityValue parsed with reference substituted or NULL
  3579. */
  3580. xmlChar *
  3581. xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) {
  3582. xmlChar *buf = NULL;
  3583. int len = 0;
  3584. int size = XML_PARSER_BUFFER_SIZE;
  3585. int c, l;
  3586. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3587. XML_MAX_HUGE_LENGTH :
  3588. XML_MAX_TEXT_LENGTH;
  3589. xmlChar stop;
  3590. xmlChar *ret = NULL;
  3591. const xmlChar *cur = NULL;
  3592. xmlParserInputPtr input;
  3593. if (RAW == '"') stop = '"';
  3594. else if (RAW == '\'') stop = '\'';
  3595. else {
  3596. xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL);
  3597. return(NULL);
  3598. }
  3599. buf = (xmlChar *) xmlMallocAtomic(size);
  3600. if (buf == NULL) {
  3601. xmlErrMemory(ctxt, NULL);
  3602. return(NULL);
  3603. }
  3604. /*
  3605. * The content of the entity definition is copied in a buffer.
  3606. */
  3607. ctxt->instate = XML_PARSER_ENTITY_VALUE;
  3608. input = ctxt->input;
  3609. GROW;
  3610. if (ctxt->instate == XML_PARSER_EOF)
  3611. goto error;
  3612. NEXT;
  3613. c = CUR_CHAR(l);
  3614. /*
  3615. * NOTE: 4.4.5 Included in Literal
  3616. * When a parameter entity reference appears in a literal entity
  3617. * value, ... a single or double quote character in the replacement
  3618. * text is always treated as a normal data character and will not
  3619. * terminate the literal.
  3620. * In practice it means we stop the loop only when back at parsing
  3621. * the initial entity and the quote is found
  3622. */
  3623. while (((IS_CHAR(c)) && ((c != stop) || /* checked */
  3624. (ctxt->input != input))) && (ctxt->instate != XML_PARSER_EOF)) {
  3625. if (len + 5 >= size) {
  3626. xmlChar *tmp;
  3627. size *= 2;
  3628. tmp = (xmlChar *) xmlRealloc(buf, size);
  3629. if (tmp == NULL) {
  3630. xmlErrMemory(ctxt, NULL);
  3631. goto error;
  3632. }
  3633. buf = tmp;
  3634. }
  3635. COPY_BUF(buf, len, c);
  3636. NEXTL(l);
  3637. GROW;
  3638. c = CUR_CHAR(l);
  3639. if (c == 0) {
  3640. GROW;
  3641. c = CUR_CHAR(l);
  3642. }
  3643. if (len > maxLength) {
  3644. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
  3645. "entity value too long\n");
  3646. goto error;
  3647. }
  3648. }
  3649. buf[len] = 0;
  3650. if (ctxt->instate == XML_PARSER_EOF)
  3651. goto error;
  3652. if (c != stop) {
  3653. xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL);
  3654. goto error;
  3655. }
  3656. NEXT;
  3657. /*
  3658. * Raise problem w.r.t. '&' and '%' being used in non-entities
  3659. * reference constructs. Note Charref will be handled in
  3660. * xmlStringDecodeEntities()
  3661. */
  3662. cur = buf;
  3663. while (*cur != 0) { /* non input consuming */
  3664. if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) {
  3665. xmlChar *name;
  3666. xmlChar tmp = *cur;
  3667. int nameOk = 0;
  3668. cur++;
  3669. name = xmlParseStringName(ctxt, &cur);
  3670. if (name != NULL) {
  3671. nameOk = 1;
  3672. xmlFree(name);
  3673. }
  3674. if ((nameOk == 0) || (*cur != ';')) {
  3675. xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR,
  3676. "EntityValue: '%c' forbidden except for entities references\n",
  3677. tmp);
  3678. goto error;
  3679. }
  3680. if ((tmp == '%') && (ctxt->inSubset == 1) &&
  3681. (ctxt->inputNr == 1)) {
  3682. xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL);
  3683. goto error;
  3684. }
  3685. if (*cur == 0)
  3686. break;
  3687. }
  3688. cur++;
  3689. }
  3690. /*
  3691. * Then PEReference entities are substituted.
  3692. *
  3693. * NOTE: 4.4.7 Bypassed
  3694. * When a general entity reference appears in the EntityValue in
  3695. * an entity declaration, it is bypassed and left as is.
  3696. * so XML_SUBSTITUTE_REF is not set here.
  3697. */
  3698. ++ctxt->depth;
  3699. ret = xmlStringDecodeEntitiesInt(ctxt, buf, len, XML_SUBSTITUTE_PEREF,
  3700. 0, 0, 0, /* check */ 1);
  3701. --ctxt->depth;
  3702. if (orig != NULL) {
  3703. *orig = buf;
  3704. buf = NULL;
  3705. }
  3706. error:
  3707. if (buf != NULL)
  3708. xmlFree(buf);
  3709. return(ret);
  3710. }
  3711. /**
  3712. * xmlParseAttValueComplex:
  3713. * @ctxt: an XML parser context
  3714. * @len: the resulting attribute len
  3715. * @normalize: whether to apply the inner normalization
  3716. *
  3717. * parse a value for an attribute, this is the fallback function
  3718. * of xmlParseAttValue() when the attribute parsing requires handling
  3719. * of non-ASCII characters, or normalization compaction.
  3720. *
  3721. * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
  3722. */
  3723. static xmlChar *
  3724. xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
  3725. xmlChar limit = 0;
  3726. xmlChar *buf = NULL;
  3727. xmlChar *rep = NULL;
  3728. size_t len = 0;
  3729. size_t buf_size = 0;
  3730. size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3731. XML_MAX_HUGE_LENGTH :
  3732. XML_MAX_TEXT_LENGTH;
  3733. int c, l, in_space = 0;
  3734. xmlChar *current = NULL;
  3735. xmlEntityPtr ent;
  3736. if (NXT(0) == '"') {
  3737. ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
  3738. limit = '"';
  3739. NEXT;
  3740. } else if (NXT(0) == '\'') {
  3741. limit = '\'';
  3742. ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
  3743. NEXT;
  3744. } else {
  3745. xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
  3746. return(NULL);
  3747. }
  3748. /*
  3749. * allocate a translation buffer.
  3750. */
  3751. buf_size = XML_PARSER_BUFFER_SIZE;
  3752. buf = (xmlChar *) xmlMallocAtomic(buf_size);
  3753. if (buf == NULL) goto mem_error;
  3754. /*
  3755. * OK loop until we reach one of the ending char or a size limit.
  3756. */
  3757. c = CUR_CHAR(l);
  3758. while (((NXT(0) != limit) && /* checked */
  3759. (IS_CHAR(c)) && (c != '<')) &&
  3760. (ctxt->instate != XML_PARSER_EOF)) {
  3761. if (c == '&') {
  3762. in_space = 0;
  3763. if (NXT(1) == '#') {
  3764. int val = xmlParseCharRef(ctxt);
  3765. if (val == '&') {
  3766. if (ctxt->replaceEntities) {
  3767. if (len + 10 > buf_size) {
  3768. growBuffer(buf, 10);
  3769. }
  3770. buf[len++] = '&';
  3771. } else {
  3772. /*
  3773. * The reparsing will be done in xmlStringGetNodeList()
  3774. * called by the attribute() function in SAX.c
  3775. */
  3776. if (len + 10 > buf_size) {
  3777. growBuffer(buf, 10);
  3778. }
  3779. buf[len++] = '&';
  3780. buf[len++] = '#';
  3781. buf[len++] = '3';
  3782. buf[len++] = '8';
  3783. buf[len++] = ';';
  3784. }
  3785. } else if (val != 0) {
  3786. if (len + 10 > buf_size) {
  3787. growBuffer(buf, 10);
  3788. }
  3789. len += xmlCopyChar(0, &buf[len], val);
  3790. }
  3791. } else {
  3792. ent = xmlParseEntityRef(ctxt);
  3793. if ((ent != NULL) &&
  3794. (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
  3795. if (len + 10 > buf_size) {
  3796. growBuffer(buf, 10);
  3797. }
  3798. if ((ctxt->replaceEntities == 0) &&
  3799. (ent->content[0] == '&')) {
  3800. buf[len++] = '&';
  3801. buf[len++] = '#';
  3802. buf[len++] = '3';
  3803. buf[len++] = '8';
  3804. buf[len++] = ';';
  3805. } else {
  3806. buf[len++] = ent->content[0];
  3807. }
  3808. } else if ((ent != NULL) &&
  3809. (ctxt->replaceEntities != 0)) {
  3810. if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) {
  3811. if (xmlParserEntityCheck(ctxt, ent->length))
  3812. goto error;
  3813. ++ctxt->depth;
  3814. rep = xmlStringDecodeEntitiesInt(ctxt, ent->content,
  3815. ent->length, XML_SUBSTITUTE_REF, 0, 0, 0,
  3816. /* check */ 1);
  3817. --ctxt->depth;
  3818. if (rep != NULL) {
  3819. current = rep;
  3820. while (*current != 0) { /* non input consuming */
  3821. if ((*current == 0xD) || (*current == 0xA) ||
  3822. (*current == 0x9)) {
  3823. buf[len++] = 0x20;
  3824. current++;
  3825. } else
  3826. buf[len++] = *current++;
  3827. if (len + 10 > buf_size) {
  3828. growBuffer(buf, 10);
  3829. }
  3830. }
  3831. xmlFree(rep);
  3832. rep = NULL;
  3833. }
  3834. } else {
  3835. if (len + 10 > buf_size) {
  3836. growBuffer(buf, 10);
  3837. }
  3838. if (ent->content != NULL)
  3839. buf[len++] = ent->content[0];
  3840. }
  3841. } else if (ent != NULL) {
  3842. int i = xmlStrlen(ent->name);
  3843. const xmlChar *cur = ent->name;
  3844. /*
  3845. * We also check for recursion and amplification
  3846. * when entities are not substituted. They're
  3847. * often expanded later.
  3848. */
  3849. if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) &&
  3850. (ent->content != NULL)) {
  3851. if ((ent->flags & XML_ENT_CHECKED) == 0) {
  3852. unsigned long oldCopy = ctxt->sizeentcopy;
  3853. ctxt->sizeentcopy = ent->length;
  3854. ++ctxt->depth;
  3855. rep = xmlStringDecodeEntitiesInt(ctxt,
  3856. ent->content, ent->length,
  3857. XML_SUBSTITUTE_REF, 0, 0, 0,
  3858. /* check */ 1);
  3859. --ctxt->depth;
  3860. /*
  3861. * If we're parsing DTD content, the entity
  3862. * might reference other entities which
  3863. * weren't defined yet, so the check isn't
  3864. * reliable.
  3865. */
  3866. if (ctxt->inSubset == 0) {
  3867. ent->flags |= XML_ENT_CHECKED;
  3868. ent->expandedSize = ctxt->sizeentcopy;
  3869. }
  3870. if (rep != NULL) {
  3871. xmlFree(rep);
  3872. rep = NULL;
  3873. } else {
  3874. ent->content[0] = 0;
  3875. }
  3876. if (xmlParserEntityCheck(ctxt, oldCopy))
  3877. goto error;
  3878. } else {
  3879. if (xmlParserEntityCheck(ctxt, ent->expandedSize))
  3880. goto error;
  3881. }
  3882. }
  3883. /*
  3884. * Just output the reference
  3885. */
  3886. buf[len++] = '&';
  3887. while (len + i + 10 > buf_size) {
  3888. growBuffer(buf, i + 10);
  3889. }
  3890. for (;i > 0;i--)
  3891. buf[len++] = *cur++;
  3892. buf[len++] = ';';
  3893. }
  3894. }
  3895. } else {
  3896. if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) {
  3897. if ((len != 0) || (!normalize)) {
  3898. if ((!normalize) || (!in_space)) {
  3899. COPY_BUF(buf, len, 0x20);
  3900. while (len + 10 > buf_size) {
  3901. growBuffer(buf, 10);
  3902. }
  3903. }
  3904. in_space = 1;
  3905. }
  3906. } else {
  3907. in_space = 0;
  3908. COPY_BUF(buf, len, c);
  3909. if (len + 10 > buf_size) {
  3910. growBuffer(buf, 10);
  3911. }
  3912. }
  3913. NEXTL(l);
  3914. }
  3915. GROW;
  3916. c = CUR_CHAR(l);
  3917. if (len > maxLength) {
  3918. xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  3919. "AttValue length too long\n");
  3920. goto mem_error;
  3921. }
  3922. }
  3923. if (ctxt->instate == XML_PARSER_EOF)
  3924. goto error;
  3925. if ((in_space) && (normalize)) {
  3926. while ((len > 0) && (buf[len - 1] == 0x20)) len--;
  3927. }
  3928. buf[len] = 0;
  3929. if (RAW == '<') {
  3930. xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL);
  3931. } else if (RAW != limit) {
  3932. if ((c != 0) && (!IS_CHAR(c))) {
  3933. xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
  3934. "invalid character in attribute value\n");
  3935. } else {
  3936. xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  3937. "AttValue: ' expected\n");
  3938. }
  3939. } else
  3940. NEXT;
  3941. if (attlen != NULL) *attlen = len;
  3942. return(buf);
  3943. mem_error:
  3944. xmlErrMemory(ctxt, NULL);
  3945. error:
  3946. if (buf != NULL)
  3947. xmlFree(buf);
  3948. if (rep != NULL)
  3949. xmlFree(rep);
  3950. return(NULL);
  3951. }
  3952. /**
  3953. * xmlParseAttValue:
  3954. * @ctxt: an XML parser context
  3955. *
  3956. * DEPRECATED: Internal function, don't use.
  3957. *
  3958. * parse a value for an attribute
  3959. * Note: the parser won't do substitution of entities here, this
  3960. * will be handled later in xmlStringGetNodeList
  3961. *
  3962. * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' |
  3963. * "'" ([^<&'] | Reference)* "'"
  3964. *
  3965. * 3.3.3 Attribute-Value Normalization:
  3966. * Before the value of an attribute is passed to the application or
  3967. * checked for validity, the XML processor must normalize it as follows:
  3968. * - a character reference is processed by appending the referenced
  3969. * character to the attribute value
  3970. * - an entity reference is processed by recursively processing the
  3971. * replacement text of the entity
  3972. * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
  3973. * appending #x20 to the normalized value, except that only a single
  3974. * #x20 is appended for a "#xD#xA" sequence that is part of an external
  3975. * parsed entity or the literal entity value of an internal parsed entity
  3976. * - other characters are processed by appending them to the normalized value
  3977. * If the declared value is not CDATA, then the XML processor must further
  3978. * process the normalized attribute value by discarding any leading and
  3979. * trailing space (#x20) characters, and by replacing sequences of space
  3980. * (#x20) characters by a single space (#x20) character.
  3981. * All attributes for which no declaration has been read should be treated
  3982. * by a non-validating parser as if declared CDATA.
  3983. *
  3984. * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
  3985. */
  3986. xmlChar *
  3987. xmlParseAttValue(xmlParserCtxtPtr ctxt) {
  3988. if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
  3989. return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0));
  3990. }
  3991. /**
  3992. * xmlParseSystemLiteral:
  3993. * @ctxt: an XML parser context
  3994. *
  3995. * DEPRECATED: Internal function, don't use.
  3996. *
  3997. * parse an XML Literal
  3998. *
  3999. * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
  4000. *
  4001. * Returns the SystemLiteral parsed or NULL
  4002. */
  4003. xmlChar *
  4004. xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) {
  4005. xmlChar *buf = NULL;
  4006. int len = 0;
  4007. int size = XML_PARSER_BUFFER_SIZE;
  4008. int cur, l;
  4009. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  4010. XML_MAX_TEXT_LENGTH :
  4011. XML_MAX_NAME_LENGTH;
  4012. xmlChar stop;
  4013. int state = ctxt->instate;
  4014. if (RAW == '"') {
  4015. NEXT;
  4016. stop = '"';
  4017. } else if (RAW == '\'') {
  4018. NEXT;
  4019. stop = '\'';
  4020. } else {
  4021. xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
  4022. return(NULL);
  4023. }
  4024. buf = (xmlChar *) xmlMallocAtomic(size);
  4025. if (buf == NULL) {
  4026. xmlErrMemory(ctxt, NULL);
  4027. return(NULL);
  4028. }
  4029. ctxt->instate = XML_PARSER_SYSTEM_LITERAL;
  4030. cur = CUR_CHAR(l);
  4031. while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */
  4032. if (len + 5 >= size) {
  4033. xmlChar *tmp;
  4034. size *= 2;
  4035. tmp = (xmlChar *) xmlRealloc(buf, size);
  4036. if (tmp == NULL) {
  4037. xmlFree(buf);
  4038. xmlErrMemory(ctxt, NULL);
  4039. ctxt->instate = (xmlParserInputState) state;
  4040. return(NULL);
  4041. }
  4042. buf = tmp;
  4043. }
  4044. COPY_BUF(buf, len, cur);
  4045. if (len > maxLength) {
  4046. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral");
  4047. xmlFree(buf);
  4048. ctxt->instate = (xmlParserInputState) state;
  4049. return(NULL);
  4050. }
  4051. NEXTL(l);
  4052. cur = CUR_CHAR(l);
  4053. }
  4054. buf[len] = 0;
  4055. if (ctxt->instate == XML_PARSER_EOF) {
  4056. xmlFree(buf);
  4057. return(NULL);
  4058. }
  4059. ctxt->instate = (xmlParserInputState) state;
  4060. if (!IS_CHAR(cur)) {
  4061. xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
  4062. } else {
  4063. NEXT;
  4064. }
  4065. return(buf);
  4066. }
  4067. /**
  4068. * xmlParsePubidLiteral:
  4069. * @ctxt: an XML parser context
  4070. *
  4071. * DEPRECATED: Internal function, don't use.
  4072. *
  4073. * parse an XML public literal
  4074. *
  4075. * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
  4076. *
  4077. * Returns the PubidLiteral parsed or NULL.
  4078. */
  4079. xmlChar *
  4080. xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) {
  4081. xmlChar *buf = NULL;
  4082. int len = 0;
  4083. int size = XML_PARSER_BUFFER_SIZE;
  4084. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  4085. XML_MAX_TEXT_LENGTH :
  4086. XML_MAX_NAME_LENGTH;
  4087. xmlChar cur;
  4088. xmlChar stop;
  4089. xmlParserInputState oldstate = ctxt->instate;
  4090. if (RAW == '"') {
  4091. NEXT;
  4092. stop = '"';
  4093. } else if (RAW == '\'') {
  4094. NEXT;
  4095. stop = '\'';
  4096. } else {
  4097. xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
  4098. return(NULL);
  4099. }
  4100. buf = (xmlChar *) xmlMallocAtomic(size);
  4101. if (buf == NULL) {
  4102. xmlErrMemory(ctxt, NULL);
  4103. return(NULL);
  4104. }
  4105. ctxt->instate = XML_PARSER_PUBLIC_LITERAL;
  4106. cur = CUR;
  4107. while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */
  4108. if (len + 1 >= size) {
  4109. xmlChar *tmp;
  4110. size *= 2;
  4111. tmp = (xmlChar *) xmlRealloc(buf, size);
  4112. if (tmp == NULL) {
  4113. xmlErrMemory(ctxt, NULL);
  4114. xmlFree(buf);
  4115. return(NULL);
  4116. }
  4117. buf = tmp;
  4118. }
  4119. buf[len++] = cur;
  4120. if (len > maxLength) {
  4121. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID");
  4122. xmlFree(buf);
  4123. return(NULL);
  4124. }
  4125. NEXT;
  4126. cur = CUR;
  4127. }
  4128. buf[len] = 0;
  4129. if (ctxt->instate == XML_PARSER_EOF) {
  4130. xmlFree(buf);
  4131. return(NULL);
  4132. }
  4133. if (cur != stop) {
  4134. xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
  4135. } else {
  4136. NEXTL(1);
  4137. }
  4138. ctxt->instate = oldstate;
  4139. return(buf);
  4140. }
  4141. static void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int partial);
  4142. /*
  4143. * used for the test in the inner loop of the char data testing
  4144. */
  4145. static const unsigned char test_char_data[256] = {
  4146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4147. 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */
  4148. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4149. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4150. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */
  4151. 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
  4152. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  4153. 0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */
  4154. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  4155. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  4156. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  4157. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */
  4158. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  4159. 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
  4160. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  4161. 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
  4162. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */
  4163. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4165. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4166. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4167. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4168. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4169. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4170. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4171. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4173. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4175. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4176. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  4178. };
  4179. /**
  4180. * xmlParseCharDataInternal:
  4181. * @ctxt: an XML parser context
  4182. * @partial: buffer may contain partial UTF-8 sequences
  4183. *
  4184. * Parse character data. Always makes progress if the first char isn't
  4185. * '<' or '&'.
  4186. *
  4187. * The right angle bracket (>) may be represented using the string "&gt;",
  4188. * and must, for compatibility, be escaped using "&gt;" or a character
  4189. * reference when it appears in the string "]]>" in content, when that
  4190. * string is not marking the end of a CDATA section.
  4191. *
  4192. * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
  4193. */
  4194. static void
  4195. xmlParseCharDataInternal(xmlParserCtxtPtr ctxt, int partial) {
  4196. const xmlChar *in;
  4197. int nbchar = 0;
  4198. int line = ctxt->input->line;
  4199. int col = ctxt->input->col;
  4200. int ccol;
  4201. GROW;
  4202. /*
  4203. * Accelerated common case where input don't need to be
  4204. * modified before passing it to the handler.
  4205. */
  4206. in = ctxt->input->cur;
  4207. do {
  4208. get_more_space:
  4209. while (*in == 0x20) { in++; ctxt->input->col++; }
  4210. if (*in == 0xA) {
  4211. do {
  4212. ctxt->input->line++; ctxt->input->col = 1;
  4213. in++;
  4214. } while (*in == 0xA);
  4215. goto get_more_space;
  4216. }
  4217. if (*in == '<') {
  4218. nbchar = in - ctxt->input->cur;
  4219. if (nbchar > 0) {
  4220. const xmlChar *tmp = ctxt->input->cur;
  4221. ctxt->input->cur = in;
  4222. if ((ctxt->sax != NULL) &&
  4223. (ctxt->disableSAX == 0) &&
  4224. (ctxt->sax->ignorableWhitespace !=
  4225. ctxt->sax->characters)) {
  4226. if (areBlanks(ctxt, tmp, nbchar, 1)) {
  4227. if (ctxt->sax->ignorableWhitespace != NULL)
  4228. ctxt->sax->ignorableWhitespace(ctxt->userData,
  4229. tmp, nbchar);
  4230. } else {
  4231. if (ctxt->sax->characters != NULL)
  4232. ctxt->sax->characters(ctxt->userData,
  4233. tmp, nbchar);
  4234. if (*ctxt->space == -1)
  4235. *ctxt->space = -2;
  4236. }
  4237. } else if ((ctxt->sax != NULL) &&
  4238. (ctxt->disableSAX == 0) &&
  4239. (ctxt->sax->characters != NULL)) {
  4240. ctxt->sax->characters(ctxt->userData,
  4241. tmp, nbchar);
  4242. }
  4243. }
  4244. return;
  4245. }
  4246. get_more:
  4247. ccol = ctxt->input->col;
  4248. while (test_char_data[*in]) {
  4249. in++;
  4250. ccol++;
  4251. }
  4252. ctxt->input->col = ccol;
  4253. if (*in == 0xA) {
  4254. do {
  4255. ctxt->input->line++; ctxt->input->col = 1;
  4256. in++;
  4257. } while (*in == 0xA);
  4258. goto get_more;
  4259. }
  4260. if (*in == ']') {
  4261. if ((in[1] == ']') && (in[2] == '>')) {
  4262. xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
  4263. if (ctxt->instate != XML_PARSER_EOF)
  4264. ctxt->input->cur = in + 1;
  4265. return;
  4266. }
  4267. in++;
  4268. ctxt->input->col++;
  4269. goto get_more;
  4270. }
  4271. nbchar = in - ctxt->input->cur;
  4272. if (nbchar > 0) {
  4273. if ((ctxt->sax != NULL) &&
  4274. (ctxt->disableSAX == 0) &&
  4275. (ctxt->sax->ignorableWhitespace !=
  4276. ctxt->sax->characters) &&
  4277. (IS_BLANK_CH(*ctxt->input->cur))) {
  4278. const xmlChar *tmp = ctxt->input->cur;
  4279. ctxt->input->cur = in;
  4280. if (areBlanks(ctxt, tmp, nbchar, 0)) {
  4281. if (ctxt->sax->ignorableWhitespace != NULL)
  4282. ctxt->sax->ignorableWhitespace(ctxt->userData,
  4283. tmp, nbchar);
  4284. } else {
  4285. if (ctxt->sax->characters != NULL)
  4286. ctxt->sax->characters(ctxt->userData,
  4287. tmp, nbchar);
  4288. if (*ctxt->space == -1)
  4289. *ctxt->space = -2;
  4290. }
  4291. line = ctxt->input->line;
  4292. col = ctxt->input->col;
  4293. } else if ((ctxt->sax != NULL) &&
  4294. (ctxt->disableSAX == 0)) {
  4295. if (ctxt->sax->characters != NULL)
  4296. ctxt->sax->characters(ctxt->userData,
  4297. ctxt->input->cur, nbchar);
  4298. line = ctxt->input->line;
  4299. col = ctxt->input->col;
  4300. }
  4301. if (ctxt->instate == XML_PARSER_EOF)
  4302. return;
  4303. }
  4304. ctxt->input->cur = in;
  4305. if (*in == 0xD) {
  4306. in++;
  4307. if (*in == 0xA) {
  4308. ctxt->input->cur = in;
  4309. in++;
  4310. ctxt->input->line++; ctxt->input->col = 1;
  4311. continue; /* while */
  4312. }
  4313. in--;
  4314. }
  4315. if (*in == '<') {
  4316. return;
  4317. }
  4318. if (*in == '&') {
  4319. return;
  4320. }
  4321. SHRINK;
  4322. GROW;
  4323. if (ctxt->instate == XML_PARSER_EOF)
  4324. return;
  4325. in = ctxt->input->cur;
  4326. } while (((*in >= 0x20) && (*in <= 0x7F)) ||
  4327. (*in == 0x09) || (*in == 0x0a));
  4328. ctxt->input->line = line;
  4329. ctxt->input->col = col;
  4330. xmlParseCharDataComplex(ctxt, partial);
  4331. }
  4332. /**
  4333. * xmlParseCharDataComplex:
  4334. * @ctxt: an XML parser context
  4335. * @cdata: int indicating whether we are within a CDATA section
  4336. *
  4337. * Always makes progress if the first char isn't '<' or '&'.
  4338. *
  4339. * parse a CharData section.this is the fallback function
  4340. * of xmlParseCharData() when the parsing requires handling
  4341. * of non-ASCII characters.
  4342. */
  4343. static void
  4344. xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int partial) {
  4345. xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5];
  4346. int nbchar = 0;
  4347. int cur, l;
  4348. cur = CUR_CHAR(l);
  4349. while ((cur != '<') && /* checked */
  4350. (cur != '&') &&
  4351. (IS_CHAR(cur))) {
  4352. if ((cur == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
  4353. xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
  4354. }
  4355. COPY_BUF(buf, nbchar, cur);
  4356. /* move current position before possible calling of ctxt->sax->characters */
  4357. NEXTL(l);
  4358. if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) {
  4359. buf[nbchar] = 0;
  4360. /*
  4361. * OK the segment is to be consumed as chars.
  4362. */
  4363. if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
  4364. if (areBlanks(ctxt, buf, nbchar, 0)) {
  4365. if (ctxt->sax->ignorableWhitespace != NULL)
  4366. ctxt->sax->ignorableWhitespace(ctxt->userData,
  4367. buf, nbchar);
  4368. } else {
  4369. if (ctxt->sax->characters != NULL)
  4370. ctxt->sax->characters(ctxt->userData, buf, nbchar);
  4371. if ((ctxt->sax->characters !=
  4372. ctxt->sax->ignorableWhitespace) &&
  4373. (*ctxt->space == -1))
  4374. *ctxt->space = -2;
  4375. }
  4376. }
  4377. nbchar = 0;
  4378. /* something really bad happened in the SAX callback */
  4379. if (ctxt->instate != XML_PARSER_CONTENT)
  4380. return;
  4381. SHRINK;
  4382. }
  4383. cur = CUR_CHAR(l);
  4384. }
  4385. if (ctxt->instate == XML_PARSER_EOF)
  4386. return;
  4387. if (nbchar != 0) {
  4388. buf[nbchar] = 0;
  4389. /*
  4390. * OK the segment is to be consumed as chars.
  4391. */
  4392. if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
  4393. if (areBlanks(ctxt, buf, nbchar, 0)) {
  4394. if (ctxt->sax->ignorableWhitespace != NULL)
  4395. ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
  4396. } else {
  4397. if (ctxt->sax->characters != NULL)
  4398. ctxt->sax->characters(ctxt->userData, buf, nbchar);
  4399. if ((ctxt->sax->characters != ctxt->sax->ignorableWhitespace) &&
  4400. (*ctxt->space == -1))
  4401. *ctxt->space = -2;
  4402. }
  4403. }
  4404. }
  4405. /*
  4406. * cur == 0 can mean
  4407. *
  4408. * - XML_PARSER_EOF or memory error. This is checked above.
  4409. * - An actual 0 character.
  4410. * - End of buffer.
  4411. * - An incomplete UTF-8 sequence. This is allowed if partial is set.
  4412. */
  4413. if (ctxt->input->cur < ctxt->input->end) {
  4414. if ((cur == 0) && (CUR != 0)) {
  4415. if (partial == 0) {
  4416. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  4417. "Incomplete UTF-8 sequence starting with %02X\n", CUR);
  4418. NEXTL(1);
  4419. }
  4420. } else if ((cur != '<') && (cur != '&')) {
  4421. /* Generate the error and skip the offending character */
  4422. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  4423. "PCDATA invalid Char value %d\n", cur);
  4424. NEXTL(l);
  4425. }
  4426. }
  4427. }
  4428. /**
  4429. * xmlParseCharData:
  4430. * @ctxt: an XML parser context
  4431. * @cdata: unused
  4432. *
  4433. * DEPRECATED: Internal function, don't use.
  4434. */
  4435. void
  4436. xmlParseCharData(xmlParserCtxtPtr ctxt, ATTRIBUTE_UNUSED int cdata) {
  4437. xmlParseCharDataInternal(ctxt, 0);
  4438. }
  4439. /**
  4440. * xmlParseExternalID:
  4441. * @ctxt: an XML parser context
  4442. * @publicID: a xmlChar** receiving PubidLiteral
  4443. * @strict: indicate whether we should restrict parsing to only
  4444. * production [75], see NOTE below
  4445. *
  4446. * DEPRECATED: Internal function, don't use.
  4447. *
  4448. * Parse an External ID or a Public ID
  4449. *
  4450. * NOTE: Productions [75] and [83] interact badly since [75] can generate
  4451. * 'PUBLIC' S PubidLiteral S SystemLiteral
  4452. *
  4453. * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
  4454. * | 'PUBLIC' S PubidLiteral S SystemLiteral
  4455. *
  4456. * [83] PublicID ::= 'PUBLIC' S PubidLiteral
  4457. *
  4458. * Returns the function returns SystemLiteral and in the second
  4459. * case publicID receives PubidLiteral, is strict is off
  4460. * it is possible to return NULL and have publicID set.
  4461. */
  4462. xmlChar *
  4463. xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
  4464. xmlChar *URI = NULL;
  4465. *publicID = NULL;
  4466. if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) {
  4467. SKIP(6);
  4468. if (SKIP_BLANKS == 0) {
  4469. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  4470. "Space required after 'SYSTEM'\n");
  4471. }
  4472. URI = xmlParseSystemLiteral(ctxt);
  4473. if (URI == NULL) {
  4474. xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
  4475. }
  4476. } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) {
  4477. SKIP(6);
  4478. if (SKIP_BLANKS == 0) {
  4479. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  4480. "Space required after 'PUBLIC'\n");
  4481. }
  4482. *publicID = xmlParsePubidLiteral(ctxt);
  4483. if (*publicID == NULL) {
  4484. xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL);
  4485. }
  4486. if (strict) {
  4487. /*
  4488. * We don't handle [83] so "S SystemLiteral" is required.
  4489. */
  4490. if (SKIP_BLANKS == 0) {
  4491. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  4492. "Space required after the Public Identifier\n");
  4493. }
  4494. } else {
  4495. /*
  4496. * We handle [83] so we return immediately, if
  4497. * "S SystemLiteral" is not detected. We skip blanks if no
  4498. * system literal was found, but this is harmless since we must
  4499. * be at the end of a NotationDecl.
  4500. */
  4501. if (SKIP_BLANKS == 0) return(NULL);
  4502. if ((CUR != '\'') && (CUR != '"')) return(NULL);
  4503. }
  4504. URI = xmlParseSystemLiteral(ctxt);
  4505. if (URI == NULL) {
  4506. xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
  4507. }
  4508. }
  4509. return(URI);
  4510. }
  4511. /**
  4512. * xmlParseCommentComplex:
  4513. * @ctxt: an XML parser context
  4514. * @buf: the already parsed part of the buffer
  4515. * @len: number of bytes in the buffer
  4516. * @size: allocated size of the buffer
  4517. *
  4518. * Skip an XML (SGML) comment <!-- .... -->
  4519. * The spec says that "For compatibility, the string "--" (double-hyphen)
  4520. * must not occur within comments. "
  4521. * This is the slow routine in case the accelerator for ascii didn't work
  4522. *
  4523. * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
  4524. */
  4525. static void
  4526. xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf,
  4527. size_t len, size_t size) {
  4528. int q, ql;
  4529. int r, rl;
  4530. int cur, l;
  4531. size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  4532. XML_MAX_HUGE_LENGTH :
  4533. XML_MAX_TEXT_LENGTH;
  4534. int inputid;
  4535. inputid = ctxt->input->id;
  4536. if (buf == NULL) {
  4537. len = 0;
  4538. size = XML_PARSER_BUFFER_SIZE;
  4539. buf = (xmlChar *) xmlMallocAtomic(size);
  4540. if (buf == NULL) {
  4541. xmlErrMemory(ctxt, NULL);
  4542. return;
  4543. }
  4544. }
  4545. q = CUR_CHAR(ql);
  4546. if (q == 0)
  4547. goto not_terminated;
  4548. if (!IS_CHAR(q)) {
  4549. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  4550. "xmlParseComment: invalid xmlChar value %d\n",
  4551. q);
  4552. xmlFree (buf);
  4553. return;
  4554. }
  4555. NEXTL(ql);
  4556. r = CUR_CHAR(rl);
  4557. if (r == 0)
  4558. goto not_terminated;
  4559. if (!IS_CHAR(r)) {
  4560. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  4561. "xmlParseComment: invalid xmlChar value %d\n",
  4562. r);
  4563. xmlFree (buf);
  4564. return;
  4565. }
  4566. NEXTL(rl);
  4567. cur = CUR_CHAR(l);
  4568. if (cur == 0)
  4569. goto not_terminated;
  4570. while (IS_CHAR(cur) && /* checked */
  4571. ((cur != '>') ||
  4572. (r != '-') || (q != '-'))) {
  4573. if ((r == '-') && (q == '-')) {
  4574. xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
  4575. }
  4576. if (len + 5 >= size) {
  4577. xmlChar *new_buf;
  4578. size_t new_size;
  4579. new_size = size * 2;
  4580. new_buf = (xmlChar *) xmlRealloc(buf, new_size);
  4581. if (new_buf == NULL) {
  4582. xmlFree (buf);
  4583. xmlErrMemory(ctxt, NULL);
  4584. return;
  4585. }
  4586. buf = new_buf;
  4587. size = new_size;
  4588. }
  4589. COPY_BUF(buf, len, q);
  4590. if (len > maxLength) {
  4591. xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
  4592. "Comment too big found", NULL);
  4593. xmlFree (buf);
  4594. return;
  4595. }
  4596. q = r;
  4597. ql = rl;
  4598. r = cur;
  4599. rl = l;
  4600. NEXTL(l);
  4601. cur = CUR_CHAR(l);
  4602. }
  4603. buf[len] = 0;
  4604. if (ctxt->instate == XML_PARSER_EOF) {
  4605. xmlFree(buf);
  4606. return;
  4607. }
  4608. if (cur == 0) {
  4609. xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
  4610. "Comment not terminated \n<!--%.50s\n", buf);
  4611. } else if (!IS_CHAR(cur)) {
  4612. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  4613. "xmlParseComment: invalid xmlChar value %d\n",
  4614. cur);
  4615. } else {
  4616. if (inputid != ctxt->input->id) {
  4617. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  4618. "Comment doesn't start and stop in the same"
  4619. " entity\n");
  4620. }
  4621. NEXT;
  4622. if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
  4623. (!ctxt->disableSAX))
  4624. ctxt->sax->comment(ctxt->userData, buf);
  4625. }
  4626. xmlFree(buf);
  4627. return;
  4628. not_terminated:
  4629. xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
  4630. "Comment not terminated\n", NULL);
  4631. xmlFree(buf);
  4632. return;
  4633. }
  4634. /**
  4635. * xmlParseComment:
  4636. * @ctxt: an XML parser context
  4637. *
  4638. * DEPRECATED: Internal function, don't use.
  4639. *
  4640. * Parse an XML (SGML) comment. Always consumes '<!'.
  4641. *
  4642. * The spec says that "For compatibility, the string "--" (double-hyphen)
  4643. * must not occur within comments. "
  4644. *
  4645. * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
  4646. */
  4647. void
  4648. xmlParseComment(xmlParserCtxtPtr ctxt) {
  4649. xmlChar *buf = NULL;
  4650. size_t size = XML_PARSER_BUFFER_SIZE;
  4651. size_t len = 0;
  4652. size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  4653. XML_MAX_HUGE_LENGTH :
  4654. XML_MAX_TEXT_LENGTH;
  4655. xmlParserInputState state;
  4656. const xmlChar *in;
  4657. size_t nbchar = 0;
  4658. int ccol;
  4659. int inputid;
  4660. /*
  4661. * Check that there is a comment right here.
  4662. */
  4663. if ((RAW != '<') || (NXT(1) != '!'))
  4664. return;
  4665. SKIP(2);
  4666. if ((RAW != '-') || (NXT(1) != '-'))
  4667. return;
  4668. state = ctxt->instate;
  4669. ctxt->instate = XML_PARSER_COMMENT;
  4670. inputid = ctxt->input->id;
  4671. SKIP(2);
  4672. GROW;
  4673. /*
  4674. * Accelerated common case where input don't need to be
  4675. * modified before passing it to the handler.
  4676. */
  4677. in = ctxt->input->cur;
  4678. do {
  4679. if (*in == 0xA) {
  4680. do {
  4681. ctxt->input->line++; ctxt->input->col = 1;
  4682. in++;
  4683. } while (*in == 0xA);
  4684. }
  4685. get_more:
  4686. ccol = ctxt->input->col;
  4687. while (((*in > '-') && (*in <= 0x7F)) ||
  4688. ((*in >= 0x20) && (*in < '-')) ||
  4689. (*in == 0x09)) {
  4690. in++;
  4691. ccol++;
  4692. }
  4693. ctxt->input->col = ccol;
  4694. if (*in == 0xA) {
  4695. do {
  4696. ctxt->input->line++; ctxt->input->col = 1;
  4697. in++;
  4698. } while (*in == 0xA);
  4699. goto get_more;
  4700. }
  4701. nbchar = in - ctxt->input->cur;
  4702. /*
  4703. * save current set of data
  4704. */
  4705. if (nbchar > 0) {
  4706. if (buf == NULL) {
  4707. if ((*in == '-') && (in[1] == '-'))
  4708. size = nbchar + 1;
  4709. else
  4710. size = XML_PARSER_BUFFER_SIZE + nbchar;
  4711. buf = (xmlChar *) xmlMallocAtomic(size);
  4712. if (buf == NULL) {
  4713. xmlErrMemory(ctxt, NULL);
  4714. ctxt->instate = state;
  4715. return;
  4716. }
  4717. len = 0;
  4718. } else if (len + nbchar + 1 >= size) {
  4719. xmlChar *new_buf;
  4720. size += len + nbchar + XML_PARSER_BUFFER_SIZE;
  4721. new_buf = (xmlChar *) xmlRealloc(buf, size);
  4722. if (new_buf == NULL) {
  4723. xmlFree (buf);
  4724. xmlErrMemory(ctxt, NULL);
  4725. ctxt->instate = state;
  4726. return;
  4727. }
  4728. buf = new_buf;
  4729. }
  4730. memcpy(&buf[len], ctxt->input->cur, nbchar);
  4731. len += nbchar;
  4732. buf[len] = 0;
  4733. }
  4734. if (len > maxLength) {
  4735. xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
  4736. "Comment too big found", NULL);
  4737. xmlFree (buf);
  4738. return;
  4739. }
  4740. ctxt->input->cur = in;
  4741. if (*in == 0xA) {
  4742. in++;
  4743. ctxt->input->line++; ctxt->input->col = 1;
  4744. }
  4745. if (*in == 0xD) {
  4746. in++;
  4747. if (*in == 0xA) {
  4748. ctxt->input->cur = in;
  4749. in++;
  4750. ctxt->input->line++; ctxt->input->col = 1;
  4751. goto get_more;
  4752. }
  4753. in--;
  4754. }
  4755. SHRINK;
  4756. GROW;
  4757. if (ctxt->instate == XML_PARSER_EOF) {
  4758. xmlFree(buf);
  4759. return;
  4760. }
  4761. in = ctxt->input->cur;
  4762. if (*in == '-') {
  4763. if (in[1] == '-') {
  4764. if (in[2] == '>') {
  4765. if (ctxt->input->id != inputid) {
  4766. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  4767. "comment doesn't start and stop in the"
  4768. " same entity\n");
  4769. }
  4770. SKIP(3);
  4771. if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
  4772. (!ctxt->disableSAX)) {
  4773. if (buf != NULL)
  4774. ctxt->sax->comment(ctxt->userData, buf);
  4775. else
  4776. ctxt->sax->comment(ctxt->userData, BAD_CAST "");
  4777. }
  4778. if (buf != NULL)
  4779. xmlFree(buf);
  4780. if (ctxt->instate != XML_PARSER_EOF)
  4781. ctxt->instate = state;
  4782. return;
  4783. }
  4784. if (buf != NULL) {
  4785. xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT,
  4786. "Double hyphen within comment: "
  4787. "<!--%.50s\n",
  4788. buf);
  4789. } else
  4790. xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT,
  4791. "Double hyphen within comment\n", NULL);
  4792. if (ctxt->instate == XML_PARSER_EOF) {
  4793. xmlFree(buf);
  4794. return;
  4795. }
  4796. in++;
  4797. ctxt->input->col++;
  4798. }
  4799. in++;
  4800. ctxt->input->col++;
  4801. goto get_more;
  4802. }
  4803. } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09) || (*in == 0x0a));
  4804. xmlParseCommentComplex(ctxt, buf, len, size);
  4805. ctxt->instate = state;
  4806. return;
  4807. }
  4808. /**
  4809. * xmlParsePITarget:
  4810. * @ctxt: an XML parser context
  4811. *
  4812. * DEPRECATED: Internal function, don't use.
  4813. *
  4814. * parse the name of a PI
  4815. *
  4816. * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
  4817. *
  4818. * Returns the PITarget name or NULL
  4819. */
  4820. const xmlChar *
  4821. xmlParsePITarget(xmlParserCtxtPtr ctxt) {
  4822. const xmlChar *name;
  4823. name = xmlParseName(ctxt);
  4824. if ((name != NULL) &&
  4825. ((name[0] == 'x') || (name[0] == 'X')) &&
  4826. ((name[1] == 'm') || (name[1] == 'M')) &&
  4827. ((name[2] == 'l') || (name[2] == 'L'))) {
  4828. int i;
  4829. if ((name[0] == 'x') && (name[1] == 'm') &&
  4830. (name[2] == 'l') && (name[3] == 0)) {
  4831. xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
  4832. "XML declaration allowed only at the start of the document\n");
  4833. return(name);
  4834. } else if (name[3] == 0) {
  4835. xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL);
  4836. return(name);
  4837. }
  4838. for (i = 0;;i++) {
  4839. if (xmlW3CPIs[i] == NULL) break;
  4840. if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
  4841. return(name);
  4842. }
  4843. xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
  4844. "xmlParsePITarget: invalid name prefix 'xml'\n",
  4845. NULL, NULL);
  4846. }
  4847. if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) {
  4848. xmlNsErr(ctxt, XML_NS_ERR_COLON,
  4849. "colons are forbidden from PI names '%s'\n", name, NULL, NULL);
  4850. }
  4851. return(name);
  4852. }
  4853. #ifdef LIBXML_CATALOG_ENABLED
  4854. /**
  4855. * xmlParseCatalogPI:
  4856. * @ctxt: an XML parser context
  4857. * @catalog: the PI value string
  4858. *
  4859. * parse an XML Catalog Processing Instruction.
  4860. *
  4861. * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?>
  4862. *
  4863. * Occurs only if allowed by the user and if happening in the Misc
  4864. * part of the document before any doctype information
  4865. * This will add the given catalog to the parsing context in order
  4866. * to be used if there is a resolution need further down in the document
  4867. */
  4868. static void
  4869. xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
  4870. xmlChar *URL = NULL;
  4871. const xmlChar *tmp, *base;
  4872. xmlChar marker;
  4873. tmp = catalog;
  4874. while (IS_BLANK_CH(*tmp)) tmp++;
  4875. if (xmlStrncmp(tmp, BAD_CAST"catalog", 7))
  4876. goto error;
  4877. tmp += 7;
  4878. while (IS_BLANK_CH(*tmp)) tmp++;
  4879. if (*tmp != '=') {
  4880. return;
  4881. }
  4882. tmp++;
  4883. while (IS_BLANK_CH(*tmp)) tmp++;
  4884. marker = *tmp;
  4885. if ((marker != '\'') && (marker != '"'))
  4886. goto error;
  4887. tmp++;
  4888. base = tmp;
  4889. while ((*tmp != 0) && (*tmp != marker)) tmp++;
  4890. if (*tmp == 0)
  4891. goto error;
  4892. URL = xmlStrndup(base, tmp - base);
  4893. tmp++;
  4894. while (IS_BLANK_CH(*tmp)) tmp++;
  4895. if (*tmp != 0)
  4896. goto error;
  4897. if (URL != NULL) {
  4898. ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
  4899. xmlFree(URL);
  4900. }
  4901. return;
  4902. error:
  4903. xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
  4904. "Catalog PI syntax error: %s\n",
  4905. catalog, NULL);
  4906. if (URL != NULL)
  4907. xmlFree(URL);
  4908. }
  4909. #endif
  4910. /**
  4911. * xmlParsePI:
  4912. * @ctxt: an XML parser context
  4913. *
  4914. * DEPRECATED: Internal function, don't use.
  4915. *
  4916. * parse an XML Processing Instruction.
  4917. *
  4918. * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
  4919. *
  4920. * The processing is transferred to SAX once parsed.
  4921. */
  4922. void
  4923. xmlParsePI(xmlParserCtxtPtr ctxt) {
  4924. xmlChar *buf = NULL;
  4925. size_t len = 0;
  4926. size_t size = XML_PARSER_BUFFER_SIZE;
  4927. size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  4928. XML_MAX_HUGE_LENGTH :
  4929. XML_MAX_TEXT_LENGTH;
  4930. int cur, l;
  4931. const xmlChar *target;
  4932. xmlParserInputState state;
  4933. if ((RAW == '<') && (NXT(1) == '?')) {
  4934. int inputid = ctxt->input->id;
  4935. state = ctxt->instate;
  4936. ctxt->instate = XML_PARSER_PI;
  4937. /*
  4938. * this is a Processing Instruction.
  4939. */
  4940. SKIP(2);
  4941. /*
  4942. * Parse the target name and check for special support like
  4943. * namespace.
  4944. */
  4945. target = xmlParsePITarget(ctxt);
  4946. if (target != NULL) {
  4947. if ((RAW == '?') && (NXT(1) == '>')) {
  4948. if (inputid != ctxt->input->id) {
  4949. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  4950. "PI declaration doesn't start and stop in"
  4951. " the same entity\n");
  4952. }
  4953. SKIP(2);
  4954. /*
  4955. * SAX: PI detected.
  4956. */
  4957. if ((ctxt->sax) && (!ctxt->disableSAX) &&
  4958. (ctxt->sax->processingInstruction != NULL))
  4959. ctxt->sax->processingInstruction(ctxt->userData,
  4960. target, NULL);
  4961. if (ctxt->instate != XML_PARSER_EOF)
  4962. ctxt->instate = state;
  4963. return;
  4964. }
  4965. buf = (xmlChar *) xmlMallocAtomic(size);
  4966. if (buf == NULL) {
  4967. xmlErrMemory(ctxt, NULL);
  4968. ctxt->instate = state;
  4969. return;
  4970. }
  4971. if (SKIP_BLANKS == 0) {
  4972. xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED,
  4973. "ParsePI: PI %s space expected\n", target);
  4974. }
  4975. cur = CUR_CHAR(l);
  4976. while (IS_CHAR(cur) && /* checked */
  4977. ((cur != '?') || (NXT(1) != '>'))) {
  4978. if (len + 5 >= size) {
  4979. xmlChar *tmp;
  4980. size_t new_size = size * 2;
  4981. tmp = (xmlChar *) xmlRealloc(buf, new_size);
  4982. if (tmp == NULL) {
  4983. xmlErrMemory(ctxt, NULL);
  4984. xmlFree(buf);
  4985. ctxt->instate = state;
  4986. return;
  4987. }
  4988. buf = tmp;
  4989. size = new_size;
  4990. }
  4991. COPY_BUF(buf, len, cur);
  4992. if (len > maxLength) {
  4993. xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
  4994. "PI %s too big found", target);
  4995. xmlFree(buf);
  4996. ctxt->instate = state;
  4997. return;
  4998. }
  4999. NEXTL(l);
  5000. cur = CUR_CHAR(l);
  5001. }
  5002. buf[len] = 0;
  5003. if (ctxt->instate == XML_PARSER_EOF) {
  5004. xmlFree(buf);
  5005. return;
  5006. }
  5007. if (cur != '?') {
  5008. xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
  5009. "ParsePI: PI %s never end ...\n", target);
  5010. } else {
  5011. if (inputid != ctxt->input->id) {
  5012. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  5013. "PI declaration doesn't start and stop in"
  5014. " the same entity\n");
  5015. }
  5016. SKIP(2);
  5017. #ifdef LIBXML_CATALOG_ENABLED
  5018. if (((state == XML_PARSER_MISC) ||
  5019. (state == XML_PARSER_START)) &&
  5020. (xmlStrEqual(target, XML_CATALOG_PI))) {
  5021. xmlCatalogAllow allow = xmlCatalogGetDefaults();
  5022. if ((allow == XML_CATA_ALLOW_DOCUMENT) ||
  5023. (allow == XML_CATA_ALLOW_ALL))
  5024. xmlParseCatalogPI(ctxt, buf);
  5025. }
  5026. #endif
  5027. /*
  5028. * SAX: PI detected.
  5029. */
  5030. if ((ctxt->sax) && (!ctxt->disableSAX) &&
  5031. (ctxt->sax->processingInstruction != NULL))
  5032. ctxt->sax->processingInstruction(ctxt->userData,
  5033. target, buf);
  5034. }
  5035. xmlFree(buf);
  5036. } else {
  5037. xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL);
  5038. }
  5039. if (ctxt->instate != XML_PARSER_EOF)
  5040. ctxt->instate = state;
  5041. }
  5042. }
  5043. /**
  5044. * xmlParseNotationDecl:
  5045. * @ctxt: an XML parser context
  5046. *
  5047. * DEPRECATED: Internal function, don't use.
  5048. *
  5049. * Parse a notation declaration. Always consumes '<!'.
  5050. *
  5051. * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
  5052. *
  5053. * Hence there is actually 3 choices:
  5054. * 'PUBLIC' S PubidLiteral
  5055. * 'PUBLIC' S PubidLiteral S SystemLiteral
  5056. * and 'SYSTEM' S SystemLiteral
  5057. *
  5058. * See the NOTE on xmlParseExternalID().
  5059. */
  5060. void
  5061. xmlParseNotationDecl(xmlParserCtxtPtr ctxt) {
  5062. const xmlChar *name;
  5063. xmlChar *Pubid;
  5064. xmlChar *Systemid;
  5065. if ((CUR != '<') || (NXT(1) != '!'))
  5066. return;
  5067. SKIP(2);
  5068. if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
  5069. int inputid = ctxt->input->id;
  5070. SKIP(8);
  5071. if (SKIP_BLANKS == 0) {
  5072. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5073. "Space required after '<!NOTATION'\n");
  5074. return;
  5075. }
  5076. name = xmlParseName(ctxt);
  5077. if (name == NULL) {
  5078. xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
  5079. return;
  5080. }
  5081. if (xmlStrchr(name, ':') != NULL) {
  5082. xmlNsErr(ctxt, XML_NS_ERR_COLON,
  5083. "colons are forbidden from notation names '%s'\n",
  5084. name, NULL, NULL);
  5085. }
  5086. if (SKIP_BLANKS == 0) {
  5087. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5088. "Space required after the NOTATION name'\n");
  5089. return;
  5090. }
  5091. /*
  5092. * Parse the IDs.
  5093. */
  5094. Systemid = xmlParseExternalID(ctxt, &Pubid, 0);
  5095. SKIP_BLANKS;
  5096. if (RAW == '>') {
  5097. if (inputid != ctxt->input->id) {
  5098. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  5099. "Notation declaration doesn't start and stop"
  5100. " in the same entity\n");
  5101. }
  5102. NEXT;
  5103. if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
  5104. (ctxt->sax->notationDecl != NULL))
  5105. ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
  5106. } else {
  5107. xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
  5108. }
  5109. if (Systemid != NULL) xmlFree(Systemid);
  5110. if (Pubid != NULL) xmlFree(Pubid);
  5111. }
  5112. }
  5113. /**
  5114. * xmlParseEntityDecl:
  5115. * @ctxt: an XML parser context
  5116. *
  5117. * DEPRECATED: Internal function, don't use.
  5118. *
  5119. * Parse an entity declaration. Always consumes '<!'.
  5120. *
  5121. * [70] EntityDecl ::= GEDecl | PEDecl
  5122. *
  5123. * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
  5124. *
  5125. * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
  5126. *
  5127. * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
  5128. *
  5129. * [74] PEDef ::= EntityValue | ExternalID
  5130. *
  5131. * [76] NDataDecl ::= S 'NDATA' S Name
  5132. *
  5133. * [ VC: Notation Declared ]
  5134. * The Name must match the declared name of a notation.
  5135. */
  5136. void
  5137. xmlParseEntityDecl(xmlParserCtxtPtr ctxt) {
  5138. const xmlChar *name = NULL;
  5139. xmlChar *value = NULL;
  5140. xmlChar *URI = NULL, *literal = NULL;
  5141. const xmlChar *ndata = NULL;
  5142. int isParameter = 0;
  5143. xmlChar *orig = NULL;
  5144. if ((CUR != '<') || (NXT(1) != '!'))
  5145. return;
  5146. SKIP(2);
  5147. /* GROW; done in the caller */
  5148. if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
  5149. int inputid = ctxt->input->id;
  5150. SKIP(6);
  5151. if (SKIP_BLANKS == 0) {
  5152. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5153. "Space required after '<!ENTITY'\n");
  5154. }
  5155. if (RAW == '%') {
  5156. NEXT;
  5157. if (SKIP_BLANKS == 0) {
  5158. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5159. "Space required after '%%'\n");
  5160. }
  5161. isParameter = 1;
  5162. }
  5163. name = xmlParseName(ctxt);
  5164. if (name == NULL) {
  5165. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  5166. "xmlParseEntityDecl: no name\n");
  5167. return;
  5168. }
  5169. if (xmlStrchr(name, ':') != NULL) {
  5170. xmlNsErr(ctxt, XML_NS_ERR_COLON,
  5171. "colons are forbidden from entities names '%s'\n",
  5172. name, NULL, NULL);
  5173. }
  5174. if (SKIP_BLANKS == 0) {
  5175. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5176. "Space required after the entity name\n");
  5177. }
  5178. ctxt->instate = XML_PARSER_ENTITY_DECL;
  5179. /*
  5180. * handle the various case of definitions...
  5181. */
  5182. if (isParameter) {
  5183. if ((RAW == '"') || (RAW == '\'')) {
  5184. value = xmlParseEntityValue(ctxt, &orig);
  5185. if (value) {
  5186. if ((ctxt->sax != NULL) &&
  5187. (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
  5188. ctxt->sax->entityDecl(ctxt->userData, name,
  5189. XML_INTERNAL_PARAMETER_ENTITY,
  5190. NULL, NULL, value);
  5191. }
  5192. } else {
  5193. URI = xmlParseExternalID(ctxt, &literal, 1);
  5194. if ((URI == NULL) && (literal == NULL)) {
  5195. xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
  5196. }
  5197. if (URI) {
  5198. xmlURIPtr uri;
  5199. uri = xmlParseURI((const char *) URI);
  5200. if (uri == NULL) {
  5201. xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
  5202. "Invalid URI: %s\n", URI);
  5203. /*
  5204. * This really ought to be a well formedness error
  5205. * but the XML Core WG decided otherwise c.f. issue
  5206. * E26 of the XML erratas.
  5207. */
  5208. } else {
  5209. if (uri->fragment != NULL) {
  5210. /*
  5211. * Okay this is foolish to block those but not
  5212. * invalid URIs.
  5213. */
  5214. xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
  5215. } else {
  5216. if ((ctxt->sax != NULL) &&
  5217. (!ctxt->disableSAX) &&
  5218. (ctxt->sax->entityDecl != NULL))
  5219. ctxt->sax->entityDecl(ctxt->userData, name,
  5220. XML_EXTERNAL_PARAMETER_ENTITY,
  5221. literal, URI, NULL);
  5222. }
  5223. xmlFreeURI(uri);
  5224. }
  5225. }
  5226. }
  5227. } else {
  5228. if ((RAW == '"') || (RAW == '\'')) {
  5229. value = xmlParseEntityValue(ctxt, &orig);
  5230. if ((ctxt->sax != NULL) &&
  5231. (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
  5232. ctxt->sax->entityDecl(ctxt->userData, name,
  5233. XML_INTERNAL_GENERAL_ENTITY,
  5234. NULL, NULL, value);
  5235. /*
  5236. * For expat compatibility in SAX mode.
  5237. */
  5238. if ((ctxt->myDoc == NULL) ||
  5239. (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
  5240. if (ctxt->myDoc == NULL) {
  5241. ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
  5242. if (ctxt->myDoc == NULL) {
  5243. xmlErrMemory(ctxt, "New Doc failed");
  5244. goto done;
  5245. }
  5246. ctxt->myDoc->properties = XML_DOC_INTERNAL;
  5247. }
  5248. if (ctxt->myDoc->intSubset == NULL)
  5249. ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
  5250. BAD_CAST "fake", NULL, NULL);
  5251. xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
  5252. NULL, NULL, value);
  5253. }
  5254. } else {
  5255. URI = xmlParseExternalID(ctxt, &literal, 1);
  5256. if ((URI == NULL) && (literal == NULL)) {
  5257. xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
  5258. }
  5259. if (URI) {
  5260. xmlURIPtr uri;
  5261. uri = xmlParseURI((const char *)URI);
  5262. if (uri == NULL) {
  5263. xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
  5264. "Invalid URI: %s\n", URI);
  5265. /*
  5266. * This really ought to be a well formedness error
  5267. * but the XML Core WG decided otherwise c.f. issue
  5268. * E26 of the XML erratas.
  5269. */
  5270. } else {
  5271. if (uri->fragment != NULL) {
  5272. /*
  5273. * Okay this is foolish to block those but not
  5274. * invalid URIs.
  5275. */
  5276. xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
  5277. }
  5278. xmlFreeURI(uri);
  5279. }
  5280. }
  5281. if ((RAW != '>') && (SKIP_BLANKS == 0)) {
  5282. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5283. "Space required before 'NDATA'\n");
  5284. }
  5285. if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) {
  5286. SKIP(5);
  5287. if (SKIP_BLANKS == 0) {
  5288. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5289. "Space required after 'NDATA'\n");
  5290. }
  5291. ndata = xmlParseName(ctxt);
  5292. if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
  5293. (ctxt->sax->unparsedEntityDecl != NULL))
  5294. ctxt->sax->unparsedEntityDecl(ctxt->userData, name,
  5295. literal, URI, ndata);
  5296. } else {
  5297. if ((ctxt->sax != NULL) &&
  5298. (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
  5299. ctxt->sax->entityDecl(ctxt->userData, name,
  5300. XML_EXTERNAL_GENERAL_PARSED_ENTITY,
  5301. literal, URI, NULL);
  5302. /*
  5303. * For expat compatibility in SAX mode.
  5304. * assuming the entity replacement was asked for
  5305. */
  5306. if ((ctxt->replaceEntities != 0) &&
  5307. ((ctxt->myDoc == NULL) ||
  5308. (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
  5309. if (ctxt->myDoc == NULL) {
  5310. ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
  5311. if (ctxt->myDoc == NULL) {
  5312. xmlErrMemory(ctxt, "New Doc failed");
  5313. goto done;
  5314. }
  5315. ctxt->myDoc->properties = XML_DOC_INTERNAL;
  5316. }
  5317. if (ctxt->myDoc->intSubset == NULL)
  5318. ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
  5319. BAD_CAST "fake", NULL, NULL);
  5320. xmlSAX2EntityDecl(ctxt, name,
  5321. XML_EXTERNAL_GENERAL_PARSED_ENTITY,
  5322. literal, URI, NULL);
  5323. }
  5324. }
  5325. }
  5326. }
  5327. if (ctxt->instate == XML_PARSER_EOF)
  5328. goto done;
  5329. SKIP_BLANKS;
  5330. if (RAW != '>') {
  5331. xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
  5332. "xmlParseEntityDecl: entity %s not terminated\n", name);
  5333. xmlHaltParser(ctxt);
  5334. } else {
  5335. if (inputid != ctxt->input->id) {
  5336. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  5337. "Entity declaration doesn't start and stop in"
  5338. " the same entity\n");
  5339. }
  5340. NEXT;
  5341. }
  5342. if (orig != NULL) {
  5343. /*
  5344. * Ugly mechanism to save the raw entity value.
  5345. */
  5346. xmlEntityPtr cur = NULL;
  5347. if (isParameter) {
  5348. if ((ctxt->sax != NULL) &&
  5349. (ctxt->sax->getParameterEntity != NULL))
  5350. cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
  5351. } else {
  5352. if ((ctxt->sax != NULL) &&
  5353. (ctxt->sax->getEntity != NULL))
  5354. cur = ctxt->sax->getEntity(ctxt->userData, name);
  5355. if ((cur == NULL) && (ctxt->userData==ctxt)) {
  5356. cur = xmlSAX2GetEntity(ctxt, name);
  5357. }
  5358. }
  5359. if ((cur != NULL) && (cur->orig == NULL)) {
  5360. cur->orig = orig;
  5361. orig = NULL;
  5362. }
  5363. }
  5364. done:
  5365. if (value != NULL) xmlFree(value);
  5366. if (URI != NULL) xmlFree(URI);
  5367. if (literal != NULL) xmlFree(literal);
  5368. if (orig != NULL) xmlFree(orig);
  5369. }
  5370. }
  5371. /**
  5372. * xmlParseDefaultDecl:
  5373. * @ctxt: an XML parser context
  5374. * @value: Receive a possible fixed default value for the attribute
  5375. *
  5376. * DEPRECATED: Internal function, don't use.
  5377. *
  5378. * Parse an attribute default declaration
  5379. *
  5380. * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
  5381. *
  5382. * [ VC: Required Attribute ]
  5383. * if the default declaration is the keyword #REQUIRED, then the
  5384. * attribute must be specified for all elements of the type in the
  5385. * attribute-list declaration.
  5386. *
  5387. * [ VC: Attribute Default Legal ]
  5388. * The declared default value must meet the lexical constraints of
  5389. * the declared attribute type c.f. xmlValidateAttributeDecl()
  5390. *
  5391. * [ VC: Fixed Attribute Default ]
  5392. * if an attribute has a default value declared with the #FIXED
  5393. * keyword, instances of that attribute must match the default value.
  5394. *
  5395. * [ WFC: No < in Attribute Values ]
  5396. * handled in xmlParseAttValue()
  5397. *
  5398. * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED
  5399. * or XML_ATTRIBUTE_FIXED.
  5400. */
  5401. int
  5402. xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) {
  5403. int val;
  5404. xmlChar *ret;
  5405. *value = NULL;
  5406. if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) {
  5407. SKIP(9);
  5408. return(XML_ATTRIBUTE_REQUIRED);
  5409. }
  5410. if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) {
  5411. SKIP(8);
  5412. return(XML_ATTRIBUTE_IMPLIED);
  5413. }
  5414. val = XML_ATTRIBUTE_NONE;
  5415. if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) {
  5416. SKIP(6);
  5417. val = XML_ATTRIBUTE_FIXED;
  5418. if (SKIP_BLANKS == 0) {
  5419. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5420. "Space required after '#FIXED'\n");
  5421. }
  5422. }
  5423. ret = xmlParseAttValue(ctxt);
  5424. ctxt->instate = XML_PARSER_DTD;
  5425. if (ret == NULL) {
  5426. xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo,
  5427. "Attribute default value declaration error\n");
  5428. } else
  5429. *value = ret;
  5430. return(val);
  5431. }
  5432. /**
  5433. * xmlParseNotationType:
  5434. * @ctxt: an XML parser context
  5435. *
  5436. * DEPRECATED: Internal function, don't use.
  5437. *
  5438. * parse an Notation attribute type.
  5439. *
  5440. * Note: the leading 'NOTATION' S part has already being parsed...
  5441. *
  5442. * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
  5443. *
  5444. * [ VC: Notation Attributes ]
  5445. * Values of this type must match one of the notation names included
  5446. * in the declaration; all notation names in the declaration must be declared.
  5447. *
  5448. * Returns: the notation attribute tree built while parsing
  5449. */
  5450. xmlEnumerationPtr
  5451. xmlParseNotationType(xmlParserCtxtPtr ctxt) {
  5452. const xmlChar *name;
  5453. xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp;
  5454. if (RAW != '(') {
  5455. xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
  5456. return(NULL);
  5457. }
  5458. do {
  5459. NEXT;
  5460. SKIP_BLANKS;
  5461. name = xmlParseName(ctxt);
  5462. if (name == NULL) {
  5463. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  5464. "Name expected in NOTATION declaration\n");
  5465. xmlFreeEnumeration(ret);
  5466. return(NULL);
  5467. }
  5468. tmp = ret;
  5469. while (tmp != NULL) {
  5470. if (xmlStrEqual(name, tmp->name)) {
  5471. xmlValidityError(ctxt, XML_DTD_DUP_TOKEN,
  5472. "standalone: attribute notation value token %s duplicated\n",
  5473. name, NULL);
  5474. if (!xmlDictOwns(ctxt->dict, name))
  5475. xmlFree((xmlChar *) name);
  5476. break;
  5477. }
  5478. tmp = tmp->next;
  5479. }
  5480. if (tmp == NULL) {
  5481. cur = xmlCreateEnumeration(name);
  5482. if (cur == NULL) {
  5483. xmlFreeEnumeration(ret);
  5484. return(NULL);
  5485. }
  5486. if (last == NULL) ret = last = cur;
  5487. else {
  5488. last->next = cur;
  5489. last = cur;
  5490. }
  5491. }
  5492. SKIP_BLANKS;
  5493. } while (RAW == '|');
  5494. if (RAW != ')') {
  5495. xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
  5496. xmlFreeEnumeration(ret);
  5497. return(NULL);
  5498. }
  5499. NEXT;
  5500. return(ret);
  5501. }
  5502. /**
  5503. * xmlParseEnumerationType:
  5504. * @ctxt: an XML parser context
  5505. *
  5506. * DEPRECATED: Internal function, don't use.
  5507. *
  5508. * parse an Enumeration attribute type.
  5509. *
  5510. * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
  5511. *
  5512. * [ VC: Enumeration ]
  5513. * Values of this type must match one of the Nmtoken tokens in
  5514. * the declaration
  5515. *
  5516. * Returns: the enumeration attribute tree built while parsing
  5517. */
  5518. xmlEnumerationPtr
  5519. xmlParseEnumerationType(xmlParserCtxtPtr ctxt) {
  5520. xmlChar *name;
  5521. xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp;
  5522. if (RAW != '(') {
  5523. xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL);
  5524. return(NULL);
  5525. }
  5526. do {
  5527. NEXT;
  5528. SKIP_BLANKS;
  5529. name = xmlParseNmtoken(ctxt);
  5530. if (name == NULL) {
  5531. xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL);
  5532. return(ret);
  5533. }
  5534. tmp = ret;
  5535. while (tmp != NULL) {
  5536. if (xmlStrEqual(name, tmp->name)) {
  5537. xmlValidityError(ctxt, XML_DTD_DUP_TOKEN,
  5538. "standalone: attribute enumeration value token %s duplicated\n",
  5539. name, NULL);
  5540. if (!xmlDictOwns(ctxt->dict, name))
  5541. xmlFree(name);
  5542. break;
  5543. }
  5544. tmp = tmp->next;
  5545. }
  5546. if (tmp == NULL) {
  5547. cur = xmlCreateEnumeration(name);
  5548. if (!xmlDictOwns(ctxt->dict, name))
  5549. xmlFree(name);
  5550. if (cur == NULL) {
  5551. xmlFreeEnumeration(ret);
  5552. return(NULL);
  5553. }
  5554. if (last == NULL) ret = last = cur;
  5555. else {
  5556. last->next = cur;
  5557. last = cur;
  5558. }
  5559. }
  5560. SKIP_BLANKS;
  5561. } while (RAW == '|');
  5562. if (RAW != ')') {
  5563. xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL);
  5564. return(ret);
  5565. }
  5566. NEXT;
  5567. return(ret);
  5568. }
  5569. /**
  5570. * xmlParseEnumeratedType:
  5571. * @ctxt: an XML parser context
  5572. * @tree: the enumeration tree built while parsing
  5573. *
  5574. * DEPRECATED: Internal function, don't use.
  5575. *
  5576. * parse an Enumerated attribute type.
  5577. *
  5578. * [57] EnumeratedType ::= NotationType | Enumeration
  5579. *
  5580. * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
  5581. *
  5582. *
  5583. * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION
  5584. */
  5585. int
  5586. xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
  5587. if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
  5588. SKIP(8);
  5589. if (SKIP_BLANKS == 0) {
  5590. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5591. "Space required after 'NOTATION'\n");
  5592. return(0);
  5593. }
  5594. *tree = xmlParseNotationType(ctxt);
  5595. if (*tree == NULL) return(0);
  5596. return(XML_ATTRIBUTE_NOTATION);
  5597. }
  5598. *tree = xmlParseEnumerationType(ctxt);
  5599. if (*tree == NULL) return(0);
  5600. return(XML_ATTRIBUTE_ENUMERATION);
  5601. }
  5602. /**
  5603. * xmlParseAttributeType:
  5604. * @ctxt: an XML parser context
  5605. * @tree: the enumeration tree built while parsing
  5606. *
  5607. * DEPRECATED: Internal function, don't use.
  5608. *
  5609. * parse the Attribute list def for an element
  5610. *
  5611. * [54] AttType ::= StringType | TokenizedType | EnumeratedType
  5612. *
  5613. * [55] StringType ::= 'CDATA'
  5614. *
  5615. * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' |
  5616. * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
  5617. *
  5618. * Validity constraints for attribute values syntax are checked in
  5619. * xmlValidateAttributeValue()
  5620. *
  5621. * [ VC: ID ]
  5622. * Values of type ID must match the Name production. A name must not
  5623. * appear more than once in an XML document as a value of this type;
  5624. * i.e., ID values must uniquely identify the elements which bear them.
  5625. *
  5626. * [ VC: One ID per Element Type ]
  5627. * No element type may have more than one ID attribute specified.
  5628. *
  5629. * [ VC: ID Attribute Default ]
  5630. * An ID attribute must have a declared default of #IMPLIED or #REQUIRED.
  5631. *
  5632. * [ VC: IDREF ]
  5633. * Values of type IDREF must match the Name production, and values
  5634. * of type IDREFS must match Names; each IDREF Name must match the value
  5635. * of an ID attribute on some element in the XML document; i.e. IDREF
  5636. * values must match the value of some ID attribute.
  5637. *
  5638. * [ VC: Entity Name ]
  5639. * Values of type ENTITY must match the Name production, values
  5640. * of type ENTITIES must match Names; each Entity Name must match the
  5641. * name of an unparsed entity declared in the DTD.
  5642. *
  5643. * [ VC: Name Token ]
  5644. * Values of type NMTOKEN must match the Nmtoken production; values
  5645. * of type NMTOKENS must match Nmtokens.
  5646. *
  5647. * Returns the attribute type
  5648. */
  5649. int
  5650. xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
  5651. if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) {
  5652. SKIP(5);
  5653. return(XML_ATTRIBUTE_CDATA);
  5654. } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) {
  5655. SKIP(6);
  5656. return(XML_ATTRIBUTE_IDREFS);
  5657. } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) {
  5658. SKIP(5);
  5659. return(XML_ATTRIBUTE_IDREF);
  5660. } else if ((RAW == 'I') && (NXT(1) == 'D')) {
  5661. SKIP(2);
  5662. return(XML_ATTRIBUTE_ID);
  5663. } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
  5664. SKIP(6);
  5665. return(XML_ATTRIBUTE_ENTITY);
  5666. } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) {
  5667. SKIP(8);
  5668. return(XML_ATTRIBUTE_ENTITIES);
  5669. } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) {
  5670. SKIP(8);
  5671. return(XML_ATTRIBUTE_NMTOKENS);
  5672. } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) {
  5673. SKIP(7);
  5674. return(XML_ATTRIBUTE_NMTOKEN);
  5675. }
  5676. return(xmlParseEnumeratedType(ctxt, tree));
  5677. }
  5678. /**
  5679. * xmlParseAttributeListDecl:
  5680. * @ctxt: an XML parser context
  5681. *
  5682. * DEPRECATED: Internal function, don't use.
  5683. *
  5684. * Parse an attribute list declaration for an element. Always consumes '<!'.
  5685. *
  5686. * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
  5687. *
  5688. * [53] AttDef ::= S Name S AttType S DefaultDecl
  5689. *
  5690. */
  5691. void
  5692. xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) {
  5693. const xmlChar *elemName;
  5694. const xmlChar *attrName;
  5695. xmlEnumerationPtr tree;
  5696. if ((CUR != '<') || (NXT(1) != '!'))
  5697. return;
  5698. SKIP(2);
  5699. if (CMP7(CUR_PTR, 'A', 'T', 'T', 'L', 'I', 'S', 'T')) {
  5700. int inputid = ctxt->input->id;
  5701. SKIP(7);
  5702. if (SKIP_BLANKS == 0) {
  5703. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5704. "Space required after '<!ATTLIST'\n");
  5705. }
  5706. elemName = xmlParseName(ctxt);
  5707. if (elemName == NULL) {
  5708. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  5709. "ATTLIST: no name for Element\n");
  5710. return;
  5711. }
  5712. SKIP_BLANKS;
  5713. GROW;
  5714. while ((RAW != '>') && (ctxt->instate != XML_PARSER_EOF)) {
  5715. int type;
  5716. int def;
  5717. xmlChar *defaultValue = NULL;
  5718. GROW;
  5719. tree = NULL;
  5720. attrName = xmlParseName(ctxt);
  5721. if (attrName == NULL) {
  5722. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  5723. "ATTLIST: no name for Attribute\n");
  5724. break;
  5725. }
  5726. GROW;
  5727. if (SKIP_BLANKS == 0) {
  5728. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5729. "Space required after the attribute name\n");
  5730. break;
  5731. }
  5732. type = xmlParseAttributeType(ctxt, &tree);
  5733. if (type <= 0) {
  5734. break;
  5735. }
  5736. GROW;
  5737. if (SKIP_BLANKS == 0) {
  5738. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5739. "Space required after the attribute type\n");
  5740. if (tree != NULL)
  5741. xmlFreeEnumeration(tree);
  5742. break;
  5743. }
  5744. def = xmlParseDefaultDecl(ctxt, &defaultValue);
  5745. if (def <= 0) {
  5746. if (defaultValue != NULL)
  5747. xmlFree(defaultValue);
  5748. if (tree != NULL)
  5749. xmlFreeEnumeration(tree);
  5750. break;
  5751. }
  5752. if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL))
  5753. xmlAttrNormalizeSpace(defaultValue, defaultValue);
  5754. GROW;
  5755. if (RAW != '>') {
  5756. if (SKIP_BLANKS == 0) {
  5757. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  5758. "Space required after the attribute default value\n");
  5759. if (defaultValue != NULL)
  5760. xmlFree(defaultValue);
  5761. if (tree != NULL)
  5762. xmlFreeEnumeration(tree);
  5763. break;
  5764. }
  5765. }
  5766. if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
  5767. (ctxt->sax->attributeDecl != NULL))
  5768. ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
  5769. type, def, defaultValue, tree);
  5770. else if (tree != NULL)
  5771. xmlFreeEnumeration(tree);
  5772. if ((ctxt->sax2) && (defaultValue != NULL) &&
  5773. (def != XML_ATTRIBUTE_IMPLIED) &&
  5774. (def != XML_ATTRIBUTE_REQUIRED)) {
  5775. xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue);
  5776. }
  5777. if (ctxt->sax2) {
  5778. xmlAddSpecialAttr(ctxt, elemName, attrName, type);
  5779. }
  5780. if (defaultValue != NULL)
  5781. xmlFree(defaultValue);
  5782. GROW;
  5783. }
  5784. if (RAW == '>') {
  5785. if (inputid != ctxt->input->id) {
  5786. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  5787. "Attribute list declaration doesn't start and"
  5788. " stop in the same entity\n");
  5789. }
  5790. NEXT;
  5791. }
  5792. }
  5793. }
  5794. /**
  5795. * xmlParseElementMixedContentDecl:
  5796. * @ctxt: an XML parser context
  5797. * @inputchk: the input used for the current entity, needed for boundary checks
  5798. *
  5799. * DEPRECATED: Internal function, don't use.
  5800. *
  5801. * parse the declaration for a Mixed Element content
  5802. * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
  5803. *
  5804. * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' |
  5805. * '(' S? '#PCDATA' S? ')'
  5806. *
  5807. * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49])
  5808. *
  5809. * [ VC: No Duplicate Types ]
  5810. * The same name must not appear more than once in a single
  5811. * mixed-content declaration.
  5812. *
  5813. * returns: the list of the xmlElementContentPtr describing the element choices
  5814. */
  5815. xmlElementContentPtr
  5816. xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) {
  5817. xmlElementContentPtr ret = NULL, cur = NULL, n;
  5818. const xmlChar *elem = NULL;
  5819. GROW;
  5820. if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
  5821. SKIP(7);
  5822. SKIP_BLANKS;
  5823. if (RAW == ')') {
  5824. if (ctxt->input->id != inputchk) {
  5825. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  5826. "Element content declaration doesn't start and"
  5827. " stop in the same entity\n");
  5828. }
  5829. NEXT;
  5830. ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
  5831. if (ret == NULL)
  5832. return(NULL);
  5833. if (RAW == '*') {
  5834. ret->ocur = XML_ELEMENT_CONTENT_MULT;
  5835. NEXT;
  5836. }
  5837. return(ret);
  5838. }
  5839. if ((RAW == '(') || (RAW == '|')) {
  5840. ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
  5841. if (ret == NULL) return(NULL);
  5842. }
  5843. while ((RAW == '|') && (ctxt->instate != XML_PARSER_EOF)) {
  5844. NEXT;
  5845. if (elem == NULL) {
  5846. ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
  5847. if (ret == NULL) {
  5848. xmlFreeDocElementContent(ctxt->myDoc, cur);
  5849. return(NULL);
  5850. }
  5851. ret->c1 = cur;
  5852. if (cur != NULL)
  5853. cur->parent = ret;
  5854. cur = ret;
  5855. } else {
  5856. n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
  5857. if (n == NULL) {
  5858. xmlFreeDocElementContent(ctxt->myDoc, ret);
  5859. return(NULL);
  5860. }
  5861. n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
  5862. if (n->c1 != NULL)
  5863. n->c1->parent = n;
  5864. cur->c2 = n;
  5865. if (n != NULL)
  5866. n->parent = cur;
  5867. cur = n;
  5868. }
  5869. SKIP_BLANKS;
  5870. elem = xmlParseName(ctxt);
  5871. if (elem == NULL) {
  5872. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  5873. "xmlParseElementMixedContentDecl : Name expected\n");
  5874. xmlFreeDocElementContent(ctxt->myDoc, ret);
  5875. return(NULL);
  5876. }
  5877. SKIP_BLANKS;
  5878. GROW;
  5879. }
  5880. if ((RAW == ')') && (NXT(1) == '*')) {
  5881. if (elem != NULL) {
  5882. cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem,
  5883. XML_ELEMENT_CONTENT_ELEMENT);
  5884. if (cur->c2 != NULL)
  5885. cur->c2->parent = cur;
  5886. }
  5887. if (ret != NULL)
  5888. ret->ocur = XML_ELEMENT_CONTENT_MULT;
  5889. if (ctxt->input->id != inputchk) {
  5890. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  5891. "Element content declaration doesn't start and"
  5892. " stop in the same entity\n");
  5893. }
  5894. SKIP(2);
  5895. } else {
  5896. xmlFreeDocElementContent(ctxt->myDoc, ret);
  5897. xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL);
  5898. return(NULL);
  5899. }
  5900. } else {
  5901. xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL);
  5902. }
  5903. return(ret);
  5904. }
  5905. /**
  5906. * xmlParseElementChildrenContentDeclPriv:
  5907. * @ctxt: an XML parser context
  5908. * @inputchk: the input used for the current entity, needed for boundary checks
  5909. * @depth: the level of recursion
  5910. *
  5911. * parse the declaration for a Mixed Element content
  5912. * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
  5913. *
  5914. *
  5915. * [47] children ::= (choice | seq) ('?' | '*' | '+')?
  5916. *
  5917. * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
  5918. *
  5919. * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
  5920. *
  5921. * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
  5922. *
  5923. * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
  5924. * TODO Parameter-entity replacement text must be properly nested
  5925. * with parenthesized groups. That is to say, if either of the
  5926. * opening or closing parentheses in a choice, seq, or Mixed
  5927. * construct is contained in the replacement text for a parameter
  5928. * entity, both must be contained in the same replacement text. For
  5929. * interoperability, if a parameter-entity reference appears in a
  5930. * choice, seq, or Mixed construct, its replacement text should not
  5931. * be empty, and neither the first nor last non-blank character of
  5932. * the replacement text should be a connector (| or ,).
  5933. *
  5934. * Returns the tree of xmlElementContentPtr describing the element
  5935. * hierarchy.
  5936. */
  5937. static xmlElementContentPtr
  5938. xmlParseElementChildrenContentDeclPriv(xmlParserCtxtPtr ctxt, int inputchk,
  5939. int depth) {
  5940. xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL;
  5941. const xmlChar *elem;
  5942. xmlChar type = 0;
  5943. if (((depth > 128) && ((ctxt->options & XML_PARSE_HUGE) == 0)) ||
  5944. (depth > 2048)) {
  5945. xmlFatalErrMsgInt(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED,
  5946. "xmlParseElementChildrenContentDecl : depth %d too deep, use XML_PARSE_HUGE\n",
  5947. depth);
  5948. return(NULL);
  5949. }
  5950. SKIP_BLANKS;
  5951. GROW;
  5952. if (RAW == '(') {
  5953. int inputid = ctxt->input->id;
  5954. /* Recurse on first child */
  5955. NEXT;
  5956. SKIP_BLANKS;
  5957. cur = ret = xmlParseElementChildrenContentDeclPriv(ctxt, inputid,
  5958. depth + 1);
  5959. if (cur == NULL)
  5960. return(NULL);
  5961. SKIP_BLANKS;
  5962. GROW;
  5963. } else {
  5964. elem = xmlParseName(ctxt);
  5965. if (elem == NULL) {
  5966. xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
  5967. return(NULL);
  5968. }
  5969. cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
  5970. if (cur == NULL) {
  5971. xmlErrMemory(ctxt, NULL);
  5972. return(NULL);
  5973. }
  5974. GROW;
  5975. if (RAW == '?') {
  5976. cur->ocur = XML_ELEMENT_CONTENT_OPT;
  5977. NEXT;
  5978. } else if (RAW == '*') {
  5979. cur->ocur = XML_ELEMENT_CONTENT_MULT;
  5980. NEXT;
  5981. } else if (RAW == '+') {
  5982. cur->ocur = XML_ELEMENT_CONTENT_PLUS;
  5983. NEXT;
  5984. } else {
  5985. cur->ocur = XML_ELEMENT_CONTENT_ONCE;
  5986. }
  5987. GROW;
  5988. }
  5989. SKIP_BLANKS;
  5990. while ((RAW != ')') && (ctxt->instate != XML_PARSER_EOF)) {
  5991. /*
  5992. * Each loop we parse one separator and one element.
  5993. */
  5994. if (RAW == ',') {
  5995. if (type == 0) type = CUR;
  5996. /*
  5997. * Detect "Name | Name , Name" error
  5998. */
  5999. else if (type != CUR) {
  6000. xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
  6001. "xmlParseElementChildrenContentDecl : '%c' expected\n",
  6002. type);
  6003. if ((last != NULL) && (last != ret))
  6004. xmlFreeDocElementContent(ctxt->myDoc, last);
  6005. if (ret != NULL)
  6006. xmlFreeDocElementContent(ctxt->myDoc, ret);
  6007. return(NULL);
  6008. }
  6009. NEXT;
  6010. op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ);
  6011. if (op == NULL) {
  6012. if ((last != NULL) && (last != ret))
  6013. xmlFreeDocElementContent(ctxt->myDoc, last);
  6014. xmlFreeDocElementContent(ctxt->myDoc, ret);
  6015. return(NULL);
  6016. }
  6017. if (last == NULL) {
  6018. op->c1 = ret;
  6019. if (ret != NULL)
  6020. ret->parent = op;
  6021. ret = cur = op;
  6022. } else {
  6023. cur->c2 = op;
  6024. if (op != NULL)
  6025. op->parent = cur;
  6026. op->c1 = last;
  6027. if (last != NULL)
  6028. last->parent = op;
  6029. cur =op;
  6030. last = NULL;
  6031. }
  6032. } else if (RAW == '|') {
  6033. if (type == 0) type = CUR;
  6034. /*
  6035. * Detect "Name , Name | Name" error
  6036. */
  6037. else if (type != CUR) {
  6038. xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
  6039. "xmlParseElementChildrenContentDecl : '%c' expected\n",
  6040. type);
  6041. if ((last != NULL) && (last != ret))
  6042. xmlFreeDocElementContent(ctxt->myDoc, last);
  6043. if (ret != NULL)
  6044. xmlFreeDocElementContent(ctxt->myDoc, ret);
  6045. return(NULL);
  6046. }
  6047. NEXT;
  6048. op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
  6049. if (op == NULL) {
  6050. if ((last != NULL) && (last != ret))
  6051. xmlFreeDocElementContent(ctxt->myDoc, last);
  6052. if (ret != NULL)
  6053. xmlFreeDocElementContent(ctxt->myDoc, ret);
  6054. return(NULL);
  6055. }
  6056. if (last == NULL) {
  6057. op->c1 = ret;
  6058. if (ret != NULL)
  6059. ret->parent = op;
  6060. ret = cur = op;
  6061. } else {
  6062. cur->c2 = op;
  6063. if (op != NULL)
  6064. op->parent = cur;
  6065. op->c1 = last;
  6066. if (last != NULL)
  6067. last->parent = op;
  6068. cur =op;
  6069. last = NULL;
  6070. }
  6071. } else {
  6072. xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL);
  6073. if ((last != NULL) && (last != ret))
  6074. xmlFreeDocElementContent(ctxt->myDoc, last);
  6075. if (ret != NULL)
  6076. xmlFreeDocElementContent(ctxt->myDoc, ret);
  6077. return(NULL);
  6078. }
  6079. GROW;
  6080. SKIP_BLANKS;
  6081. GROW;
  6082. if (RAW == '(') {
  6083. int inputid = ctxt->input->id;
  6084. /* Recurse on second child */
  6085. NEXT;
  6086. SKIP_BLANKS;
  6087. last = xmlParseElementChildrenContentDeclPriv(ctxt, inputid,
  6088. depth + 1);
  6089. if (last == NULL) {
  6090. if (ret != NULL)
  6091. xmlFreeDocElementContent(ctxt->myDoc, ret);
  6092. return(NULL);
  6093. }
  6094. SKIP_BLANKS;
  6095. } else {
  6096. elem = xmlParseName(ctxt);
  6097. if (elem == NULL) {
  6098. xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
  6099. if (ret != NULL)
  6100. xmlFreeDocElementContent(ctxt->myDoc, ret);
  6101. return(NULL);
  6102. }
  6103. last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
  6104. if (last == NULL) {
  6105. if (ret != NULL)
  6106. xmlFreeDocElementContent(ctxt->myDoc, ret);
  6107. return(NULL);
  6108. }
  6109. if (RAW == '?') {
  6110. last->ocur = XML_ELEMENT_CONTENT_OPT;
  6111. NEXT;
  6112. } else if (RAW == '*') {
  6113. last->ocur = XML_ELEMENT_CONTENT_MULT;
  6114. NEXT;
  6115. } else if (RAW == '+') {
  6116. last->ocur = XML_ELEMENT_CONTENT_PLUS;
  6117. NEXT;
  6118. } else {
  6119. last->ocur = XML_ELEMENT_CONTENT_ONCE;
  6120. }
  6121. }
  6122. SKIP_BLANKS;
  6123. GROW;
  6124. }
  6125. if ((cur != NULL) && (last != NULL)) {
  6126. cur->c2 = last;
  6127. if (last != NULL)
  6128. last->parent = cur;
  6129. }
  6130. if (ctxt->input->id != inputchk) {
  6131. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  6132. "Element content declaration doesn't start and stop in"
  6133. " the same entity\n");
  6134. }
  6135. NEXT;
  6136. if (RAW == '?') {
  6137. if (ret != NULL) {
  6138. if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) ||
  6139. (ret->ocur == XML_ELEMENT_CONTENT_MULT))
  6140. ret->ocur = XML_ELEMENT_CONTENT_MULT;
  6141. else
  6142. ret->ocur = XML_ELEMENT_CONTENT_OPT;
  6143. }
  6144. NEXT;
  6145. } else if (RAW == '*') {
  6146. if (ret != NULL) {
  6147. ret->ocur = XML_ELEMENT_CONTENT_MULT;
  6148. cur = ret;
  6149. /*
  6150. * Some normalization:
  6151. * (a | b* | c?)* == (a | b | c)*
  6152. */
  6153. while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) {
  6154. if ((cur->c1 != NULL) &&
  6155. ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
  6156. (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT)))
  6157. cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
  6158. if ((cur->c2 != NULL) &&
  6159. ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
  6160. (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT)))
  6161. cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
  6162. cur = cur->c2;
  6163. }
  6164. }
  6165. NEXT;
  6166. } else if (RAW == '+') {
  6167. if (ret != NULL) {
  6168. int found = 0;
  6169. if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) ||
  6170. (ret->ocur == XML_ELEMENT_CONTENT_MULT))
  6171. ret->ocur = XML_ELEMENT_CONTENT_MULT;
  6172. else
  6173. ret->ocur = XML_ELEMENT_CONTENT_PLUS;
  6174. /*
  6175. * Some normalization:
  6176. * (a | b*)+ == (a | b)*
  6177. * (a | b?)+ == (a | b)*
  6178. */
  6179. while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) {
  6180. if ((cur->c1 != NULL) &&
  6181. ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
  6182. (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
  6183. cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
  6184. found = 1;
  6185. }
  6186. if ((cur->c2 != NULL) &&
  6187. ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
  6188. (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) {
  6189. cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
  6190. found = 1;
  6191. }
  6192. cur = cur->c2;
  6193. }
  6194. if (found)
  6195. ret->ocur = XML_ELEMENT_CONTENT_MULT;
  6196. }
  6197. NEXT;
  6198. }
  6199. return(ret);
  6200. }
  6201. /**
  6202. * xmlParseElementChildrenContentDecl:
  6203. * @ctxt: an XML parser context
  6204. * @inputchk: the input used for the current entity, needed for boundary checks
  6205. *
  6206. * DEPRECATED: Internal function, don't use.
  6207. *
  6208. * parse the declaration for a Mixed Element content
  6209. * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
  6210. *
  6211. * [47] children ::= (choice | seq) ('?' | '*' | '+')?
  6212. *
  6213. * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
  6214. *
  6215. * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
  6216. *
  6217. * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
  6218. *
  6219. * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
  6220. * TODO Parameter-entity replacement text must be properly nested
  6221. * with parenthesized groups. That is to say, if either of the
  6222. * opening or closing parentheses in a choice, seq, or Mixed
  6223. * construct is contained in the replacement text for a parameter
  6224. * entity, both must be contained in the same replacement text. For
  6225. * interoperability, if a parameter-entity reference appears in a
  6226. * choice, seq, or Mixed construct, its replacement text should not
  6227. * be empty, and neither the first nor last non-blank character of
  6228. * the replacement text should be a connector (| or ,).
  6229. *
  6230. * Returns the tree of xmlElementContentPtr describing the element
  6231. * hierarchy.
  6232. */
  6233. xmlElementContentPtr
  6234. xmlParseElementChildrenContentDecl(xmlParserCtxtPtr ctxt, int inputchk) {
  6235. /* stub left for API/ABI compat */
  6236. return(xmlParseElementChildrenContentDeclPriv(ctxt, inputchk, 1));
  6237. }
  6238. /**
  6239. * xmlParseElementContentDecl:
  6240. * @ctxt: an XML parser context
  6241. * @name: the name of the element being defined.
  6242. * @result: the Element Content pointer will be stored here if any
  6243. *
  6244. * DEPRECATED: Internal function, don't use.
  6245. *
  6246. * parse the declaration for an Element content either Mixed or Children,
  6247. * the cases EMPTY and ANY are handled directly in xmlParseElementDecl
  6248. *
  6249. * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
  6250. *
  6251. * returns: the type of element content XML_ELEMENT_TYPE_xxx
  6252. */
  6253. int
  6254. xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name,
  6255. xmlElementContentPtr *result) {
  6256. xmlElementContentPtr tree = NULL;
  6257. int inputid = ctxt->input->id;
  6258. int res;
  6259. *result = NULL;
  6260. if (RAW != '(') {
  6261. xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
  6262. "xmlParseElementContentDecl : %s '(' expected\n", name);
  6263. return(-1);
  6264. }
  6265. NEXT;
  6266. GROW;
  6267. if (ctxt->instate == XML_PARSER_EOF)
  6268. return(-1);
  6269. SKIP_BLANKS;
  6270. if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
  6271. tree = xmlParseElementMixedContentDecl(ctxt, inputid);
  6272. res = XML_ELEMENT_TYPE_MIXED;
  6273. } else {
  6274. tree = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, 1);
  6275. res = XML_ELEMENT_TYPE_ELEMENT;
  6276. }
  6277. SKIP_BLANKS;
  6278. *result = tree;
  6279. return(res);
  6280. }
  6281. /**
  6282. * xmlParseElementDecl:
  6283. * @ctxt: an XML parser context
  6284. *
  6285. * DEPRECATED: Internal function, don't use.
  6286. *
  6287. * Parse an element declaration. Always consumes '<!'.
  6288. *
  6289. * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
  6290. *
  6291. * [ VC: Unique Element Type Declaration ]
  6292. * No element type may be declared more than once
  6293. *
  6294. * Returns the type of the element, or -1 in case of error
  6295. */
  6296. int
  6297. xmlParseElementDecl(xmlParserCtxtPtr ctxt) {
  6298. const xmlChar *name;
  6299. int ret = -1;
  6300. xmlElementContentPtr content = NULL;
  6301. if ((CUR != '<') || (NXT(1) != '!'))
  6302. return(ret);
  6303. SKIP(2);
  6304. /* GROW; done in the caller */
  6305. if (CMP7(CUR_PTR, 'E', 'L', 'E', 'M', 'E', 'N', 'T')) {
  6306. int inputid = ctxt->input->id;
  6307. SKIP(7);
  6308. if (SKIP_BLANKS == 0) {
  6309. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  6310. "Space required after 'ELEMENT'\n");
  6311. return(-1);
  6312. }
  6313. name = xmlParseName(ctxt);
  6314. if (name == NULL) {
  6315. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  6316. "xmlParseElementDecl: no name for Element\n");
  6317. return(-1);
  6318. }
  6319. if (SKIP_BLANKS == 0) {
  6320. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  6321. "Space required after the element name\n");
  6322. }
  6323. if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) {
  6324. SKIP(5);
  6325. /*
  6326. * Element must always be empty.
  6327. */
  6328. ret = XML_ELEMENT_TYPE_EMPTY;
  6329. } else if ((RAW == 'A') && (NXT(1) == 'N') &&
  6330. (NXT(2) == 'Y')) {
  6331. SKIP(3);
  6332. /*
  6333. * Element is a generic container.
  6334. */
  6335. ret = XML_ELEMENT_TYPE_ANY;
  6336. } else if (RAW == '(') {
  6337. ret = xmlParseElementContentDecl(ctxt, name, &content);
  6338. } else {
  6339. /*
  6340. * [ WFC: PEs in Internal Subset ] error handling.
  6341. */
  6342. if ((RAW == '%') && (ctxt->external == 0) &&
  6343. (ctxt->inputNr == 1)) {
  6344. xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET,
  6345. "PEReference: forbidden within markup decl in internal subset\n");
  6346. } else {
  6347. xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
  6348. "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
  6349. }
  6350. return(-1);
  6351. }
  6352. SKIP_BLANKS;
  6353. if (RAW != '>') {
  6354. xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
  6355. if (content != NULL) {
  6356. xmlFreeDocElementContent(ctxt->myDoc, content);
  6357. }
  6358. } else {
  6359. if (inputid != ctxt->input->id) {
  6360. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  6361. "Element declaration doesn't start and stop in"
  6362. " the same entity\n");
  6363. }
  6364. NEXT;
  6365. if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
  6366. (ctxt->sax->elementDecl != NULL)) {
  6367. if (content != NULL)
  6368. content->parent = NULL;
  6369. ctxt->sax->elementDecl(ctxt->userData, name, ret,
  6370. content);
  6371. if ((content != NULL) && (content->parent == NULL)) {
  6372. /*
  6373. * this is a trick: if xmlAddElementDecl is called,
  6374. * instead of copying the full tree it is plugged directly
  6375. * if called from the parser. Avoid duplicating the
  6376. * interfaces or change the API/ABI
  6377. */
  6378. xmlFreeDocElementContent(ctxt->myDoc, content);
  6379. }
  6380. } else if (content != NULL) {
  6381. xmlFreeDocElementContent(ctxt->myDoc, content);
  6382. }
  6383. }
  6384. }
  6385. return(ret);
  6386. }
  6387. /**
  6388. * xmlParseConditionalSections
  6389. * @ctxt: an XML parser context
  6390. *
  6391. * Parse a conditional section. Always consumes '<!['.
  6392. *
  6393. * [61] conditionalSect ::= includeSect | ignoreSect
  6394. * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
  6395. * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
  6396. * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)*
  6397. * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
  6398. */
  6399. static void
  6400. xmlParseConditionalSections(xmlParserCtxtPtr ctxt) {
  6401. int *inputIds = NULL;
  6402. size_t inputIdsSize = 0;
  6403. size_t depth = 0;
  6404. while (ctxt->instate != XML_PARSER_EOF) {
  6405. if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
  6406. int id = ctxt->input->id;
  6407. SKIP(3);
  6408. SKIP_BLANKS;
  6409. if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) {
  6410. SKIP(7);
  6411. SKIP_BLANKS;
  6412. if (RAW != '[') {
  6413. xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
  6414. xmlHaltParser(ctxt);
  6415. goto error;
  6416. }
  6417. if (ctxt->input->id != id) {
  6418. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  6419. "All markup of the conditional section is"
  6420. " not in the same entity\n");
  6421. }
  6422. NEXT;
  6423. if (inputIdsSize <= depth) {
  6424. int *tmp;
  6425. inputIdsSize = (inputIdsSize == 0 ? 4 : inputIdsSize * 2);
  6426. tmp = (int *) xmlRealloc(inputIds,
  6427. inputIdsSize * sizeof(int));
  6428. if (tmp == NULL) {
  6429. xmlErrMemory(ctxt, NULL);
  6430. goto error;
  6431. }
  6432. inputIds = tmp;
  6433. }
  6434. inputIds[depth] = id;
  6435. depth++;
  6436. } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) {
  6437. size_t ignoreDepth = 0;
  6438. SKIP(6);
  6439. SKIP_BLANKS;
  6440. if (RAW != '[') {
  6441. xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
  6442. xmlHaltParser(ctxt);
  6443. goto error;
  6444. }
  6445. if (ctxt->input->id != id) {
  6446. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  6447. "All markup of the conditional section is"
  6448. " not in the same entity\n");
  6449. }
  6450. NEXT;
  6451. while (RAW != 0) {
  6452. if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
  6453. SKIP(3);
  6454. ignoreDepth++;
  6455. /* Check for integer overflow */
  6456. if (ignoreDepth == 0) {
  6457. xmlErrMemory(ctxt, NULL);
  6458. goto error;
  6459. }
  6460. } else if ((RAW == ']') && (NXT(1) == ']') &&
  6461. (NXT(2) == '>')) {
  6462. if (ignoreDepth == 0)
  6463. break;
  6464. SKIP(3);
  6465. ignoreDepth--;
  6466. } else {
  6467. NEXT;
  6468. }
  6469. }
  6470. if (RAW == 0) {
  6471. xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL);
  6472. goto error;
  6473. }
  6474. if (ctxt->input->id != id) {
  6475. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  6476. "All markup of the conditional section is"
  6477. " not in the same entity\n");
  6478. }
  6479. SKIP(3);
  6480. } else {
  6481. xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL);
  6482. xmlHaltParser(ctxt);
  6483. goto error;
  6484. }
  6485. } else if ((depth > 0) &&
  6486. (RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
  6487. depth--;
  6488. if (ctxt->input->id != inputIds[depth]) {
  6489. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
  6490. "All markup of the conditional section is not"
  6491. " in the same entity\n");
  6492. }
  6493. SKIP(3);
  6494. } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
  6495. xmlParseMarkupDecl(ctxt);
  6496. } else {
  6497. xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
  6498. xmlHaltParser(ctxt);
  6499. goto error;
  6500. }
  6501. if (depth == 0)
  6502. break;
  6503. SKIP_BLANKS;
  6504. SHRINK;
  6505. GROW;
  6506. }
  6507. error:
  6508. xmlFree(inputIds);
  6509. }
  6510. /**
  6511. * xmlParseMarkupDecl:
  6512. * @ctxt: an XML parser context
  6513. *
  6514. * DEPRECATED: Internal function, don't use.
  6515. *
  6516. * Parse markup declarations. Always consumes '<!' or '<?'.
  6517. *
  6518. * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
  6519. * NotationDecl | PI | Comment
  6520. *
  6521. * [ VC: Proper Declaration/PE Nesting ]
  6522. * Parameter-entity replacement text must be properly nested with
  6523. * markup declarations. That is to say, if either the first character
  6524. * or the last character of a markup declaration (markupdecl above) is
  6525. * contained in the replacement text for a parameter-entity reference,
  6526. * both must be contained in the same replacement text.
  6527. *
  6528. * [ WFC: PEs in Internal Subset ]
  6529. * In the internal DTD subset, parameter-entity references can occur
  6530. * only where markup declarations can occur, not within markup declarations.
  6531. * (This does not apply to references that occur in external parameter
  6532. * entities or to the external subset.)
  6533. */
  6534. void
  6535. xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) {
  6536. GROW;
  6537. if (CUR == '<') {
  6538. if (NXT(1) == '!') {
  6539. switch (NXT(2)) {
  6540. case 'E':
  6541. if (NXT(3) == 'L')
  6542. xmlParseElementDecl(ctxt);
  6543. else if (NXT(3) == 'N')
  6544. xmlParseEntityDecl(ctxt);
  6545. else
  6546. SKIP(2);
  6547. break;
  6548. case 'A':
  6549. xmlParseAttributeListDecl(ctxt);
  6550. break;
  6551. case 'N':
  6552. xmlParseNotationDecl(ctxt);
  6553. break;
  6554. case '-':
  6555. xmlParseComment(ctxt);
  6556. break;
  6557. default:
  6558. /* there is an error but it will be detected later */
  6559. SKIP(2);
  6560. break;
  6561. }
  6562. } else if (NXT(1) == '?') {
  6563. xmlParsePI(ctxt);
  6564. }
  6565. }
  6566. /*
  6567. * detect requirement to exit there and act accordingly
  6568. * and avoid having instate overridden later on
  6569. */
  6570. if (ctxt->instate == XML_PARSER_EOF)
  6571. return;
  6572. ctxt->instate = XML_PARSER_DTD;
  6573. }
  6574. /**
  6575. * xmlParseTextDecl:
  6576. * @ctxt: an XML parser context
  6577. *
  6578. * DEPRECATED: Internal function, don't use.
  6579. *
  6580. * parse an XML declaration header for external entities
  6581. *
  6582. * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
  6583. */
  6584. void
  6585. xmlParseTextDecl(xmlParserCtxtPtr ctxt) {
  6586. xmlChar *version;
  6587. int oldstate;
  6588. /*
  6589. * We know that '<?xml' is here.
  6590. */
  6591. if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
  6592. SKIP(5);
  6593. } else {
  6594. xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL);
  6595. return;
  6596. }
  6597. /* Avoid expansion of parameter entities when skipping blanks. */
  6598. oldstate = ctxt->instate;
  6599. ctxt->instate = XML_PARSER_START;
  6600. if (SKIP_BLANKS == 0) {
  6601. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  6602. "Space needed after '<?xml'\n");
  6603. }
  6604. /*
  6605. * We may have the VersionInfo here.
  6606. */
  6607. version = xmlParseVersionInfo(ctxt);
  6608. if (version == NULL)
  6609. version = xmlCharStrdup(XML_DEFAULT_VERSION);
  6610. else {
  6611. if (SKIP_BLANKS == 0) {
  6612. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  6613. "Space needed here\n");
  6614. }
  6615. }
  6616. ctxt->input->version = version;
  6617. /*
  6618. * We must have the encoding declaration
  6619. */
  6620. xmlParseEncodingDecl(ctxt);
  6621. if (ctxt->instate == XML_PARSER_EOF)
  6622. return;
  6623. if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
  6624. /*
  6625. * The XML REC instructs us to stop parsing right here
  6626. */
  6627. ctxt->instate = oldstate;
  6628. return;
  6629. }
  6630. SKIP_BLANKS;
  6631. if ((RAW == '?') && (NXT(1) == '>')) {
  6632. SKIP(2);
  6633. } else if (RAW == '>') {
  6634. /* Deprecated old WD ... */
  6635. xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
  6636. NEXT;
  6637. } else {
  6638. int c;
  6639. xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
  6640. while ((c = CUR) != 0) {
  6641. NEXT;
  6642. if (c == '>')
  6643. break;
  6644. }
  6645. }
  6646. if (ctxt->instate != XML_PARSER_EOF)
  6647. ctxt->instate = oldstate;
  6648. }
  6649. /**
  6650. * xmlParseExternalSubset:
  6651. * @ctxt: an XML parser context
  6652. * @ExternalID: the external identifier
  6653. * @SystemID: the system identifier (or URL)
  6654. *
  6655. * parse Markup declarations from an external subset
  6656. *
  6657. * [30] extSubset ::= textDecl? extSubsetDecl
  6658. *
  6659. * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) *
  6660. */
  6661. void
  6662. xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID,
  6663. const xmlChar *SystemID) {
  6664. xmlDetectSAX2(ctxt);
  6665. xmlDetectEncoding(ctxt);
  6666. if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) {
  6667. xmlParseTextDecl(ctxt);
  6668. if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
  6669. /*
  6670. * The XML REC instructs us to stop parsing right here
  6671. */
  6672. xmlHaltParser(ctxt);
  6673. return;
  6674. }
  6675. }
  6676. if (ctxt->myDoc == NULL) {
  6677. ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
  6678. if (ctxt->myDoc == NULL) {
  6679. xmlErrMemory(ctxt, "New Doc failed");
  6680. return;
  6681. }
  6682. ctxt->myDoc->properties = XML_DOC_INTERNAL;
  6683. }
  6684. if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL))
  6685. xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID);
  6686. ctxt->instate = XML_PARSER_DTD;
  6687. ctxt->external = 1;
  6688. SKIP_BLANKS;
  6689. while ((ctxt->instate != XML_PARSER_EOF) && (RAW != 0)) {
  6690. GROW;
  6691. if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
  6692. xmlParseConditionalSections(ctxt);
  6693. } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
  6694. xmlParseMarkupDecl(ctxt);
  6695. } else {
  6696. xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
  6697. xmlHaltParser(ctxt);
  6698. return;
  6699. }
  6700. SKIP_BLANKS;
  6701. SHRINK;
  6702. }
  6703. if (RAW != 0) {
  6704. xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
  6705. }
  6706. }
  6707. /**
  6708. * xmlParseReference:
  6709. * @ctxt: an XML parser context
  6710. *
  6711. * DEPRECATED: Internal function, don't use.
  6712. *
  6713. * parse and handle entity references in content, depending on the SAX
  6714. * interface, this may end-up in a call to character() if this is a
  6715. * CharRef, a predefined entity, if there is no reference() callback.
  6716. * or if the parser was asked to switch to that mode.
  6717. *
  6718. * Always consumes '&'.
  6719. *
  6720. * [67] Reference ::= EntityRef | CharRef
  6721. */
  6722. void
  6723. xmlParseReference(xmlParserCtxtPtr ctxt) {
  6724. xmlEntityPtr ent;
  6725. xmlChar *val;
  6726. int was_checked;
  6727. xmlNodePtr list = NULL;
  6728. xmlParserErrors ret = XML_ERR_OK;
  6729. if (RAW != '&')
  6730. return;
  6731. /*
  6732. * Simple case of a CharRef
  6733. */
  6734. if (NXT(1) == '#') {
  6735. int i = 0;
  6736. xmlChar out[16];
  6737. int value = xmlParseCharRef(ctxt);
  6738. if (value == 0)
  6739. return;
  6740. /*
  6741. * Just encode the value in UTF-8
  6742. */
  6743. COPY_BUF(out, i, value);
  6744. out[i] = 0;
  6745. if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
  6746. (!ctxt->disableSAX))
  6747. ctxt->sax->characters(ctxt->userData, out, i);
  6748. return;
  6749. }
  6750. /*
  6751. * We are seeing an entity reference
  6752. */
  6753. ent = xmlParseEntityRef(ctxt);
  6754. if (ent == NULL) return;
  6755. if (!ctxt->wellFormed)
  6756. return;
  6757. was_checked = ent->flags & XML_ENT_PARSED;
  6758. /* special case of predefined entities */
  6759. if ((ent->name == NULL) ||
  6760. (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
  6761. val = ent->content;
  6762. if (val == NULL) return;
  6763. /*
  6764. * inline the entity.
  6765. */
  6766. if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
  6767. (!ctxt->disableSAX))
  6768. ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
  6769. return;
  6770. }
  6771. /*
  6772. * Some users try to parse entities on their own and used to set
  6773. * the renamed "checked" member. Fix the flags to cover this
  6774. * case.
  6775. */
  6776. if (((ent->flags & XML_ENT_PARSED) == 0) && (ent->children != NULL))
  6777. ent->flags |= XML_ENT_PARSED;
  6778. /*
  6779. * The first reference to the entity trigger a parsing phase
  6780. * where the ent->children is filled with the result from
  6781. * the parsing.
  6782. * Note: external parsed entities will not be loaded, it is not
  6783. * required for a non-validating parser, unless the parsing option
  6784. * of validating, or substituting entities were given. Doing so is
  6785. * far more secure as the parser will only process data coming from
  6786. * the document entity by default.
  6787. *
  6788. * FIXME: This doesn't work correctly since entities can be
  6789. * expanded with different namespace declarations in scope.
  6790. * For example:
  6791. *
  6792. * <!DOCTYPE doc [
  6793. * <!ENTITY ent "<ns:elem/>">
  6794. * ]>
  6795. * <doc>
  6796. * <decl1 xmlns:ns="urn:ns1">
  6797. * &ent;
  6798. * </decl1>
  6799. * <decl2 xmlns:ns="urn:ns2">
  6800. * &ent;
  6801. * </decl2>
  6802. * </doc>
  6803. *
  6804. * Proposed fix:
  6805. *
  6806. * - Remove the ent->owner optimization which tries to avoid the
  6807. * initial copy of the entity. Always make entities own the
  6808. * subtree.
  6809. * - Ignore current namespace declarations when parsing the
  6810. * entity. If a prefix can't be resolved, don't report an error
  6811. * but mark it as unresolved.
  6812. * - Try to resolve these prefixes when expanding the entity.
  6813. * This will require a specialized version of xmlStaticCopyNode
  6814. * which can also make use of the namespace hash table to avoid
  6815. * quadratic behavior.
  6816. *
  6817. * Alternatively, we could simply reparse the entity on each
  6818. * expansion like we already do with custom SAX callbacks.
  6819. * External entity content should be cached in this case.
  6820. */
  6821. if (((ent->flags & XML_ENT_PARSED) == 0) &&
  6822. ((ent->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY) ||
  6823. (ctxt->options & (XML_PARSE_NOENT | XML_PARSE_DTDVALID)))) {
  6824. unsigned long oldsizeentcopy = ctxt->sizeentcopy;
  6825. /*
  6826. * This is a bit hackish but this seems the best
  6827. * way to make sure both SAX and DOM entity support
  6828. * behaves okay.
  6829. */
  6830. void *user_data;
  6831. if (ctxt->userData == ctxt)
  6832. user_data = NULL;
  6833. else
  6834. user_data = ctxt->userData;
  6835. /* Avoid overflow as much as possible */
  6836. ctxt->sizeentcopy = 0;
  6837. if (ent->flags & XML_ENT_EXPANDING) {
  6838. xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
  6839. xmlHaltParser(ctxt);
  6840. return;
  6841. }
  6842. ent->flags |= XML_ENT_EXPANDING;
  6843. /*
  6844. * Check that this entity is well formed
  6845. * 4.3.2: An internal general parsed entity is well-formed
  6846. * if its replacement text matches the production labeled
  6847. * content.
  6848. */
  6849. if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) {
  6850. ctxt->depth++;
  6851. ret = xmlParseBalancedChunkMemoryInternal(ctxt, ent->content,
  6852. user_data, &list);
  6853. ctxt->depth--;
  6854. } else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
  6855. ctxt->depth++;
  6856. ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, ctxt->sax,
  6857. user_data, ctxt->depth, ent->URI,
  6858. ent->ExternalID, &list);
  6859. ctxt->depth--;
  6860. } else {
  6861. ret = XML_ERR_ENTITY_PE_INTERNAL;
  6862. xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR,
  6863. "invalid entity type found\n", NULL);
  6864. }
  6865. ent->flags &= ~XML_ENT_EXPANDING;
  6866. ent->flags |= XML_ENT_PARSED | XML_ENT_CHECKED;
  6867. ent->expandedSize = ctxt->sizeentcopy;
  6868. if (ret == XML_ERR_ENTITY_LOOP) {
  6869. xmlHaltParser(ctxt);
  6870. xmlFreeNodeList(list);
  6871. return;
  6872. }
  6873. if (xmlParserEntityCheck(ctxt, oldsizeentcopy)) {
  6874. xmlFreeNodeList(list);
  6875. return;
  6876. }
  6877. if ((ret == XML_ERR_OK) && (list != NULL)) {
  6878. ent->children = list;
  6879. /*
  6880. * Prune it directly in the generated document
  6881. * except for single text nodes.
  6882. */
  6883. if ((ctxt->replaceEntities == 0) ||
  6884. (ctxt->parseMode == XML_PARSE_READER) ||
  6885. ((list->type == XML_TEXT_NODE) &&
  6886. (list->next == NULL))) {
  6887. ent->owner = 1;
  6888. while (list != NULL) {
  6889. list->parent = (xmlNodePtr) ent;
  6890. if (list->doc != ent->doc)
  6891. xmlSetTreeDoc(list, ent->doc);
  6892. if (list->next == NULL)
  6893. ent->last = list;
  6894. list = list->next;
  6895. }
  6896. list = NULL;
  6897. } else {
  6898. ent->owner = 0;
  6899. while (list != NULL) {
  6900. list->parent = (xmlNodePtr) ctxt->node;
  6901. list->doc = ctxt->myDoc;
  6902. if (list->next == NULL)
  6903. ent->last = list;
  6904. list = list->next;
  6905. }
  6906. list = ent->children;
  6907. #ifdef LIBXML_LEGACY_ENABLED
  6908. if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
  6909. xmlAddEntityReference(ent, list, NULL);
  6910. #endif /* LIBXML_LEGACY_ENABLED */
  6911. }
  6912. } else if ((ret != XML_ERR_OK) &&
  6913. (ret != XML_WAR_UNDECLARED_ENTITY)) {
  6914. xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
  6915. "Entity '%s' failed to parse\n", ent->name);
  6916. if (ent->content != NULL)
  6917. ent->content[0] = 0;
  6918. } else if (list != NULL) {
  6919. xmlFreeNodeList(list);
  6920. list = NULL;
  6921. }
  6922. /* Prevent entity from being parsed and expanded twice (Bug 760367). */
  6923. was_checked = 0;
  6924. }
  6925. /*
  6926. * Now that the entity content has been gathered
  6927. * provide it to the application, this can take different forms based
  6928. * on the parsing modes.
  6929. */
  6930. if (ent->children == NULL) {
  6931. /*
  6932. * Probably running in SAX mode and the callbacks don't
  6933. * build the entity content. So unless we already went
  6934. * though parsing for first checking go though the entity
  6935. * content to generate callbacks associated to the entity
  6936. */
  6937. if (was_checked != 0) {
  6938. void *user_data;
  6939. /*
  6940. * This is a bit hackish but this seems the best
  6941. * way to make sure both SAX and DOM entity support
  6942. * behaves okay.
  6943. */
  6944. if (ctxt->userData == ctxt)
  6945. user_data = NULL;
  6946. else
  6947. user_data = ctxt->userData;
  6948. if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) {
  6949. ctxt->depth++;
  6950. ret = xmlParseBalancedChunkMemoryInternal(ctxt,
  6951. ent->content, user_data, NULL);
  6952. ctxt->depth--;
  6953. } else if (ent->etype ==
  6954. XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
  6955. unsigned long oldsizeentities = ctxt->sizeentities;
  6956. ctxt->depth++;
  6957. ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt,
  6958. ctxt->sax, user_data, ctxt->depth,
  6959. ent->URI, ent->ExternalID, NULL);
  6960. ctxt->depth--;
  6961. /* Undo the change to sizeentities */
  6962. ctxt->sizeentities = oldsizeentities;
  6963. } else {
  6964. ret = XML_ERR_ENTITY_PE_INTERNAL;
  6965. xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR,
  6966. "invalid entity type found\n", NULL);
  6967. }
  6968. if (ret == XML_ERR_ENTITY_LOOP) {
  6969. xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
  6970. return;
  6971. }
  6972. if (xmlParserEntityCheck(ctxt, 0))
  6973. return;
  6974. }
  6975. if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
  6976. (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) {
  6977. /*
  6978. * Entity reference callback comes second, it's somewhat
  6979. * superfluous but a compatibility to historical behaviour
  6980. */
  6981. ctxt->sax->reference(ctxt->userData, ent->name);
  6982. }
  6983. return;
  6984. }
  6985. /*
  6986. * We also check for amplification if entities aren't substituted.
  6987. * They might be expanded later.
  6988. */
  6989. if ((was_checked != 0) &&
  6990. (xmlParserEntityCheck(ctxt, ent->expandedSize)))
  6991. return;
  6992. /*
  6993. * If we didn't get any children for the entity being built
  6994. */
  6995. if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
  6996. (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) {
  6997. /*
  6998. * Create a node.
  6999. */
  7000. ctxt->sax->reference(ctxt->userData, ent->name);
  7001. return;
  7002. }
  7003. if (ctxt->replaceEntities) {
  7004. /*
  7005. * There is a problem on the handling of _private for entities
  7006. * (bug 155816): Should we copy the content of the field from
  7007. * the entity (possibly overwriting some value set by the user
  7008. * when a copy is created), should we leave it alone, or should
  7009. * we try to take care of different situations? The problem
  7010. * is exacerbated by the usage of this field by the xmlReader.
  7011. * To fix this bug, we look at _private on the created node
  7012. * and, if it's NULL, we copy in whatever was in the entity.
  7013. * If it's not NULL we leave it alone. This is somewhat of a
  7014. * hack - maybe we should have further tests to determine
  7015. * what to do.
  7016. */
  7017. if (ctxt->node != NULL) {
  7018. /*
  7019. * Seems we are generating the DOM content, do
  7020. * a simple tree copy for all references except the first
  7021. * In the first occurrence list contains the replacement.
  7022. */
  7023. if (((list == NULL) && (ent->owner == 0)) ||
  7024. (ctxt->parseMode == XML_PARSE_READER)) {
  7025. xmlNodePtr nw = NULL, cur, firstChild = NULL;
  7026. /*
  7027. * when operating on a reader, the entities definitions
  7028. * are always owning the entities subtree.
  7029. if (ctxt->parseMode == XML_PARSE_READER)
  7030. ent->owner = 1;
  7031. */
  7032. cur = ent->children;
  7033. while (cur != NULL) {
  7034. nw = xmlDocCopyNode(cur, ctxt->myDoc, 1);
  7035. if (nw != NULL) {
  7036. if (nw->_private == NULL)
  7037. nw->_private = cur->_private;
  7038. if (firstChild == NULL){
  7039. firstChild = nw;
  7040. }
  7041. nw = xmlAddChild(ctxt->node, nw);
  7042. }
  7043. if (cur == ent->last) {
  7044. /*
  7045. * needed to detect some strange empty
  7046. * node cases in the reader tests
  7047. */
  7048. if ((ctxt->parseMode == XML_PARSE_READER) &&
  7049. (nw != NULL) &&
  7050. (nw->type == XML_ELEMENT_NODE) &&
  7051. (nw->children == NULL))
  7052. nw->extra = 1;
  7053. break;
  7054. }
  7055. cur = cur->next;
  7056. }
  7057. #ifdef LIBXML_LEGACY_ENABLED
  7058. if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
  7059. xmlAddEntityReference(ent, firstChild, nw);
  7060. #endif /* LIBXML_LEGACY_ENABLED */
  7061. } else if ((list == NULL) || (ctxt->inputNr > 0)) {
  7062. xmlNodePtr nw = NULL, cur, next, last,
  7063. firstChild = NULL;
  7064. /*
  7065. * Copy the entity child list and make it the new
  7066. * entity child list. The goal is to make sure any
  7067. * ID or REF referenced will be the one from the
  7068. * document content and not the entity copy.
  7069. */
  7070. cur = ent->children;
  7071. ent->children = NULL;
  7072. last = ent->last;
  7073. ent->last = NULL;
  7074. while (cur != NULL) {
  7075. next = cur->next;
  7076. cur->next = NULL;
  7077. cur->parent = NULL;
  7078. nw = xmlDocCopyNode(cur, ctxt->myDoc, 1);
  7079. if (nw != NULL) {
  7080. if (nw->_private == NULL)
  7081. nw->_private = cur->_private;
  7082. if (firstChild == NULL){
  7083. firstChild = cur;
  7084. }
  7085. xmlAddChild((xmlNodePtr) ent, nw);
  7086. }
  7087. xmlAddChild(ctxt->node, cur);
  7088. if (cur == last)
  7089. break;
  7090. cur = next;
  7091. }
  7092. if (ent->owner == 0)
  7093. ent->owner = 1;
  7094. #ifdef LIBXML_LEGACY_ENABLED
  7095. if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
  7096. xmlAddEntityReference(ent, firstChild, nw);
  7097. #endif /* LIBXML_LEGACY_ENABLED */
  7098. } else {
  7099. const xmlChar *nbktext;
  7100. /*
  7101. * the name change is to avoid coalescing of the
  7102. * node with a possible previous text one which
  7103. * would make ent->children a dangling pointer
  7104. */
  7105. nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
  7106. -1);
  7107. if (ent->children->type == XML_TEXT_NODE)
  7108. ent->children->name = nbktext;
  7109. if ((ent->last != ent->children) &&
  7110. (ent->last->type == XML_TEXT_NODE))
  7111. ent->last->name = nbktext;
  7112. xmlAddChildList(ctxt->node, ent->children);
  7113. }
  7114. /*
  7115. * This is to avoid a nasty side effect, see
  7116. * characters() in SAX.c
  7117. */
  7118. ctxt->nodemem = 0;
  7119. ctxt->nodelen = 0;
  7120. return;
  7121. }
  7122. }
  7123. }
  7124. /**
  7125. * xmlParseEntityRef:
  7126. * @ctxt: an XML parser context
  7127. *
  7128. * DEPRECATED: Internal function, don't use.
  7129. *
  7130. * Parse an entitiy reference. Always consumes '&'.
  7131. *
  7132. * [68] EntityRef ::= '&' Name ';'
  7133. *
  7134. * [ WFC: Entity Declared ]
  7135. * In a document without any DTD, a document with only an internal DTD
  7136. * subset which contains no parameter entity references, or a document
  7137. * with "standalone='yes'", the Name given in the entity reference
  7138. * must match that in an entity declaration, except that well-formed
  7139. * documents need not declare any of the following entities: amp, lt,
  7140. * gt, apos, quot. The declaration of a parameter entity must precede
  7141. * any reference to it. Similarly, the declaration of a general entity
  7142. * must precede any reference to it which appears in a default value in an
  7143. * attribute-list declaration. Note that if entities are declared in the
  7144. * external subset or in external parameter entities, a non-validating
  7145. * processor is not obligated to read and process their declarations;
  7146. * for such documents, the rule that an entity must be declared is a
  7147. * well-formedness constraint only if standalone='yes'.
  7148. *
  7149. * [ WFC: Parsed Entity ]
  7150. * An entity reference must not contain the name of an unparsed entity
  7151. *
  7152. * Returns the xmlEntityPtr if found, or NULL otherwise.
  7153. */
  7154. xmlEntityPtr
  7155. xmlParseEntityRef(xmlParserCtxtPtr ctxt) {
  7156. const xmlChar *name;
  7157. xmlEntityPtr ent = NULL;
  7158. GROW;
  7159. if (ctxt->instate == XML_PARSER_EOF)
  7160. return(NULL);
  7161. if (RAW != '&')
  7162. return(NULL);
  7163. NEXT;
  7164. name = xmlParseName(ctxt);
  7165. if (name == NULL) {
  7166. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  7167. "xmlParseEntityRef: no name\n");
  7168. return(NULL);
  7169. }
  7170. if (RAW != ';') {
  7171. xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
  7172. return(NULL);
  7173. }
  7174. NEXT;
  7175. /*
  7176. * Predefined entities override any extra definition
  7177. */
  7178. if ((ctxt->options & XML_PARSE_OLDSAX) == 0) {
  7179. ent = xmlGetPredefinedEntity(name);
  7180. if (ent != NULL)
  7181. return(ent);
  7182. }
  7183. /*
  7184. * Ask first SAX for entity resolution, otherwise try the
  7185. * entities which may have stored in the parser context.
  7186. */
  7187. if (ctxt->sax != NULL) {
  7188. if (ctxt->sax->getEntity != NULL)
  7189. ent = ctxt->sax->getEntity(ctxt->userData, name);
  7190. if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
  7191. (ctxt->options & XML_PARSE_OLDSAX))
  7192. ent = xmlGetPredefinedEntity(name);
  7193. if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
  7194. (ctxt->userData==ctxt)) {
  7195. ent = xmlSAX2GetEntity(ctxt, name);
  7196. }
  7197. }
  7198. if (ctxt->instate == XML_PARSER_EOF)
  7199. return(NULL);
  7200. /*
  7201. * [ WFC: Entity Declared ]
  7202. * In a document without any DTD, a document with only an
  7203. * internal DTD subset which contains no parameter entity
  7204. * references, or a document with "standalone='yes'", the
  7205. * Name given in the entity reference must match that in an
  7206. * entity declaration, except that well-formed documents
  7207. * need not declare any of the following entities: amp, lt,
  7208. * gt, apos, quot.
  7209. * The declaration of a parameter entity must precede any
  7210. * reference to it.
  7211. * Similarly, the declaration of a general entity must
  7212. * precede any reference to it which appears in a default
  7213. * value in an attribute-list declaration. Note that if
  7214. * entities are declared in the external subset or in
  7215. * external parameter entities, a non-validating processor
  7216. * is not obligated to read and process their declarations;
  7217. * for such documents, the rule that an entity must be
  7218. * declared is a well-formedness constraint only if
  7219. * standalone='yes'.
  7220. */
  7221. if (ent == NULL) {
  7222. if ((ctxt->standalone == 1) ||
  7223. ((ctxt->hasExternalSubset == 0) &&
  7224. (ctxt->hasPErefs == 0))) {
  7225. xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
  7226. "Entity '%s' not defined\n", name);
  7227. } else {
  7228. xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
  7229. "Entity '%s' not defined\n", name);
  7230. if ((ctxt->inSubset == 0) &&
  7231. (ctxt->sax != NULL) &&
  7232. (ctxt->disableSAX == 0) &&
  7233. (ctxt->sax->reference != NULL)) {
  7234. ctxt->sax->reference(ctxt->userData, name);
  7235. }
  7236. }
  7237. ctxt->valid = 0;
  7238. }
  7239. /*
  7240. * [ WFC: Parsed Entity ]
  7241. * An entity reference must not contain the name of an
  7242. * unparsed entity
  7243. */
  7244. else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
  7245. xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
  7246. "Entity reference to unparsed entity %s\n", name);
  7247. }
  7248. /*
  7249. * [ WFC: No External Entity References ]
  7250. * Attribute values cannot contain direct or indirect
  7251. * entity references to external entities.
  7252. */
  7253. else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
  7254. (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
  7255. xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
  7256. "Attribute references external entity '%s'\n", name);
  7257. }
  7258. /*
  7259. * [ WFC: No < in Attribute Values ]
  7260. * The replacement text of any entity referred to directly or
  7261. * indirectly in an attribute value (other than "&lt;") must
  7262. * not contain a <.
  7263. */
  7264. else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
  7265. (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
  7266. if ((ent->flags & XML_ENT_CHECKED_LT) == 0) {
  7267. if ((ent->content != NULL) && (xmlStrchr(ent->content, '<')))
  7268. ent->flags |= XML_ENT_CONTAINS_LT;
  7269. ent->flags |= XML_ENT_CHECKED_LT;
  7270. }
  7271. if (ent->flags & XML_ENT_CONTAINS_LT)
  7272. xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
  7273. "'<' in entity '%s' is not allowed in attributes "
  7274. "values\n", name);
  7275. }
  7276. /*
  7277. * Internal check, no parameter entities here ...
  7278. */
  7279. else {
  7280. switch (ent->etype) {
  7281. case XML_INTERNAL_PARAMETER_ENTITY:
  7282. case XML_EXTERNAL_PARAMETER_ENTITY:
  7283. xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
  7284. "Attempt to reference the parameter entity '%s'\n",
  7285. name);
  7286. break;
  7287. default:
  7288. break;
  7289. }
  7290. }
  7291. /*
  7292. * [ WFC: No Recursion ]
  7293. * A parsed entity must not contain a recursive reference
  7294. * to itself, either directly or indirectly.
  7295. * Done somewhere else
  7296. */
  7297. return(ent);
  7298. }
  7299. /**
  7300. * xmlParseStringEntityRef:
  7301. * @ctxt: an XML parser context
  7302. * @str: a pointer to an index in the string
  7303. *
  7304. * parse ENTITY references declarations, but this version parses it from
  7305. * a string value.
  7306. *
  7307. * [68] EntityRef ::= '&' Name ';'
  7308. *
  7309. * [ WFC: Entity Declared ]
  7310. * In a document without any DTD, a document with only an internal DTD
  7311. * subset which contains no parameter entity references, or a document
  7312. * with "standalone='yes'", the Name given in the entity reference
  7313. * must match that in an entity declaration, except that well-formed
  7314. * documents need not declare any of the following entities: amp, lt,
  7315. * gt, apos, quot. The declaration of a parameter entity must precede
  7316. * any reference to it. Similarly, the declaration of a general entity
  7317. * must precede any reference to it which appears in a default value in an
  7318. * attribute-list declaration. Note that if entities are declared in the
  7319. * external subset or in external parameter entities, a non-validating
  7320. * processor is not obligated to read and process their declarations;
  7321. * for such documents, the rule that an entity must be declared is a
  7322. * well-formedness constraint only if standalone='yes'.
  7323. *
  7324. * [ WFC: Parsed Entity ]
  7325. * An entity reference must not contain the name of an unparsed entity
  7326. *
  7327. * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer
  7328. * is updated to the current location in the string.
  7329. */
  7330. static xmlEntityPtr
  7331. xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
  7332. xmlChar *name;
  7333. const xmlChar *ptr;
  7334. xmlChar cur;
  7335. xmlEntityPtr ent = NULL;
  7336. if ((str == NULL) || (*str == NULL))
  7337. return(NULL);
  7338. ptr = *str;
  7339. cur = *ptr;
  7340. if (cur != '&')
  7341. return(NULL);
  7342. ptr++;
  7343. name = xmlParseStringName(ctxt, &ptr);
  7344. if (name == NULL) {
  7345. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  7346. "xmlParseStringEntityRef: no name\n");
  7347. *str = ptr;
  7348. return(NULL);
  7349. }
  7350. if (*ptr != ';') {
  7351. xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
  7352. xmlFree(name);
  7353. *str = ptr;
  7354. return(NULL);
  7355. }
  7356. ptr++;
  7357. /*
  7358. * Predefined entities override any extra definition
  7359. */
  7360. if ((ctxt->options & XML_PARSE_OLDSAX) == 0) {
  7361. ent = xmlGetPredefinedEntity(name);
  7362. if (ent != NULL) {
  7363. xmlFree(name);
  7364. *str = ptr;
  7365. return(ent);
  7366. }
  7367. }
  7368. /*
  7369. * Ask first SAX for entity resolution, otherwise try the
  7370. * entities which may have stored in the parser context.
  7371. */
  7372. if (ctxt->sax != NULL) {
  7373. if (ctxt->sax->getEntity != NULL)
  7374. ent = ctxt->sax->getEntity(ctxt->userData, name);
  7375. if ((ent == NULL) && (ctxt->options & XML_PARSE_OLDSAX))
  7376. ent = xmlGetPredefinedEntity(name);
  7377. if ((ent == NULL) && (ctxt->userData==ctxt)) {
  7378. ent = xmlSAX2GetEntity(ctxt, name);
  7379. }
  7380. }
  7381. if (ctxt->instate == XML_PARSER_EOF) {
  7382. xmlFree(name);
  7383. return(NULL);
  7384. }
  7385. /*
  7386. * [ WFC: Entity Declared ]
  7387. * In a document without any DTD, a document with only an
  7388. * internal DTD subset which contains no parameter entity
  7389. * references, or a document with "standalone='yes'", the
  7390. * Name given in the entity reference must match that in an
  7391. * entity declaration, except that well-formed documents
  7392. * need not declare any of the following entities: amp, lt,
  7393. * gt, apos, quot.
  7394. * The declaration of a parameter entity must precede any
  7395. * reference to it.
  7396. * Similarly, the declaration of a general entity must
  7397. * precede any reference to it which appears in a default
  7398. * value in an attribute-list declaration. Note that if
  7399. * entities are declared in the external subset or in
  7400. * external parameter entities, a non-validating processor
  7401. * is not obligated to read and process their declarations;
  7402. * for such documents, the rule that an entity must be
  7403. * declared is a well-formedness constraint only if
  7404. * standalone='yes'.
  7405. */
  7406. if (ent == NULL) {
  7407. if ((ctxt->standalone == 1) ||
  7408. ((ctxt->hasExternalSubset == 0) &&
  7409. (ctxt->hasPErefs == 0))) {
  7410. xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
  7411. "Entity '%s' not defined\n", name);
  7412. } else {
  7413. xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
  7414. "Entity '%s' not defined\n",
  7415. name);
  7416. }
  7417. /* TODO ? check regressions ctxt->valid = 0; */
  7418. }
  7419. /*
  7420. * [ WFC: Parsed Entity ]
  7421. * An entity reference must not contain the name of an
  7422. * unparsed entity
  7423. */
  7424. else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
  7425. xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
  7426. "Entity reference to unparsed entity %s\n", name);
  7427. }
  7428. /*
  7429. * [ WFC: No External Entity References ]
  7430. * Attribute values cannot contain direct or indirect
  7431. * entity references to external entities.
  7432. */
  7433. else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
  7434. (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
  7435. xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
  7436. "Attribute references external entity '%s'\n", name);
  7437. }
  7438. /*
  7439. * [ WFC: No < in Attribute Values ]
  7440. * The replacement text of any entity referred to directly or
  7441. * indirectly in an attribute value (other than "&lt;") must
  7442. * not contain a <.
  7443. */
  7444. else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
  7445. (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
  7446. if ((ent->flags & XML_ENT_CHECKED_LT) == 0) {
  7447. if ((ent->content != NULL) && (xmlStrchr(ent->content, '<')))
  7448. ent->flags |= XML_ENT_CONTAINS_LT;
  7449. ent->flags |= XML_ENT_CHECKED_LT;
  7450. }
  7451. if (ent->flags & XML_ENT_CONTAINS_LT)
  7452. xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
  7453. "'<' in entity '%s' is not allowed in attributes "
  7454. "values\n", name);
  7455. }
  7456. /*
  7457. * Internal check, no parameter entities here ...
  7458. */
  7459. else {
  7460. switch (ent->etype) {
  7461. case XML_INTERNAL_PARAMETER_ENTITY:
  7462. case XML_EXTERNAL_PARAMETER_ENTITY:
  7463. xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
  7464. "Attempt to reference the parameter entity '%s'\n",
  7465. name);
  7466. break;
  7467. default:
  7468. break;
  7469. }
  7470. }
  7471. /*
  7472. * [ WFC: No Recursion ]
  7473. * A parsed entity must not contain a recursive reference
  7474. * to itself, either directly or indirectly.
  7475. * Done somewhere else
  7476. */
  7477. xmlFree(name);
  7478. *str = ptr;
  7479. return(ent);
  7480. }
  7481. /**
  7482. * xmlParsePEReference:
  7483. * @ctxt: an XML parser context
  7484. *
  7485. * DEPRECATED: Internal function, don't use.
  7486. *
  7487. * Parse a parameter entity reference. Always consumes '%'.
  7488. *
  7489. * The entity content is handled directly by pushing it's content as
  7490. * a new input stream.
  7491. *
  7492. * [69] PEReference ::= '%' Name ';'
  7493. *
  7494. * [ WFC: No Recursion ]
  7495. * A parsed entity must not contain a recursive
  7496. * reference to itself, either directly or indirectly.
  7497. *
  7498. * [ WFC: Entity Declared ]
  7499. * In a document without any DTD, a document with only an internal DTD
  7500. * subset which contains no parameter entity references, or a document
  7501. * with "standalone='yes'", ... ... The declaration of a parameter
  7502. * entity must precede any reference to it...
  7503. *
  7504. * [ VC: Entity Declared ]
  7505. * In a document with an external subset or external parameter entities
  7506. * with "standalone='no'", ... ... The declaration of a parameter entity
  7507. * must precede any reference to it...
  7508. *
  7509. * [ WFC: In DTD ]
  7510. * Parameter-entity references may only appear in the DTD.
  7511. * NOTE: misleading but this is handled.
  7512. */
  7513. void
  7514. xmlParsePEReference(xmlParserCtxtPtr ctxt)
  7515. {
  7516. const xmlChar *name;
  7517. xmlEntityPtr entity = NULL;
  7518. xmlParserInputPtr input;
  7519. if (RAW != '%')
  7520. return;
  7521. NEXT;
  7522. name = xmlParseName(ctxt);
  7523. if (name == NULL) {
  7524. xmlFatalErrMsg(ctxt, XML_ERR_PEREF_NO_NAME, "PEReference: no name\n");
  7525. return;
  7526. }
  7527. if (xmlParserDebugEntities)
  7528. xmlGenericError(xmlGenericErrorContext,
  7529. "PEReference: %s\n", name);
  7530. if (RAW != ';') {
  7531. xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL);
  7532. return;
  7533. }
  7534. NEXT;
  7535. /*
  7536. * Request the entity from SAX
  7537. */
  7538. if ((ctxt->sax != NULL) &&
  7539. (ctxt->sax->getParameterEntity != NULL))
  7540. entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
  7541. if (ctxt->instate == XML_PARSER_EOF)
  7542. return;
  7543. if (entity == NULL) {
  7544. /*
  7545. * [ WFC: Entity Declared ]
  7546. * In a document without any DTD, a document with only an
  7547. * internal DTD subset which contains no parameter entity
  7548. * references, or a document with "standalone='yes'", ...
  7549. * ... The declaration of a parameter entity must precede
  7550. * any reference to it...
  7551. */
  7552. if ((ctxt->standalone == 1) ||
  7553. ((ctxt->hasExternalSubset == 0) &&
  7554. (ctxt->hasPErefs == 0))) {
  7555. xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
  7556. "PEReference: %%%s; not found\n",
  7557. name);
  7558. } else {
  7559. /*
  7560. * [ VC: Entity Declared ]
  7561. * In a document with an external subset or external
  7562. * parameter entities with "standalone='no'", ...
  7563. * ... The declaration of a parameter entity must
  7564. * precede any reference to it...
  7565. */
  7566. if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) {
  7567. xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY,
  7568. "PEReference: %%%s; not found\n",
  7569. name, NULL);
  7570. } else
  7571. xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
  7572. "PEReference: %%%s; not found\n",
  7573. name, NULL);
  7574. ctxt->valid = 0;
  7575. }
  7576. } else {
  7577. /*
  7578. * Internal checking in case the entity quest barfed
  7579. */
  7580. if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
  7581. (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
  7582. xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
  7583. "Internal: %%%s; is not a parameter entity\n",
  7584. name, NULL);
  7585. } else {
  7586. unsigned long parentConsumed;
  7587. xmlEntityPtr oldEnt;
  7588. if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
  7589. ((ctxt->options & XML_PARSE_NOENT) == 0) &&
  7590. ((ctxt->options & XML_PARSE_DTDVALID) == 0) &&
  7591. ((ctxt->options & XML_PARSE_DTDLOAD) == 0) &&
  7592. ((ctxt->options & XML_PARSE_DTDATTR) == 0) &&
  7593. (ctxt->replaceEntities == 0) &&
  7594. (ctxt->validate == 0))
  7595. return;
  7596. if (entity->flags & XML_ENT_EXPANDING) {
  7597. xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
  7598. xmlHaltParser(ctxt);
  7599. return;
  7600. }
  7601. /* Must be computed from old input before pushing new input. */
  7602. parentConsumed = ctxt->input->parentConsumed;
  7603. oldEnt = ctxt->input->entity;
  7604. if ((oldEnt == NULL) ||
  7605. ((oldEnt->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
  7606. ((oldEnt->flags & XML_ENT_PARSED) == 0))) {
  7607. xmlSaturatedAdd(&parentConsumed, ctxt->input->consumed);
  7608. xmlSaturatedAddSizeT(&parentConsumed,
  7609. ctxt->input->cur - ctxt->input->base);
  7610. }
  7611. input = xmlNewEntityInputStream(ctxt, entity);
  7612. if (xmlPushInput(ctxt, input) < 0) {
  7613. xmlFreeInputStream(input);
  7614. return;
  7615. }
  7616. entity->flags |= XML_ENT_EXPANDING;
  7617. input->parentConsumed = parentConsumed;
  7618. if (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) {
  7619. xmlDetectEncoding(ctxt);
  7620. if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
  7621. (IS_BLANK_CH(NXT(5)))) {
  7622. xmlParseTextDecl(ctxt);
  7623. }
  7624. }
  7625. }
  7626. }
  7627. ctxt->hasPErefs = 1;
  7628. }
  7629. /**
  7630. * xmlLoadEntityContent:
  7631. * @ctxt: an XML parser context
  7632. * @entity: an unloaded system entity
  7633. *
  7634. * Load the original content of the given system entity from the
  7635. * ExternalID/SystemID given. This is to be used for Included in Literal
  7636. * http://www.w3.org/TR/REC-xml/#inliteral processing of entities references
  7637. *
  7638. * Returns 0 in case of success and -1 in case of failure
  7639. */
  7640. static int
  7641. xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
  7642. xmlParserInputPtr oldinput, input = NULL;
  7643. xmlParserInputPtr *oldinputTab;
  7644. const xmlChar *oldencoding;
  7645. xmlChar *content = NULL;
  7646. size_t length, i;
  7647. int oldinputNr, oldinputMax, oldprogressive;
  7648. int ret = -1;
  7649. int res;
  7650. if ((ctxt == NULL) || (entity == NULL) ||
  7651. ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) &&
  7652. (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) ||
  7653. (entity->content != NULL)) {
  7654. xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
  7655. "xmlLoadEntityContent parameter error");
  7656. return(-1);
  7657. }
  7658. if (xmlParserDebugEntities)
  7659. xmlGenericError(xmlGenericErrorContext,
  7660. "Reading %s entity content input\n", entity->name);
  7661. input = xmlLoadExternalEntity((char *) entity->URI,
  7662. (char *) entity->ExternalID, ctxt);
  7663. if (input == NULL) {
  7664. xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
  7665. "xmlLoadEntityContent input error");
  7666. return(-1);
  7667. }
  7668. oldinput = ctxt->input;
  7669. oldinputNr = ctxt->inputNr;
  7670. oldinputMax = ctxt->inputMax;
  7671. oldinputTab = ctxt->inputTab;
  7672. oldencoding = ctxt->encoding;
  7673. oldprogressive = ctxt->progressive;
  7674. ctxt->input = NULL;
  7675. ctxt->inputNr = 0;
  7676. ctxt->inputMax = 1;
  7677. ctxt->encoding = NULL;
  7678. ctxt->progressive = 0;
  7679. ctxt->inputTab = xmlMalloc(sizeof(xmlParserInputPtr));
  7680. if (ctxt->inputTab == NULL) {
  7681. xmlErrMemory(ctxt, NULL);
  7682. xmlFreeInputStream(input);
  7683. goto error;
  7684. }
  7685. xmlBufResetInput(input->buf->buffer, input);
  7686. inputPush(ctxt, input);
  7687. xmlDetectEncoding(ctxt);
  7688. /*
  7689. * Parse a possible text declaration first
  7690. */
  7691. if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
  7692. xmlParseTextDecl(ctxt);
  7693. /*
  7694. * An XML-1.0 document can't reference an entity not XML-1.0
  7695. */
  7696. if ((xmlStrEqual(ctxt->version, BAD_CAST "1.0")) &&
  7697. (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) {
  7698. xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH,
  7699. "Version mismatch between document and entity\n");
  7700. }
  7701. }
  7702. if (ctxt->instate == XML_PARSER_EOF)
  7703. goto error;
  7704. length = input->cur - input->base;
  7705. xmlBufShrink(input->buf->buffer, length);
  7706. xmlSaturatedAdd(&ctxt->sizeentities, length);
  7707. while ((res = xmlParserInputBufferGrow(input->buf, 4096)) > 0)
  7708. ;
  7709. xmlBufResetInput(input->buf->buffer, input);
  7710. if (res < 0) {
  7711. xmlFatalErr(ctxt, input->buf->error, NULL);
  7712. goto error;
  7713. }
  7714. length = xmlBufUse(input->buf->buffer);
  7715. content = xmlBufDetach(input->buf->buffer);
  7716. if (length > INT_MAX) {
  7717. xmlErrMemory(ctxt, NULL);
  7718. goto error;
  7719. }
  7720. for (i = 0; i < length; ) {
  7721. int clen = length - i;
  7722. int c = xmlGetUTF8Char(content + i, &clen);
  7723. if ((c < 0) || (!IS_CHAR(c))) {
  7724. xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
  7725. "xmlLoadEntityContent: invalid char value %d\n",
  7726. content[i]);
  7727. goto error;
  7728. }
  7729. i += clen;
  7730. }
  7731. xmlSaturatedAdd(&ctxt->sizeentities, length);
  7732. entity->content = content;
  7733. entity->length = length;
  7734. content = NULL;
  7735. ret = 0;
  7736. error:
  7737. while (ctxt->inputNr > 0)
  7738. xmlFreeInputStream(inputPop(ctxt));
  7739. xmlFree(ctxt->inputTab);
  7740. xmlFree((xmlChar *) ctxt->encoding);
  7741. ctxt->input = oldinput;
  7742. ctxt->inputNr = oldinputNr;
  7743. ctxt->inputMax = oldinputMax;
  7744. ctxt->inputTab = oldinputTab;
  7745. ctxt->encoding = oldencoding;
  7746. ctxt->progressive = oldprogressive;
  7747. xmlFree(content);
  7748. return(ret);
  7749. }
  7750. /**
  7751. * xmlParseStringPEReference:
  7752. * @ctxt: an XML parser context
  7753. * @str: a pointer to an index in the string
  7754. *
  7755. * parse PEReference declarations
  7756. *
  7757. * [69] PEReference ::= '%' Name ';'
  7758. *
  7759. * [ WFC: No Recursion ]
  7760. * A parsed entity must not contain a recursive
  7761. * reference to itself, either directly or indirectly.
  7762. *
  7763. * [ WFC: Entity Declared ]
  7764. * In a document without any DTD, a document with only an internal DTD
  7765. * subset which contains no parameter entity references, or a document
  7766. * with "standalone='yes'", ... ... The declaration of a parameter
  7767. * entity must precede any reference to it...
  7768. *
  7769. * [ VC: Entity Declared ]
  7770. * In a document with an external subset or external parameter entities
  7771. * with "standalone='no'", ... ... The declaration of a parameter entity
  7772. * must precede any reference to it...
  7773. *
  7774. * [ WFC: In DTD ]
  7775. * Parameter-entity references may only appear in the DTD.
  7776. * NOTE: misleading but this is handled.
  7777. *
  7778. * Returns the string of the entity content.
  7779. * str is updated to the current value of the index
  7780. */
  7781. static xmlEntityPtr
  7782. xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
  7783. const xmlChar *ptr;
  7784. xmlChar cur;
  7785. xmlChar *name;
  7786. xmlEntityPtr entity = NULL;
  7787. if ((str == NULL) || (*str == NULL)) return(NULL);
  7788. ptr = *str;
  7789. cur = *ptr;
  7790. if (cur != '%')
  7791. return(NULL);
  7792. ptr++;
  7793. name = xmlParseStringName(ctxt, &ptr);
  7794. if (name == NULL) {
  7795. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  7796. "xmlParseStringPEReference: no name\n");
  7797. *str = ptr;
  7798. return(NULL);
  7799. }
  7800. cur = *ptr;
  7801. if (cur != ';') {
  7802. xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
  7803. xmlFree(name);
  7804. *str = ptr;
  7805. return(NULL);
  7806. }
  7807. ptr++;
  7808. /*
  7809. * Request the entity from SAX
  7810. */
  7811. if ((ctxt->sax != NULL) &&
  7812. (ctxt->sax->getParameterEntity != NULL))
  7813. entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
  7814. if (ctxt->instate == XML_PARSER_EOF) {
  7815. xmlFree(name);
  7816. *str = ptr;
  7817. return(NULL);
  7818. }
  7819. if (entity == NULL) {
  7820. /*
  7821. * [ WFC: Entity Declared ]
  7822. * In a document without any DTD, a document with only an
  7823. * internal DTD subset which contains no parameter entity
  7824. * references, or a document with "standalone='yes'", ...
  7825. * ... The declaration of a parameter entity must precede
  7826. * any reference to it...
  7827. */
  7828. if ((ctxt->standalone == 1) ||
  7829. ((ctxt->hasExternalSubset == 0) && (ctxt->hasPErefs == 0))) {
  7830. xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
  7831. "PEReference: %%%s; not found\n", name);
  7832. } else {
  7833. /*
  7834. * [ VC: Entity Declared ]
  7835. * In a document with an external subset or external
  7836. * parameter entities with "standalone='no'", ...
  7837. * ... The declaration of a parameter entity must
  7838. * precede any reference to it...
  7839. */
  7840. xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
  7841. "PEReference: %%%s; not found\n",
  7842. name, NULL);
  7843. ctxt->valid = 0;
  7844. }
  7845. } else {
  7846. /*
  7847. * Internal checking in case the entity quest barfed
  7848. */
  7849. if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
  7850. (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
  7851. xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
  7852. "%%%s; is not a parameter entity\n",
  7853. name, NULL);
  7854. }
  7855. }
  7856. ctxt->hasPErefs = 1;
  7857. xmlFree(name);
  7858. *str = ptr;
  7859. return(entity);
  7860. }
  7861. /**
  7862. * xmlParseDocTypeDecl:
  7863. * @ctxt: an XML parser context
  7864. *
  7865. * DEPRECATED: Internal function, don't use.
  7866. *
  7867. * parse a DOCTYPE declaration
  7868. *
  7869. * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
  7870. * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
  7871. *
  7872. * [ VC: Root Element Type ]
  7873. * The Name in the document type declaration must match the element
  7874. * type of the root element.
  7875. */
  7876. void
  7877. xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) {
  7878. const xmlChar *name = NULL;
  7879. xmlChar *ExternalID = NULL;
  7880. xmlChar *URI = NULL;
  7881. /*
  7882. * We know that '<!DOCTYPE' has been detected.
  7883. */
  7884. SKIP(9);
  7885. SKIP_BLANKS;
  7886. /*
  7887. * Parse the DOCTYPE name.
  7888. */
  7889. name = xmlParseName(ctxt);
  7890. if (name == NULL) {
  7891. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  7892. "xmlParseDocTypeDecl : no DOCTYPE name !\n");
  7893. }
  7894. ctxt->intSubName = name;
  7895. SKIP_BLANKS;
  7896. /*
  7897. * Check for SystemID and ExternalID
  7898. */
  7899. URI = xmlParseExternalID(ctxt, &ExternalID, 1);
  7900. if ((URI != NULL) || (ExternalID != NULL)) {
  7901. ctxt->hasExternalSubset = 1;
  7902. }
  7903. ctxt->extSubURI = URI;
  7904. ctxt->extSubSystem = ExternalID;
  7905. SKIP_BLANKS;
  7906. /*
  7907. * Create and update the internal subset.
  7908. */
  7909. if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
  7910. (!ctxt->disableSAX))
  7911. ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
  7912. if (ctxt->instate == XML_PARSER_EOF)
  7913. return;
  7914. /*
  7915. * Is there any internal subset declarations ?
  7916. * they are handled separately in xmlParseInternalSubset()
  7917. */
  7918. if (RAW == '[')
  7919. return;
  7920. /*
  7921. * We should be at the end of the DOCTYPE declaration.
  7922. */
  7923. if (RAW != '>') {
  7924. xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
  7925. }
  7926. NEXT;
  7927. }
  7928. /**
  7929. * xmlParseInternalSubset:
  7930. * @ctxt: an XML parser context
  7931. *
  7932. * parse the internal subset declaration
  7933. *
  7934. * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
  7935. */
  7936. static void
  7937. xmlParseInternalSubset(xmlParserCtxtPtr ctxt) {
  7938. /*
  7939. * Is there any DTD definition ?
  7940. */
  7941. if (RAW == '[') {
  7942. int baseInputNr = ctxt->inputNr;
  7943. ctxt->instate = XML_PARSER_DTD;
  7944. NEXT;
  7945. /*
  7946. * Parse the succession of Markup declarations and
  7947. * PEReferences.
  7948. * Subsequence (markupdecl | PEReference | S)*
  7949. */
  7950. SKIP_BLANKS;
  7951. while (((RAW != ']') || (ctxt->inputNr > baseInputNr)) &&
  7952. (ctxt->instate != XML_PARSER_EOF)) {
  7953. /*
  7954. * Conditional sections are allowed from external entities included
  7955. * by PE References in the internal subset.
  7956. */
  7957. if ((ctxt->inputNr > 1) && (ctxt->input->filename != NULL) &&
  7958. (RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
  7959. xmlParseConditionalSections(ctxt);
  7960. } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
  7961. xmlParseMarkupDecl(ctxt);
  7962. } else if (RAW == '%') {
  7963. xmlParsePEReference(ctxt);
  7964. } else {
  7965. xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
  7966. "xmlParseInternalSubset: error detected in"
  7967. " Markup declaration\n");
  7968. xmlHaltParser(ctxt);
  7969. return;
  7970. }
  7971. SKIP_BLANKS;
  7972. SHRINK;
  7973. GROW;
  7974. }
  7975. if (RAW == ']') {
  7976. NEXT;
  7977. SKIP_BLANKS;
  7978. }
  7979. }
  7980. /*
  7981. * We should be at the end of the DOCTYPE declaration.
  7982. */
  7983. if (RAW != '>') {
  7984. xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
  7985. return;
  7986. }
  7987. NEXT;
  7988. }
  7989. #ifdef LIBXML_SAX1_ENABLED
  7990. /**
  7991. * xmlParseAttribute:
  7992. * @ctxt: an XML parser context
  7993. * @value: a xmlChar ** used to store the value of the attribute
  7994. *
  7995. * DEPRECATED: Internal function, don't use.
  7996. *
  7997. * parse an attribute
  7998. *
  7999. * [41] Attribute ::= Name Eq AttValue
  8000. *
  8001. * [ WFC: No External Entity References ]
  8002. * Attribute values cannot contain direct or indirect entity references
  8003. * to external entities.
  8004. *
  8005. * [ WFC: No < in Attribute Values ]
  8006. * The replacement text of any entity referred to directly or indirectly in
  8007. * an attribute value (other than "&lt;") must not contain a <.
  8008. *
  8009. * [ VC: Attribute Value Type ]
  8010. * The attribute must have been declared; the value must be of the type
  8011. * declared for it.
  8012. *
  8013. * [25] Eq ::= S? '=' S?
  8014. *
  8015. * With namespace:
  8016. *
  8017. * [NS 11] Attribute ::= QName Eq AttValue
  8018. *
  8019. * Also the case QName == xmlns:??? is handled independently as a namespace
  8020. * definition.
  8021. *
  8022. * Returns the attribute name, and the value in *value.
  8023. */
  8024. const xmlChar *
  8025. xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) {
  8026. const xmlChar *name;
  8027. xmlChar *val;
  8028. *value = NULL;
  8029. GROW;
  8030. name = xmlParseName(ctxt);
  8031. if (name == NULL) {
  8032. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  8033. "error parsing attribute name\n");
  8034. return(NULL);
  8035. }
  8036. /*
  8037. * read the value
  8038. */
  8039. SKIP_BLANKS;
  8040. if (RAW == '=') {
  8041. NEXT;
  8042. SKIP_BLANKS;
  8043. val = xmlParseAttValue(ctxt);
  8044. ctxt->instate = XML_PARSER_CONTENT;
  8045. } else {
  8046. xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
  8047. "Specification mandates value for attribute %s\n", name);
  8048. return(name);
  8049. }
  8050. /*
  8051. * Check that xml:lang conforms to the specification
  8052. * No more registered as an error, just generate a warning now
  8053. * since this was deprecated in XML second edition
  8054. */
  8055. if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
  8056. if (!xmlCheckLanguageID(val)) {
  8057. xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
  8058. "Malformed value for xml:lang : %s\n",
  8059. val, NULL);
  8060. }
  8061. }
  8062. /*
  8063. * Check that xml:space conforms to the specification
  8064. */
  8065. if (xmlStrEqual(name, BAD_CAST "xml:space")) {
  8066. if (xmlStrEqual(val, BAD_CAST "default"))
  8067. *(ctxt->space) = 0;
  8068. else if (xmlStrEqual(val, BAD_CAST "preserve"))
  8069. *(ctxt->space) = 1;
  8070. else {
  8071. xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
  8072. "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
  8073. val, NULL);
  8074. }
  8075. }
  8076. *value = val;
  8077. return(name);
  8078. }
  8079. /**
  8080. * xmlParseStartTag:
  8081. * @ctxt: an XML parser context
  8082. *
  8083. * DEPRECATED: Internal function, don't use.
  8084. *
  8085. * Parse a start tag. Always consumes '<'.
  8086. *
  8087. * [40] STag ::= '<' Name (S Attribute)* S? '>'
  8088. *
  8089. * [ WFC: Unique Att Spec ]
  8090. * No attribute name may appear more than once in the same start-tag or
  8091. * empty-element tag.
  8092. *
  8093. * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
  8094. *
  8095. * [ WFC: Unique Att Spec ]
  8096. * No attribute name may appear more than once in the same start-tag or
  8097. * empty-element tag.
  8098. *
  8099. * With namespace:
  8100. *
  8101. * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
  8102. *
  8103. * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
  8104. *
  8105. * Returns the element name parsed
  8106. */
  8107. const xmlChar *
  8108. xmlParseStartTag(xmlParserCtxtPtr ctxt) {
  8109. const xmlChar *name;
  8110. const xmlChar *attname;
  8111. xmlChar *attvalue;
  8112. const xmlChar **atts = ctxt->atts;
  8113. int nbatts = 0;
  8114. int maxatts = ctxt->maxatts;
  8115. int i;
  8116. if (RAW != '<') return(NULL);
  8117. NEXT1;
  8118. name = xmlParseName(ctxt);
  8119. if (name == NULL) {
  8120. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  8121. "xmlParseStartTag: invalid element name\n");
  8122. return(NULL);
  8123. }
  8124. /*
  8125. * Now parse the attributes, it ends up with the ending
  8126. *
  8127. * (S Attribute)* S?
  8128. */
  8129. SKIP_BLANKS;
  8130. GROW;
  8131. while (((RAW != '>') &&
  8132. ((RAW != '/') || (NXT(1) != '>')) &&
  8133. (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) {
  8134. attname = xmlParseAttribute(ctxt, &attvalue);
  8135. if (attname == NULL) {
  8136. xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
  8137. "xmlParseStartTag: problem parsing attributes\n");
  8138. break;
  8139. }
  8140. if (attvalue != NULL) {
  8141. /*
  8142. * [ WFC: Unique Att Spec ]
  8143. * No attribute name may appear more than once in the same
  8144. * start-tag or empty-element tag.
  8145. */
  8146. for (i = 0; i < nbatts;i += 2) {
  8147. if (xmlStrEqual(atts[i], attname)) {
  8148. xmlErrAttributeDup(ctxt, NULL, attname);
  8149. xmlFree(attvalue);
  8150. goto failed;
  8151. }
  8152. }
  8153. /*
  8154. * Add the pair to atts
  8155. */
  8156. if (atts == NULL) {
  8157. maxatts = 22; /* allow for 10 attrs by default */
  8158. atts = (const xmlChar **)
  8159. xmlMalloc(maxatts * sizeof(xmlChar *));
  8160. if (atts == NULL) {
  8161. xmlErrMemory(ctxt, NULL);
  8162. if (attvalue != NULL)
  8163. xmlFree(attvalue);
  8164. goto failed;
  8165. }
  8166. ctxt->atts = atts;
  8167. ctxt->maxatts = maxatts;
  8168. } else if (nbatts + 4 > maxatts) {
  8169. const xmlChar **n;
  8170. maxatts *= 2;
  8171. n = (const xmlChar **) xmlRealloc((void *) atts,
  8172. maxatts * sizeof(const xmlChar *));
  8173. if (n == NULL) {
  8174. xmlErrMemory(ctxt, NULL);
  8175. if (attvalue != NULL)
  8176. xmlFree(attvalue);
  8177. goto failed;
  8178. }
  8179. atts = n;
  8180. ctxt->atts = atts;
  8181. ctxt->maxatts = maxatts;
  8182. }
  8183. atts[nbatts++] = attname;
  8184. atts[nbatts++] = attvalue;
  8185. atts[nbatts] = NULL;
  8186. atts[nbatts + 1] = NULL;
  8187. } else {
  8188. if (attvalue != NULL)
  8189. xmlFree(attvalue);
  8190. }
  8191. failed:
  8192. GROW
  8193. if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
  8194. break;
  8195. if (SKIP_BLANKS == 0) {
  8196. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  8197. "attributes construct error\n");
  8198. }
  8199. SHRINK;
  8200. GROW;
  8201. }
  8202. /*
  8203. * SAX: Start of Element !
  8204. */
  8205. if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) &&
  8206. (!ctxt->disableSAX)) {
  8207. if (nbatts > 0)
  8208. ctxt->sax->startElement(ctxt->userData, name, atts);
  8209. else
  8210. ctxt->sax->startElement(ctxt->userData, name, NULL);
  8211. }
  8212. if (atts != NULL) {
  8213. /* Free only the content strings */
  8214. for (i = 1;i < nbatts;i+=2)
  8215. if (atts[i] != NULL)
  8216. xmlFree((xmlChar *) atts[i]);
  8217. }
  8218. return(name);
  8219. }
  8220. /**
  8221. * xmlParseEndTag1:
  8222. * @ctxt: an XML parser context
  8223. * @line: line of the start tag
  8224. * @nsNr: number of namespaces on the start tag
  8225. *
  8226. * Parse an end tag. Always consumes '</'.
  8227. *
  8228. * [42] ETag ::= '</' Name S? '>'
  8229. *
  8230. * With namespace
  8231. *
  8232. * [NS 9] ETag ::= '</' QName S? '>'
  8233. */
  8234. static void
  8235. xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) {
  8236. const xmlChar *name;
  8237. GROW;
  8238. if ((RAW != '<') || (NXT(1) != '/')) {
  8239. xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED,
  8240. "xmlParseEndTag: '</' not found\n");
  8241. return;
  8242. }
  8243. SKIP(2);
  8244. name = xmlParseNameAndCompare(ctxt,ctxt->name);
  8245. /*
  8246. * We should definitely be at the ending "S? '>'" part
  8247. */
  8248. GROW;
  8249. SKIP_BLANKS;
  8250. if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
  8251. xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
  8252. } else
  8253. NEXT1;
  8254. /*
  8255. * [ WFC: Element Type Match ]
  8256. * The Name in an element's end-tag must match the element type in the
  8257. * start-tag.
  8258. *
  8259. */
  8260. if (name != (xmlChar*)1) {
  8261. if (name == NULL) name = BAD_CAST "unparsable";
  8262. xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
  8263. "Opening and ending tag mismatch: %s line %d and %s\n",
  8264. ctxt->name, line, name);
  8265. }
  8266. /*
  8267. * SAX: End of Tag
  8268. */
  8269. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
  8270. (!ctxt->disableSAX))
  8271. ctxt->sax->endElement(ctxt->userData, ctxt->name);
  8272. namePop(ctxt);
  8273. spacePop(ctxt);
  8274. return;
  8275. }
  8276. /**
  8277. * xmlParseEndTag:
  8278. * @ctxt: an XML parser context
  8279. *
  8280. * DEPRECATED: Internal function, don't use.
  8281. *
  8282. * parse an end of tag
  8283. *
  8284. * [42] ETag ::= '</' Name S? '>'
  8285. *
  8286. * With namespace
  8287. *
  8288. * [NS 9] ETag ::= '</' QName S? '>'
  8289. */
  8290. void
  8291. xmlParseEndTag(xmlParserCtxtPtr ctxt) {
  8292. xmlParseEndTag1(ctxt, 0);
  8293. }
  8294. #endif /* LIBXML_SAX1_ENABLED */
  8295. /************************************************************************
  8296. * *
  8297. * SAX 2 specific operations *
  8298. * *
  8299. ************************************************************************/
  8300. /**
  8301. * xmlParseQNameHashed:
  8302. * @ctxt: an XML parser context
  8303. * @prefix: pointer to store the prefix part
  8304. *
  8305. * parse an XML Namespace QName
  8306. *
  8307. * [6] QName ::= (Prefix ':')? LocalPart
  8308. * [7] Prefix ::= NCName
  8309. * [8] LocalPart ::= NCName
  8310. *
  8311. * Returns the Name parsed or NULL
  8312. */
  8313. static xmlHashedString
  8314. xmlParseQNameHashed(xmlParserCtxtPtr ctxt, xmlHashedString *prefix) {
  8315. xmlHashedString l, p;
  8316. int start, isNCName = 0;
  8317. l.name = NULL;
  8318. p.name = NULL;
  8319. GROW;
  8320. if (ctxt->instate == XML_PARSER_EOF)
  8321. return(l);
  8322. start = CUR_PTR - BASE_PTR;
  8323. l = xmlParseNCName(ctxt);
  8324. if (l.name != NULL) {
  8325. isNCName = 1;
  8326. if (CUR == ':') {
  8327. NEXT;
  8328. p = l;
  8329. l = xmlParseNCName(ctxt);
  8330. }
  8331. }
  8332. if ((l.name == NULL) || (CUR == ':')) {
  8333. xmlChar *tmp;
  8334. l.name = NULL;
  8335. p.name = NULL;
  8336. if (ctxt->instate == XML_PARSER_EOF)
  8337. return(l);
  8338. if ((isNCName == 0) && (CUR != ':'))
  8339. return(l);
  8340. tmp = xmlParseNmtoken(ctxt);
  8341. if (tmp != NULL)
  8342. xmlFree(tmp);
  8343. if (ctxt->instate == XML_PARSER_EOF)
  8344. return(l);
  8345. l = xmlDictLookupHashed(ctxt->dict, BASE_PTR + start,
  8346. CUR_PTR - (BASE_PTR + start));
  8347. xmlNsErr(ctxt, XML_NS_ERR_QNAME,
  8348. "Failed to parse QName '%s'\n", l.name, NULL, NULL);
  8349. }
  8350. *prefix = p;
  8351. return(l);
  8352. }
  8353. /**
  8354. * xmlParseQName:
  8355. * @ctxt: an XML parser context
  8356. * @prefix: pointer to store the prefix part
  8357. *
  8358. * parse an XML Namespace QName
  8359. *
  8360. * [6] QName ::= (Prefix ':')? LocalPart
  8361. * [7] Prefix ::= NCName
  8362. * [8] LocalPart ::= NCName
  8363. *
  8364. * Returns the Name parsed or NULL
  8365. */
  8366. static const xmlChar *
  8367. xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) {
  8368. xmlHashedString n, p;
  8369. n = xmlParseQNameHashed(ctxt, &p);
  8370. if (n.name == NULL)
  8371. return(NULL);
  8372. *prefix = p.name;
  8373. return(n.name);
  8374. }
  8375. /**
  8376. * xmlParseQNameAndCompare:
  8377. * @ctxt: an XML parser context
  8378. * @name: the localname
  8379. * @prefix: the prefix, if any.
  8380. *
  8381. * parse an XML name and compares for match
  8382. * (specialized for endtag parsing)
  8383. *
  8384. * Returns NULL for an illegal name, (xmlChar*) 1 for success
  8385. * and the name for mismatch
  8386. */
  8387. static const xmlChar *
  8388. xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name,
  8389. xmlChar const *prefix) {
  8390. const xmlChar *cmp;
  8391. const xmlChar *in;
  8392. const xmlChar *ret;
  8393. const xmlChar *prefix2;
  8394. if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name));
  8395. GROW;
  8396. in = ctxt->input->cur;
  8397. cmp = prefix;
  8398. while (*in != 0 && *in == *cmp) {
  8399. ++in;
  8400. ++cmp;
  8401. }
  8402. if ((*cmp == 0) && (*in == ':')) {
  8403. in++;
  8404. cmp = name;
  8405. while (*in != 0 && *in == *cmp) {
  8406. ++in;
  8407. ++cmp;
  8408. }
  8409. if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
  8410. /* success */
  8411. ctxt->input->col += in - ctxt->input->cur;
  8412. ctxt->input->cur = in;
  8413. return((const xmlChar*) 1);
  8414. }
  8415. }
  8416. /*
  8417. * all strings coms from the dictionary, equality can be done directly
  8418. */
  8419. ret = xmlParseQName (ctxt, &prefix2);
  8420. if (ret == NULL)
  8421. return(NULL);
  8422. if ((ret == name) && (prefix == prefix2))
  8423. return((const xmlChar*) 1);
  8424. return ret;
  8425. }
  8426. /**
  8427. * xmlParseAttValueInternal:
  8428. * @ctxt: an XML parser context
  8429. * @len: attribute len result
  8430. * @alloc: whether the attribute was reallocated as a new string
  8431. * @normalize: if 1 then further non-CDATA normalization must be done
  8432. *
  8433. * parse a value for an attribute.
  8434. * NOTE: if no normalization is needed, the routine will return pointers
  8435. * directly from the data buffer.
  8436. *
  8437. * 3.3.3 Attribute-Value Normalization:
  8438. * Before the value of an attribute is passed to the application or
  8439. * checked for validity, the XML processor must normalize it as follows:
  8440. * - a character reference is processed by appending the referenced
  8441. * character to the attribute value
  8442. * - an entity reference is processed by recursively processing the
  8443. * replacement text of the entity
  8444. * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
  8445. * appending #x20 to the normalized value, except that only a single
  8446. * #x20 is appended for a "#xD#xA" sequence that is part of an external
  8447. * parsed entity or the literal entity value of an internal parsed entity
  8448. * - other characters are processed by appending them to the normalized value
  8449. * If the declared value is not CDATA, then the XML processor must further
  8450. * process the normalized attribute value by discarding any leading and
  8451. * trailing space (#x20) characters, and by replacing sequences of space
  8452. * (#x20) characters by a single space (#x20) character.
  8453. * All attributes for which no declaration has been read should be treated
  8454. * by a non-validating parser as if declared CDATA.
  8455. *
  8456. * Returns the AttValue parsed or NULL. The value has to be freed by the
  8457. * caller if it was copied, this can be detected by val[*len] == 0.
  8458. */
  8459. #define GROW_PARSE_ATT_VALUE_INTERNAL(ctxt, in, start, end) \
  8460. const xmlChar *oldbase = ctxt->input->base;\
  8461. GROW;\
  8462. if (ctxt->instate == XML_PARSER_EOF)\
  8463. return(NULL);\
  8464. if (oldbase != ctxt->input->base) {\
  8465. ptrdiff_t delta = ctxt->input->base - oldbase;\
  8466. start = start + delta;\
  8467. in = in + delta;\
  8468. }\
  8469. end = ctxt->input->end;
  8470. static xmlChar *
  8471. xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
  8472. int normalize)
  8473. {
  8474. xmlChar limit = 0;
  8475. const xmlChar *in = NULL, *start, *end, *last;
  8476. xmlChar *ret = NULL;
  8477. int line, col;
  8478. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  8479. XML_MAX_HUGE_LENGTH :
  8480. XML_MAX_TEXT_LENGTH;
  8481. GROW;
  8482. in = (xmlChar *) CUR_PTR;
  8483. line = ctxt->input->line;
  8484. col = ctxt->input->col;
  8485. if (*in != '"' && *in != '\'') {
  8486. xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
  8487. return (NULL);
  8488. }
  8489. ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
  8490. /*
  8491. * try to handle in this routine the most common case where no
  8492. * allocation of a new string is required and where content is
  8493. * pure ASCII.
  8494. */
  8495. limit = *in++;
  8496. col++;
  8497. end = ctxt->input->end;
  8498. start = in;
  8499. if (in >= end) {
  8500. GROW_PARSE_ATT_VALUE_INTERNAL(ctxt, in, start, end)
  8501. }
  8502. if (normalize) {
  8503. /*
  8504. * Skip any leading spaces
  8505. */
  8506. while ((in < end) && (*in != limit) &&
  8507. ((*in == 0x20) || (*in == 0x9) ||
  8508. (*in == 0xA) || (*in == 0xD))) {
  8509. if (*in == 0xA) {
  8510. line++; col = 1;
  8511. } else {
  8512. col++;
  8513. }
  8514. in++;
  8515. start = in;
  8516. if (in >= end) {
  8517. GROW_PARSE_ATT_VALUE_INTERNAL(ctxt, in, start, end)
  8518. if ((in - start) > maxLength) {
  8519. xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  8520. "AttValue length too long\n");
  8521. return(NULL);
  8522. }
  8523. }
  8524. }
  8525. while ((in < end) && (*in != limit) && (*in >= 0x20) &&
  8526. (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
  8527. col++;
  8528. if ((*in++ == 0x20) && (*in == 0x20)) break;
  8529. if (in >= end) {
  8530. GROW_PARSE_ATT_VALUE_INTERNAL(ctxt, in, start, end)
  8531. if ((in - start) > maxLength) {
  8532. xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  8533. "AttValue length too long\n");
  8534. return(NULL);
  8535. }
  8536. }
  8537. }
  8538. last = in;
  8539. /*
  8540. * skip the trailing blanks
  8541. */
  8542. while ((last[-1] == 0x20) && (last > start)) last--;
  8543. while ((in < end) && (*in != limit) &&
  8544. ((*in == 0x20) || (*in == 0x9) ||
  8545. (*in == 0xA) || (*in == 0xD))) {
  8546. if (*in == 0xA) {
  8547. line++, col = 1;
  8548. } else {
  8549. col++;
  8550. }
  8551. in++;
  8552. if (in >= end) {
  8553. const xmlChar *oldbase = ctxt->input->base;
  8554. GROW;
  8555. if (ctxt->instate == XML_PARSER_EOF)
  8556. return(NULL);
  8557. if (oldbase != ctxt->input->base) {
  8558. ptrdiff_t delta = ctxt->input->base - oldbase;
  8559. start = start + delta;
  8560. in = in + delta;
  8561. last = last + delta;
  8562. }
  8563. end = ctxt->input->end;
  8564. if ((in - start) > maxLength) {
  8565. xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  8566. "AttValue length too long\n");
  8567. return(NULL);
  8568. }
  8569. }
  8570. }
  8571. if ((in - start) > maxLength) {
  8572. xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  8573. "AttValue length too long\n");
  8574. return(NULL);
  8575. }
  8576. if (*in != limit) goto need_complex;
  8577. } else {
  8578. while ((in < end) && (*in != limit) && (*in >= 0x20) &&
  8579. (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
  8580. in++;
  8581. col++;
  8582. if (in >= end) {
  8583. GROW_PARSE_ATT_VALUE_INTERNAL(ctxt, in, start, end)
  8584. if ((in - start) > maxLength) {
  8585. xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  8586. "AttValue length too long\n");
  8587. return(NULL);
  8588. }
  8589. }
  8590. }
  8591. last = in;
  8592. if ((in - start) > maxLength) {
  8593. xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  8594. "AttValue length too long\n");
  8595. return(NULL);
  8596. }
  8597. if (*in != limit) goto need_complex;
  8598. }
  8599. in++;
  8600. col++;
  8601. if (len != NULL) {
  8602. if (alloc) *alloc = 0;
  8603. *len = last - start;
  8604. ret = (xmlChar *) start;
  8605. } else {
  8606. if (alloc) *alloc = 1;
  8607. ret = xmlStrndup(start, last - start);
  8608. }
  8609. CUR_PTR = in;
  8610. ctxt->input->line = line;
  8611. ctxt->input->col = col;
  8612. return ret;
  8613. need_complex:
  8614. if (alloc) *alloc = 1;
  8615. return xmlParseAttValueComplex(ctxt, len, normalize);
  8616. }
  8617. /**
  8618. * xmlParseAttribute2:
  8619. * @ctxt: an XML parser context
  8620. * @pref: the element prefix
  8621. * @elem: the element name
  8622. * @prefix: a xmlChar ** used to store the value of the attribute prefix
  8623. * @value: a xmlChar ** used to store the value of the attribute
  8624. * @len: an int * to save the length of the attribute
  8625. * @alloc: an int * to indicate if the attribute was allocated
  8626. *
  8627. * parse an attribute in the new SAX2 framework.
  8628. *
  8629. * Returns the attribute name, and the value in *value, .
  8630. */
  8631. static xmlHashedString
  8632. xmlParseAttribute2(xmlParserCtxtPtr ctxt,
  8633. const xmlChar * pref, const xmlChar * elem,
  8634. xmlHashedString * hprefix, xmlChar ** value,
  8635. int *len, int *alloc)
  8636. {
  8637. xmlHashedString hname;
  8638. const xmlChar *prefix, *name;
  8639. xmlChar *val, *internal_val = NULL;
  8640. int normalize = 0;
  8641. *value = NULL;
  8642. GROW;
  8643. hname = xmlParseQNameHashed(ctxt, hprefix);
  8644. if (hname.name == NULL) {
  8645. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  8646. "error parsing attribute name\n");
  8647. return(hname);
  8648. }
  8649. name = hname.name;
  8650. if (hprefix->name != NULL)
  8651. prefix = hprefix->name;
  8652. else
  8653. prefix = NULL;
  8654. /*
  8655. * get the type if needed
  8656. */
  8657. if (ctxt->attsSpecial != NULL) {
  8658. int type;
  8659. type = (int) (ptrdiff_t) xmlHashQLookup2(ctxt->attsSpecial,
  8660. pref, elem,
  8661. prefix, name);
  8662. if (type != 0)
  8663. normalize = 1;
  8664. }
  8665. /*
  8666. * read the value
  8667. */
  8668. SKIP_BLANKS;
  8669. if (RAW == '=') {
  8670. NEXT;
  8671. SKIP_BLANKS;
  8672. val = xmlParseAttValueInternal(ctxt, len, alloc, normalize);
  8673. if (val == NULL) {
  8674. hname.name = NULL;
  8675. return(hname);
  8676. }
  8677. if (normalize) {
  8678. /*
  8679. * Sometimes a second normalisation pass for spaces is needed
  8680. * but that only happens if charrefs or entities references
  8681. * have been used in the attribute value, i.e. the attribute
  8682. * value have been extracted in an allocated string already.
  8683. */
  8684. if (*alloc) {
  8685. const xmlChar *val2;
  8686. val2 = xmlAttrNormalizeSpace2(ctxt, val, len);
  8687. if ((val2 != NULL) && (val2 != val)) {
  8688. xmlFree(val);
  8689. val = (xmlChar *) val2;
  8690. }
  8691. }
  8692. }
  8693. ctxt->instate = XML_PARSER_CONTENT;
  8694. } else {
  8695. xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
  8696. "Specification mandates value for attribute %s\n",
  8697. name);
  8698. return(hname);
  8699. }
  8700. if (prefix == ctxt->str_xml) {
  8701. /*
  8702. * Check that xml:lang conforms to the specification
  8703. * No more registered as an error, just generate a warning now
  8704. * since this was deprecated in XML second edition
  8705. */
  8706. if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) {
  8707. internal_val = xmlStrndup(val, *len);
  8708. if (!xmlCheckLanguageID(internal_val)) {
  8709. xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
  8710. "Malformed value for xml:lang : %s\n",
  8711. internal_val, NULL);
  8712. }
  8713. }
  8714. /*
  8715. * Check that xml:space conforms to the specification
  8716. */
  8717. if (xmlStrEqual(name, BAD_CAST "space")) {
  8718. internal_val = xmlStrndup(val, *len);
  8719. if (xmlStrEqual(internal_val, BAD_CAST "default"))
  8720. *(ctxt->space) = 0;
  8721. else if (xmlStrEqual(internal_val, BAD_CAST "preserve"))
  8722. *(ctxt->space) = 1;
  8723. else {
  8724. xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
  8725. "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
  8726. internal_val, NULL);
  8727. }
  8728. }
  8729. if (internal_val) {
  8730. xmlFree(internal_val);
  8731. }
  8732. }
  8733. *value = val;
  8734. return (hname);
  8735. }
  8736. /**
  8737. * xmlAttrHashInsert:
  8738. * @ctxt: parser context
  8739. * @size: size of the hash table
  8740. * @name: attribute name
  8741. * @uri: namespace uri
  8742. * @hashValue: combined hash value of name and uri
  8743. * @aindex: attribute index (this is a multiple of 5)
  8744. *
  8745. * Inserts a new attribute into the hash table.
  8746. *
  8747. * Returns INT_MAX if no existing attribute was found, the attribute
  8748. * index if an attribute was found, -1 if a memory allocation failed.
  8749. */
  8750. static int
  8751. xmlAttrHashInsert(xmlParserCtxtPtr ctxt, unsigned size, const xmlChar *name,
  8752. const xmlChar *uri, unsigned hashValue, int aindex) {
  8753. xmlAttrHashBucket *table = ctxt->attrHash;
  8754. xmlAttrHashBucket *bucket;
  8755. unsigned hindex;
  8756. hindex = hashValue & (size - 1);
  8757. bucket = &table[hindex];
  8758. while (bucket->index >= 0) {
  8759. const xmlChar **atts = &ctxt->atts[bucket->index];
  8760. if (name == atts[0]) {
  8761. int nsIndex = (int) (ptrdiff_t) atts[2];
  8762. if ((nsIndex == NS_INDEX_EMPTY) ? (uri == NULL) :
  8763. (nsIndex == NS_INDEX_XML) ? (uri == ctxt->str_xml_ns) :
  8764. (uri == ctxt->nsTab[nsIndex * 2 + 1]))
  8765. return(bucket->index);
  8766. }
  8767. hindex++;
  8768. bucket++;
  8769. if (hindex >= size) {
  8770. hindex = 0;
  8771. bucket = table;
  8772. }
  8773. }
  8774. bucket->index = aindex;
  8775. return(INT_MAX);
  8776. }
  8777. /**
  8778. * xmlParseStartTag2:
  8779. * @ctxt: an XML parser context
  8780. *
  8781. * Parse a start tag. Always consumes '<'.
  8782. *
  8783. * This routine is called when running SAX2 parsing
  8784. *
  8785. * [40] STag ::= '<' Name (S Attribute)* S? '>'
  8786. *
  8787. * [ WFC: Unique Att Spec ]
  8788. * No attribute name may appear more than once in the same start-tag or
  8789. * empty-element tag.
  8790. *
  8791. * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
  8792. *
  8793. * [ WFC: Unique Att Spec ]
  8794. * No attribute name may appear more than once in the same start-tag or
  8795. * empty-element tag.
  8796. *
  8797. * With namespace:
  8798. *
  8799. * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
  8800. *
  8801. * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
  8802. *
  8803. * Returns the element name parsed
  8804. */
  8805. static const xmlChar *
  8806. xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
  8807. const xmlChar **URI, int *nbNsPtr) {
  8808. xmlHashedString hlocalname;
  8809. xmlHashedString hprefix;
  8810. xmlHashedString hattname;
  8811. xmlHashedString haprefix;
  8812. const xmlChar *localname;
  8813. const xmlChar *prefix;
  8814. const xmlChar *attname;
  8815. const xmlChar *aprefix;
  8816. const xmlChar *uri;
  8817. xmlChar *attvalue = NULL;
  8818. const xmlChar **atts = ctxt->atts;
  8819. unsigned attrHashSize = 0;
  8820. int maxatts = ctxt->maxatts;
  8821. int nratts, nbatts, nbdef, inputid;
  8822. int i, j, nbNs, nbTotalDef, attval, nsIndex, maxAtts;
  8823. int alloc = 0;
  8824. if (RAW != '<') return(NULL);
  8825. NEXT1;
  8826. inputid = ctxt->input->id;
  8827. nbatts = 0;
  8828. nratts = 0;
  8829. nbdef = 0;
  8830. nbNs = 0;
  8831. nbTotalDef = 0;
  8832. attval = 0;
  8833. if (xmlParserNsStartElement(ctxt->nsdb) < 0) {
  8834. xmlErrMemory(ctxt, NULL);
  8835. return(NULL);
  8836. }
  8837. hlocalname = xmlParseQNameHashed(ctxt, &hprefix);
  8838. if (hlocalname.name == NULL) {
  8839. xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
  8840. "StartTag: invalid element name\n");
  8841. return(NULL);
  8842. }
  8843. localname = hlocalname.name;
  8844. prefix = hprefix.name;
  8845. /*
  8846. * Now parse the attributes, it ends up with the ending
  8847. *
  8848. * (S Attribute)* S?
  8849. */
  8850. SKIP_BLANKS;
  8851. GROW;
  8852. /*
  8853. * The ctxt->atts array will be ultimately passed to the SAX callback
  8854. * containing five xmlChar pointers for each attribute:
  8855. *
  8856. * [0] attribute name
  8857. * [1] attribute prefix
  8858. * [2] namespace URI
  8859. * [3] attribute value
  8860. * [4] end of attribute value
  8861. *
  8862. * To save memory, we reuse this array temporarily and store integers
  8863. * in these pointer variables.
  8864. *
  8865. * [0] attribute name
  8866. * [1] attribute prefix
  8867. * [2] hash value of attribute prefix, and later namespace index
  8868. * [3] for non-allocated values: ptrdiff_t offset into input buffer
  8869. * [4] for non-allocated values: ptrdiff_t offset into input buffer
  8870. *
  8871. * The ctxt->attallocs array contains an additional unsigned int for
  8872. * each attribute, containing the hash value of the attribute name
  8873. * and the alloc flag in bit 31.
  8874. */
  8875. while (((RAW != '>') &&
  8876. ((RAW != '/') || (NXT(1) != '>')) &&
  8877. (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) {
  8878. int len = -1;
  8879. hattname = xmlParseAttribute2(ctxt, prefix, localname,
  8880. &haprefix, &attvalue, &len,
  8881. &alloc);
  8882. if (hattname.name == NULL) {
  8883. xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
  8884. "xmlParseStartTag: problem parsing attributes\n");
  8885. break;
  8886. }
  8887. if (attvalue == NULL)
  8888. goto next_attr;
  8889. attname = hattname.name;
  8890. aprefix = haprefix.name;
  8891. if (len < 0) len = xmlStrlen(attvalue);
  8892. if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
  8893. xmlHashedString huri;
  8894. xmlURIPtr parsedUri;
  8895. huri = xmlDictLookupHashed(ctxt->dict, attvalue, len);
  8896. uri = huri.name;
  8897. if (uri == NULL) {
  8898. xmlErrMemory(ctxt, NULL);
  8899. goto next_attr;
  8900. }
  8901. if (*uri != 0) {
  8902. parsedUri = xmlParseURI((const char *) uri);
  8903. if (parsedUri == NULL) {
  8904. xmlNsErr(ctxt, XML_WAR_NS_URI,
  8905. "xmlns: '%s' is not a valid URI\n",
  8906. uri, NULL, NULL);
  8907. } else {
  8908. if (parsedUri->scheme == NULL) {
  8909. xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE,
  8910. "xmlns: URI %s is not absolute\n",
  8911. uri, NULL, NULL);
  8912. }
  8913. xmlFreeURI(parsedUri);
  8914. }
  8915. if (uri == ctxt->str_xml_ns) {
  8916. if (attname != ctxt->str_xml) {
  8917. xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
  8918. "xml namespace URI cannot be the default namespace\n",
  8919. NULL, NULL, NULL);
  8920. }
  8921. goto next_attr;
  8922. }
  8923. if ((len == 29) &&
  8924. (xmlStrEqual(uri,
  8925. BAD_CAST "http://www.w3.org/2000/xmlns/"))) {
  8926. xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
  8927. "reuse of the xmlns namespace name is forbidden\n",
  8928. NULL, NULL, NULL);
  8929. goto next_attr;
  8930. }
  8931. }
  8932. if (xmlParserNsPush(ctxt, NULL, &huri, NULL, 0) > 0)
  8933. nbNs++;
  8934. } else if (aprefix == ctxt->str_xmlns) {
  8935. xmlHashedString huri;
  8936. xmlURIPtr parsedUri;
  8937. huri = xmlDictLookupHashed(ctxt->dict, attvalue, len);
  8938. uri = huri.name;
  8939. if (uri == NULL) {
  8940. xmlErrMemory(ctxt, NULL);
  8941. goto next_attr;
  8942. }
  8943. if (attname == ctxt->str_xml) {
  8944. if (uri != ctxt->str_xml_ns) {
  8945. xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
  8946. "xml namespace prefix mapped to wrong URI\n",
  8947. NULL, NULL, NULL);
  8948. }
  8949. /*
  8950. * Do not keep a namespace definition node
  8951. */
  8952. goto next_attr;
  8953. }
  8954. if (uri == ctxt->str_xml_ns) {
  8955. if (attname != ctxt->str_xml) {
  8956. xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
  8957. "xml namespace URI mapped to wrong prefix\n",
  8958. NULL, NULL, NULL);
  8959. }
  8960. goto next_attr;
  8961. }
  8962. if (attname == ctxt->str_xmlns) {
  8963. xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
  8964. "redefinition of the xmlns prefix is forbidden\n",
  8965. NULL, NULL, NULL);
  8966. goto next_attr;
  8967. }
  8968. if ((len == 29) &&
  8969. (xmlStrEqual(uri,
  8970. BAD_CAST "http://www.w3.org/2000/xmlns/"))) {
  8971. xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
  8972. "reuse of the xmlns namespace name is forbidden\n",
  8973. NULL, NULL, NULL);
  8974. goto next_attr;
  8975. }
  8976. if ((uri == NULL) || (uri[0] == 0)) {
  8977. xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
  8978. "xmlns:%s: Empty XML namespace is not allowed\n",
  8979. attname, NULL, NULL);
  8980. goto next_attr;
  8981. } else {
  8982. parsedUri = xmlParseURI((const char *) uri);
  8983. if (parsedUri == NULL) {
  8984. xmlNsErr(ctxt, XML_WAR_NS_URI,
  8985. "xmlns:%s: '%s' is not a valid URI\n",
  8986. attname, uri, NULL);
  8987. } else {
  8988. if ((ctxt->pedantic) && (parsedUri->scheme == NULL)) {
  8989. xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE,
  8990. "xmlns:%s: URI %s is not absolute\n",
  8991. attname, uri, NULL);
  8992. }
  8993. xmlFreeURI(parsedUri);
  8994. }
  8995. }
  8996. if (xmlParserNsPush(ctxt, &hattname, &huri, NULL, 0) > 0)
  8997. nbNs++;
  8998. } else {
  8999. /*
  9000. * Populate attributes array, see above for repurposing
  9001. * of xmlChar pointers.
  9002. */
  9003. if ((atts == NULL) || (nbatts + 5 > maxatts)) {
  9004. if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
  9005. goto next_attr;
  9006. }
  9007. maxatts = ctxt->maxatts;
  9008. atts = ctxt->atts;
  9009. }
  9010. ctxt->attallocs[nratts++] = (hattname.hashValue & 0x7FFFFFFF) |
  9011. ((unsigned) alloc << 31);
  9012. atts[nbatts++] = attname;
  9013. atts[nbatts++] = aprefix;
  9014. atts[nbatts++] = (const xmlChar *) (size_t) haprefix.hashValue;
  9015. if (alloc) {
  9016. atts[nbatts++] = attvalue;
  9017. attvalue += len;
  9018. atts[nbatts++] = attvalue;
  9019. } else {
  9020. /*
  9021. * attvalue points into the input buffer which can be
  9022. * reallocated. Store differences to input->base instead.
  9023. * The pointers will be reconstructed later.
  9024. */
  9025. atts[nbatts++] = (void *) (attvalue - BASE_PTR);
  9026. attvalue += len;
  9027. atts[nbatts++] = (void *) (attvalue - BASE_PTR);
  9028. }
  9029. /*
  9030. * tag if some deallocation is needed
  9031. */
  9032. if (alloc != 0) attval = 1;
  9033. attvalue = NULL; /* moved into atts */
  9034. }
  9035. next_attr:
  9036. if ((attvalue != NULL) && (alloc != 0)) {
  9037. xmlFree(attvalue);
  9038. attvalue = NULL;
  9039. }
  9040. GROW
  9041. if (ctxt->instate == XML_PARSER_EOF)
  9042. break;
  9043. if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
  9044. break;
  9045. if (SKIP_BLANKS == 0) {
  9046. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  9047. "attributes construct error\n");
  9048. break;
  9049. }
  9050. GROW;
  9051. }
  9052. if (ctxt->input->id != inputid) {
  9053. xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
  9054. "Unexpected change of input\n");
  9055. localname = NULL;
  9056. goto done;
  9057. }
  9058. /*
  9059. * Namespaces from default attributes
  9060. */
  9061. if (ctxt->attsDefault != NULL) {
  9062. xmlDefAttrsPtr defaults;
  9063. defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
  9064. if (defaults != NULL) {
  9065. for (i = 0; i < defaults->nbAttrs; i++) {
  9066. xmlDefAttr *attr = &defaults->attrs[i];
  9067. attname = attr->name.name;
  9068. aprefix = attr->prefix.name;
  9069. if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
  9070. xmlParserEntityCheck(ctxt, attr->expandedSize);
  9071. if (xmlParserNsPush(ctxt, NULL, &attr->value, NULL, 1) > 0)
  9072. nbNs++;
  9073. } else if (aprefix == ctxt->str_xmlns) {
  9074. xmlParserEntityCheck(ctxt, attr->expandedSize);
  9075. if (xmlParserNsPush(ctxt, &attr->name, &attr->value,
  9076. NULL, 1) > 0)
  9077. nbNs++;
  9078. } else {
  9079. nbTotalDef += 1;
  9080. }
  9081. }
  9082. }
  9083. }
  9084. /*
  9085. * Resolve attribute namespaces
  9086. */
  9087. for (i = 0; i < nbatts; i += 5) {
  9088. attname = atts[i];
  9089. aprefix = atts[i+1];
  9090. /*
  9091. * The default namespace does not apply to attribute names.
  9092. */
  9093. if (aprefix == NULL) {
  9094. nsIndex = NS_INDEX_EMPTY;
  9095. } else if (aprefix == ctxt->str_xml) {
  9096. nsIndex = NS_INDEX_XML;
  9097. } else {
  9098. haprefix.name = aprefix;
  9099. haprefix.hashValue = (size_t) atts[i+2];
  9100. nsIndex = xmlParserNsLookup(ctxt, &haprefix, NULL);
  9101. if (nsIndex == INT_MAX) {
  9102. xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  9103. "Namespace prefix %s for %s on %s is not defined\n",
  9104. aprefix, attname, localname);
  9105. nsIndex = NS_INDEX_EMPTY;
  9106. }
  9107. }
  9108. atts[i+2] = (const xmlChar *) (ptrdiff_t) nsIndex;
  9109. }
  9110. /*
  9111. * Maximum number of attributes including default attributes.
  9112. */
  9113. maxAtts = nratts + nbTotalDef;
  9114. /*
  9115. * Verify that attribute names are unique.
  9116. */
  9117. if (maxAtts > 1) {
  9118. attrHashSize = 4;
  9119. while (attrHashSize / 2 < (unsigned) maxAtts)
  9120. attrHashSize *= 2;
  9121. if (attrHashSize > ctxt->attrHashMax) {
  9122. xmlAttrHashBucket *tmp;
  9123. tmp = xmlRealloc(ctxt->attrHash, attrHashSize * sizeof(tmp[0]));
  9124. if (tmp == NULL) {
  9125. xmlErrMemory(ctxt, NULL);
  9126. goto done;
  9127. }
  9128. ctxt->attrHash = tmp;
  9129. ctxt->attrHashMax = attrHashSize;
  9130. }
  9131. memset(ctxt->attrHash, -1, attrHashSize * sizeof(ctxt->attrHash[0]));
  9132. for (i = 0, j = 0; j < nratts; i += 5, j++) {
  9133. const xmlChar *nsuri;
  9134. unsigned hashValue, nameHashValue, uriHashValue;
  9135. int res;
  9136. attname = atts[i];
  9137. aprefix = atts[i+1];
  9138. nsIndex = (ptrdiff_t) atts[i+2];
  9139. /* Hash values always have bit 31 set, see dict.c */
  9140. nameHashValue = ctxt->attallocs[j] | 0x80000000;
  9141. if (nsIndex == NS_INDEX_EMPTY) {
  9142. nsuri = NULL;
  9143. uriHashValue = URI_HASH_EMPTY;
  9144. } else if (nsIndex == NS_INDEX_XML) {
  9145. nsuri = ctxt->str_xml_ns;
  9146. uriHashValue = URI_HASH_XML;
  9147. } else {
  9148. nsuri = ctxt->nsTab[nsIndex * 2 + 1];
  9149. uriHashValue = ctxt->nsdb->extra[nsIndex].uriHashValue;
  9150. }
  9151. hashValue = xmlDictCombineHash(nameHashValue, uriHashValue);
  9152. res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
  9153. hashValue, i);
  9154. if (res < 0)
  9155. continue;
  9156. /*
  9157. * [ WFC: Unique Att Spec ]
  9158. * No attribute name may appear more than once in the same
  9159. * start-tag or empty-element tag.
  9160. * As extended by the Namespace in XML REC.
  9161. */
  9162. if (res < INT_MAX) {
  9163. if (aprefix == atts[res+1]) {
  9164. xmlErrAttributeDup(ctxt, aprefix, attname);
  9165. } else {
  9166. xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
  9167. "Namespaced Attribute %s in '%s' redefined\n",
  9168. attname, nsuri, NULL);
  9169. }
  9170. }
  9171. }
  9172. }
  9173. /*
  9174. * Default attributes
  9175. */
  9176. if (ctxt->attsDefault != NULL) {
  9177. xmlDefAttrsPtr defaults;
  9178. defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
  9179. if (defaults != NULL) {
  9180. for (i = 0; i < defaults->nbAttrs; i++) {
  9181. xmlDefAttr *attr = &defaults->attrs[i];
  9182. const xmlChar *nsuri;
  9183. unsigned hashValue, uriHashValue;
  9184. int res;
  9185. attname = attr->name.name;
  9186. aprefix = attr->prefix.name;
  9187. if ((attname == ctxt->str_xmlns) && (aprefix == NULL))
  9188. continue;
  9189. if (aprefix == ctxt->str_xmlns)
  9190. continue;
  9191. if (aprefix == NULL) {
  9192. nsIndex = NS_INDEX_EMPTY;
  9193. nsuri = NULL;
  9194. uriHashValue = URI_HASH_EMPTY;
  9195. } if (aprefix == ctxt->str_xml) {
  9196. nsIndex = NS_INDEX_XML;
  9197. nsuri = ctxt->str_xml_ns;
  9198. uriHashValue = URI_HASH_XML;
  9199. } else if (aprefix != NULL) {
  9200. nsIndex = xmlParserNsLookup(ctxt, &attr->prefix, NULL);
  9201. if (nsIndex == INT_MAX) {
  9202. xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  9203. "Namespace prefix %s for %s on %s is not "
  9204. "defined\n",
  9205. aprefix, attname, localname);
  9206. nsIndex = NS_INDEX_EMPTY;
  9207. nsuri = NULL;
  9208. uriHashValue = URI_HASH_EMPTY;
  9209. } else {
  9210. nsuri = ctxt->nsTab[nsIndex * 2 + 1];
  9211. uriHashValue = ctxt->nsdb->extra[nsIndex].uriHashValue;
  9212. }
  9213. }
  9214. /*
  9215. * Check whether the attribute exists
  9216. */
  9217. if (maxAtts > 1) {
  9218. hashValue = xmlDictCombineHash(attr->name.hashValue,
  9219. uriHashValue);
  9220. res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
  9221. hashValue, nbatts);
  9222. if (res < 0)
  9223. continue;
  9224. if (res < INT_MAX) {
  9225. if (aprefix == atts[res+1])
  9226. continue;
  9227. xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
  9228. "Namespaced Attribute %s in '%s' redefined\n",
  9229. attname, nsuri, NULL);
  9230. }
  9231. }
  9232. xmlParserEntityCheck(ctxt, attr->expandedSize);
  9233. if ((atts == NULL) || (nbatts + 5 > maxatts)) {
  9234. if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
  9235. localname = NULL;
  9236. goto done;
  9237. }
  9238. maxatts = ctxt->maxatts;
  9239. atts = ctxt->atts;
  9240. }
  9241. atts[nbatts++] = attname;
  9242. atts[nbatts++] = aprefix;
  9243. atts[nbatts++] = (const xmlChar *) (ptrdiff_t) nsIndex;
  9244. atts[nbatts++] = attr->value.name;
  9245. atts[nbatts++] = attr->valueEnd;
  9246. if ((ctxt->standalone == 1) && (attr->external != 0)) {
  9247. xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED,
  9248. "standalone: attribute %s on %s defaulted "
  9249. "from external subset\n",
  9250. attname, localname);
  9251. }
  9252. nbdef++;
  9253. }
  9254. }
  9255. }
  9256. /*
  9257. * Reconstruct attribute pointers
  9258. */
  9259. for (i = 0, j = 0; i < nbatts; i += 5, j++) {
  9260. /* namespace URI */
  9261. nsIndex = (ptrdiff_t) atts[i+2];
  9262. if (nsIndex == INT_MAX)
  9263. atts[i+2] = NULL;
  9264. else if (nsIndex == INT_MAX - 1)
  9265. atts[i+2] = ctxt->str_xml_ns;
  9266. else
  9267. atts[i+2] = ctxt->nsTab[nsIndex * 2 + 1];
  9268. if ((j < nratts) && (ctxt->attallocs[j] & 0x80000000) == 0) {
  9269. atts[i+3] = BASE_PTR + (ptrdiff_t) atts[i+3]; /* value */
  9270. atts[i+4] = BASE_PTR + (ptrdiff_t) atts[i+4]; /* valuend */
  9271. }
  9272. }
  9273. uri = xmlParserNsLookupUri(ctxt, &hprefix);
  9274. if ((prefix != NULL) && (uri == NULL)) {
  9275. xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  9276. "Namespace prefix %s on %s is not defined\n",
  9277. prefix, localname, NULL);
  9278. }
  9279. *pref = prefix;
  9280. *URI = uri;
  9281. /*
  9282. * SAX callback
  9283. */
  9284. if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) &&
  9285. (!ctxt->disableSAX)) {
  9286. if (nbNs > 0)
  9287. ctxt->sax->startElementNs(ctxt->userData, localname, prefix, uri,
  9288. nbNs, ctxt->nsTab + 2 * (ctxt->nsNr - nbNs),
  9289. nbatts / 5, nbdef, atts);
  9290. else
  9291. ctxt->sax->startElementNs(ctxt->userData, localname, prefix, uri,
  9292. 0, NULL, nbatts / 5, nbdef, atts);
  9293. }
  9294. done:
  9295. /*
  9296. * Free allocated attribute values
  9297. */
  9298. if (attval != 0) {
  9299. for (i = 0, j = 0; j < nratts; i += 5, j++)
  9300. if (ctxt->attallocs[j] & 0x80000000)
  9301. xmlFree((xmlChar *) atts[i+3]);
  9302. }
  9303. *nbNsPtr = nbNs;
  9304. return(localname);
  9305. }
  9306. /**
  9307. * xmlParseEndTag2:
  9308. * @ctxt: an XML parser context
  9309. * @line: line of the start tag
  9310. * @nsNr: number of namespaces on the start tag
  9311. *
  9312. * Parse an end tag. Always consumes '</'.
  9313. *
  9314. * [42] ETag ::= '</' Name S? '>'
  9315. *
  9316. * With namespace
  9317. *
  9318. * [NS 9] ETag ::= '</' QName S? '>'
  9319. */
  9320. static void
  9321. xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlStartTag *tag) {
  9322. const xmlChar *name;
  9323. GROW;
  9324. if ((RAW != '<') || (NXT(1) != '/')) {
  9325. xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL);
  9326. return;
  9327. }
  9328. SKIP(2);
  9329. if (tag->prefix == NULL)
  9330. name = xmlParseNameAndCompare(ctxt, ctxt->name);
  9331. else
  9332. name = xmlParseQNameAndCompare(ctxt, ctxt->name, tag->prefix);
  9333. /*
  9334. * We should definitely be at the ending "S? '>'" part
  9335. */
  9336. GROW;
  9337. if (ctxt->instate == XML_PARSER_EOF)
  9338. return;
  9339. SKIP_BLANKS;
  9340. if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
  9341. xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
  9342. } else
  9343. NEXT1;
  9344. /*
  9345. * [ WFC: Element Type Match ]
  9346. * The Name in an element's end-tag must match the element type in the
  9347. * start-tag.
  9348. *
  9349. */
  9350. if (name != (xmlChar*)1) {
  9351. if (name == NULL) name = BAD_CAST "unparsable";
  9352. xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
  9353. "Opening and ending tag mismatch: %s line %d and %s\n",
  9354. ctxt->name, tag->line, name);
  9355. }
  9356. /*
  9357. * SAX: End of Tag
  9358. */
  9359. if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
  9360. (!ctxt->disableSAX))
  9361. ctxt->sax->endElementNs(ctxt->userData, ctxt->name, tag->prefix,
  9362. tag->URI);
  9363. spacePop(ctxt);
  9364. if (tag->nsNr != 0)
  9365. xmlParserNsPop(ctxt, tag->nsNr);
  9366. }
  9367. /**
  9368. * xmlParseCDSect:
  9369. * @ctxt: an XML parser context
  9370. *
  9371. * DEPRECATED: Internal function, don't use.
  9372. *
  9373. * Parse escaped pure raw content. Always consumes '<!['.
  9374. *
  9375. * [18] CDSect ::= CDStart CData CDEnd
  9376. *
  9377. * [19] CDStart ::= '<![CDATA['
  9378. *
  9379. * [20] Data ::= (Char* - (Char* ']]>' Char*))
  9380. *
  9381. * [21] CDEnd ::= ']]>'
  9382. */
  9383. void
  9384. xmlParseCDSect(xmlParserCtxtPtr ctxt) {
  9385. xmlChar *buf = NULL;
  9386. int len = 0;
  9387. int size = XML_PARSER_BUFFER_SIZE;
  9388. int r, rl;
  9389. int s, sl;
  9390. int cur, l;
  9391. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  9392. XML_MAX_HUGE_LENGTH :
  9393. XML_MAX_TEXT_LENGTH;
  9394. if ((CUR != '<') || (NXT(1) != '!') || (NXT(2) != '['))
  9395. return;
  9396. SKIP(3);
  9397. if (!CMP6(CUR_PTR, 'C', 'D', 'A', 'T', 'A', '['))
  9398. return;
  9399. SKIP(6);
  9400. ctxt->instate = XML_PARSER_CDATA_SECTION;
  9401. r = CUR_CHAR(rl);
  9402. if (!IS_CHAR(r)) {
  9403. xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
  9404. goto out;
  9405. }
  9406. NEXTL(rl);
  9407. s = CUR_CHAR(sl);
  9408. if (!IS_CHAR(s)) {
  9409. xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
  9410. goto out;
  9411. }
  9412. NEXTL(sl);
  9413. cur = CUR_CHAR(l);
  9414. buf = (xmlChar *) xmlMallocAtomic(size);
  9415. if (buf == NULL) {
  9416. xmlErrMemory(ctxt, NULL);
  9417. goto out;
  9418. }
  9419. while (IS_CHAR(cur) &&
  9420. ((r != ']') || (s != ']') || (cur != '>'))) {
  9421. if (len + 5 >= size) {
  9422. xmlChar *tmp;
  9423. tmp = (xmlChar *) xmlRealloc(buf, size * 2);
  9424. if (tmp == NULL) {
  9425. xmlErrMemory(ctxt, NULL);
  9426. goto out;
  9427. }
  9428. buf = tmp;
  9429. size *= 2;
  9430. }
  9431. COPY_BUF(buf, len, r);
  9432. if (len > maxLength) {
  9433. xmlFatalErrMsg(ctxt, XML_ERR_CDATA_NOT_FINISHED,
  9434. "CData section too big found\n");
  9435. goto out;
  9436. }
  9437. r = s;
  9438. rl = sl;
  9439. s = cur;
  9440. sl = l;
  9441. NEXTL(l);
  9442. cur = CUR_CHAR(l);
  9443. }
  9444. buf[len] = 0;
  9445. if (ctxt->instate == XML_PARSER_EOF) {
  9446. xmlFree(buf);
  9447. return;
  9448. }
  9449. if (cur != '>') {
  9450. xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
  9451. "CData section not finished\n%.50s\n", buf);
  9452. goto out;
  9453. }
  9454. NEXTL(l);
  9455. /*
  9456. * OK the buffer is to be consumed as cdata.
  9457. */
  9458. if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
  9459. if (ctxt->sax->cdataBlock != NULL)
  9460. ctxt->sax->cdataBlock(ctxt->userData, buf, len);
  9461. else if (ctxt->sax->characters != NULL)
  9462. ctxt->sax->characters(ctxt->userData, buf, len);
  9463. }
  9464. out:
  9465. if (ctxt->instate != XML_PARSER_EOF)
  9466. ctxt->instate = XML_PARSER_CONTENT;
  9467. xmlFree(buf);
  9468. }
  9469. /**
  9470. * xmlParseContentInternal:
  9471. * @ctxt: an XML parser context
  9472. *
  9473. * Parse a content sequence. Stops at EOF or '</'. Leaves checking of
  9474. * unexpected EOF to the caller.
  9475. */
  9476. static void
  9477. xmlParseContentInternal(xmlParserCtxtPtr ctxt) {
  9478. int nameNr = ctxt->nameNr;
  9479. GROW;
  9480. while ((ctxt->input->cur < ctxt->input->end) &&
  9481. (ctxt->instate != XML_PARSER_EOF)) {
  9482. const xmlChar *cur = ctxt->input->cur;
  9483. /*
  9484. * First case : a Processing Instruction.
  9485. */
  9486. if ((*cur == '<') && (cur[1] == '?')) {
  9487. xmlParsePI(ctxt);
  9488. }
  9489. /*
  9490. * Second case : a CDSection
  9491. */
  9492. /* 2.6.0 test was *cur not RAW */
  9493. else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
  9494. xmlParseCDSect(ctxt);
  9495. }
  9496. /*
  9497. * Third case : a comment
  9498. */
  9499. else if ((*cur == '<') && (NXT(1) == '!') &&
  9500. (NXT(2) == '-') && (NXT(3) == '-')) {
  9501. xmlParseComment(ctxt);
  9502. ctxt->instate = XML_PARSER_CONTENT;
  9503. }
  9504. /*
  9505. * Fourth case : a sub-element.
  9506. */
  9507. else if (*cur == '<') {
  9508. if (NXT(1) == '/') {
  9509. if (ctxt->nameNr <= nameNr)
  9510. break;
  9511. xmlParseElementEnd(ctxt);
  9512. } else {
  9513. xmlParseElementStart(ctxt);
  9514. }
  9515. }
  9516. /*
  9517. * Fifth case : a reference. If if has not been resolved,
  9518. * parsing returns it's Name, create the node
  9519. */
  9520. else if (*cur == '&') {
  9521. xmlParseReference(ctxt);
  9522. }
  9523. /*
  9524. * Last case, text. Note that References are handled directly.
  9525. */
  9526. else {
  9527. xmlParseCharDataInternal(ctxt, 0);
  9528. }
  9529. SHRINK;
  9530. GROW;
  9531. }
  9532. }
  9533. /**
  9534. * xmlParseContent:
  9535. * @ctxt: an XML parser context
  9536. *
  9537. * Parse a content sequence. Stops at EOF or '</'.
  9538. *
  9539. * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
  9540. */
  9541. void
  9542. xmlParseContent(xmlParserCtxtPtr ctxt) {
  9543. int nameNr = ctxt->nameNr;
  9544. xmlParseContentInternal(ctxt);
  9545. if ((ctxt->instate != XML_PARSER_EOF) &&
  9546. (ctxt->errNo == XML_ERR_OK) &&
  9547. (ctxt->nameNr > nameNr)) {
  9548. const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
  9549. int line = ctxt->pushTab[ctxt->nameNr - 1].line;
  9550. xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
  9551. "Premature end of data in tag %s line %d\n",
  9552. name, line, NULL);
  9553. }
  9554. }
  9555. /**
  9556. * xmlParseElement:
  9557. * @ctxt: an XML parser context
  9558. *
  9559. * DEPRECATED: Internal function, don't use.
  9560. *
  9561. * parse an XML element
  9562. *
  9563. * [39] element ::= EmptyElemTag | STag content ETag
  9564. *
  9565. * [ WFC: Element Type Match ]
  9566. * The Name in an element's end-tag must match the element type in the
  9567. * start-tag.
  9568. *
  9569. */
  9570. void
  9571. xmlParseElement(xmlParserCtxtPtr ctxt) {
  9572. if (xmlParseElementStart(ctxt) != 0)
  9573. return;
  9574. xmlParseContentInternal(ctxt);
  9575. if (ctxt->instate == XML_PARSER_EOF)
  9576. return;
  9577. if (ctxt->input->cur >= ctxt->input->end) {
  9578. if (ctxt->errNo == XML_ERR_OK) {
  9579. const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
  9580. int line = ctxt->pushTab[ctxt->nameNr - 1].line;
  9581. xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
  9582. "Premature end of data in tag %s line %d\n",
  9583. name, line, NULL);
  9584. }
  9585. return;
  9586. }
  9587. xmlParseElementEnd(ctxt);
  9588. }
  9589. /**
  9590. * xmlParseElementStart:
  9591. * @ctxt: an XML parser context
  9592. *
  9593. * Parse the start of an XML element. Returns -1 in case of error, 0 if an
  9594. * opening tag was parsed, 1 if an empty element was parsed.
  9595. *
  9596. * Always consumes '<'.
  9597. */
  9598. static int
  9599. xmlParseElementStart(xmlParserCtxtPtr ctxt) {
  9600. const xmlChar *name;
  9601. const xmlChar *prefix = NULL;
  9602. const xmlChar *URI = NULL;
  9603. xmlParserNodeInfo node_info;
  9604. int line;
  9605. xmlNodePtr cur;
  9606. int nbNs = 0;
  9607. if (((unsigned int) ctxt->nameNr > xmlParserMaxDepth) &&
  9608. ((ctxt->options & XML_PARSE_HUGE) == 0)) {
  9609. xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR,
  9610. "Excessive depth in document: %d use XML_PARSE_HUGE option\n",
  9611. xmlParserMaxDepth);
  9612. xmlHaltParser(ctxt);
  9613. return(-1);
  9614. }
  9615. /* Capture start position */
  9616. if (ctxt->record_info) {
  9617. node_info.begin_pos = ctxt->input->consumed +
  9618. (CUR_PTR - ctxt->input->base);
  9619. node_info.begin_line = ctxt->input->line;
  9620. }
  9621. if (ctxt->spaceNr == 0)
  9622. spacePush(ctxt, -1);
  9623. else if (*ctxt->space == -2)
  9624. spacePush(ctxt, -1);
  9625. else
  9626. spacePush(ctxt, *ctxt->space);
  9627. line = ctxt->input->line;
  9628. #ifdef LIBXML_SAX1_ENABLED
  9629. if (ctxt->sax2)
  9630. #endif /* LIBXML_SAX1_ENABLED */
  9631. name = xmlParseStartTag2(ctxt, &prefix, &URI, &nbNs);
  9632. #ifdef LIBXML_SAX1_ENABLED
  9633. else
  9634. name = xmlParseStartTag(ctxt);
  9635. #endif /* LIBXML_SAX1_ENABLED */
  9636. if (ctxt->instate == XML_PARSER_EOF)
  9637. return(-1);
  9638. if (name == NULL) {
  9639. spacePop(ctxt);
  9640. return(-1);
  9641. }
  9642. nameNsPush(ctxt, name, prefix, URI, line, nbNs);
  9643. cur = ctxt->node;
  9644. #ifdef LIBXML_VALID_ENABLED
  9645. /*
  9646. * [ VC: Root Element Type ]
  9647. * The Name in the document type declaration must match the element
  9648. * type of the root element.
  9649. */
  9650. if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
  9651. ctxt->node && (ctxt->node == ctxt->myDoc->children))
  9652. ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
  9653. #endif /* LIBXML_VALID_ENABLED */
  9654. /*
  9655. * Check for an Empty Element.
  9656. */
  9657. if ((RAW == '/') && (NXT(1) == '>')) {
  9658. SKIP(2);
  9659. if (ctxt->sax2) {
  9660. if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
  9661. (!ctxt->disableSAX))
  9662. ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI);
  9663. #ifdef LIBXML_SAX1_ENABLED
  9664. } else {
  9665. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
  9666. (!ctxt->disableSAX))
  9667. ctxt->sax->endElement(ctxt->userData, name);
  9668. #endif /* LIBXML_SAX1_ENABLED */
  9669. }
  9670. namePop(ctxt);
  9671. spacePop(ctxt);
  9672. if (nbNs > 0)
  9673. xmlParserNsPop(ctxt, nbNs);
  9674. if (cur != NULL && ctxt->record_info) {
  9675. node_info.node = cur;
  9676. node_info.end_pos = ctxt->input->consumed +
  9677. (CUR_PTR - ctxt->input->base);
  9678. node_info.end_line = ctxt->input->line;
  9679. xmlParserAddNodeInfo(ctxt, &node_info);
  9680. }
  9681. return(1);
  9682. }
  9683. if (RAW == '>') {
  9684. NEXT1;
  9685. if (cur != NULL && ctxt->record_info) {
  9686. node_info.node = cur;
  9687. node_info.end_pos = 0;
  9688. node_info.end_line = 0;
  9689. xmlParserAddNodeInfo(ctxt, &node_info);
  9690. }
  9691. } else {
  9692. xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED,
  9693. "Couldn't find end of Start Tag %s line %d\n",
  9694. name, line, NULL);
  9695. /*
  9696. * end of parsing of this node.
  9697. */
  9698. nodePop(ctxt);
  9699. namePop(ctxt);
  9700. spacePop(ctxt);
  9701. if (nbNs > 0)
  9702. xmlParserNsPop(ctxt, nbNs);
  9703. return(-1);
  9704. }
  9705. return(0);
  9706. }
  9707. /**
  9708. * xmlParseElementEnd:
  9709. * @ctxt: an XML parser context
  9710. *
  9711. * Parse the end of an XML element. Always consumes '</'.
  9712. */
  9713. static void
  9714. xmlParseElementEnd(xmlParserCtxtPtr ctxt) {
  9715. xmlNodePtr cur = ctxt->node;
  9716. if (ctxt->nameNr <= 0) {
  9717. if ((RAW == '<') && (NXT(1) == '/'))
  9718. SKIP(2);
  9719. return;
  9720. }
  9721. /*
  9722. * parse the end of tag: '</' should be here.
  9723. */
  9724. if (ctxt->sax2) {
  9725. xmlParseEndTag2(ctxt, &ctxt->pushTab[ctxt->nameNr - 1]);
  9726. namePop(ctxt);
  9727. }
  9728. #ifdef LIBXML_SAX1_ENABLED
  9729. else
  9730. xmlParseEndTag1(ctxt, 0);
  9731. #endif /* LIBXML_SAX1_ENABLED */
  9732. /*
  9733. * Capture end position
  9734. */
  9735. if (cur != NULL && ctxt->record_info) {
  9736. xmlParserNodeInfoPtr node_info;
  9737. node_info = (xmlParserNodeInfoPtr) xmlParserFindNodeInfo(ctxt, cur);
  9738. if (node_info != NULL) {
  9739. node_info->end_pos = ctxt->input->consumed +
  9740. (CUR_PTR - ctxt->input->base);
  9741. node_info->end_line = ctxt->input->line;
  9742. }
  9743. }
  9744. }
  9745. /**
  9746. * xmlParseVersionNum:
  9747. * @ctxt: an XML parser context
  9748. *
  9749. * DEPRECATED: Internal function, don't use.
  9750. *
  9751. * parse the XML version value.
  9752. *
  9753. * [26] VersionNum ::= '1.' [0-9]+
  9754. *
  9755. * In practice allow [0-9].[0-9]+ at that level
  9756. *
  9757. * Returns the string giving the XML version number, or NULL
  9758. */
  9759. xmlChar *
  9760. xmlParseVersionNum(xmlParserCtxtPtr ctxt) {
  9761. xmlChar *buf = NULL;
  9762. int len = 0;
  9763. int size = 10;
  9764. xmlChar cur;
  9765. buf = (xmlChar *) xmlMallocAtomic(size);
  9766. if (buf == NULL) {
  9767. xmlErrMemory(ctxt, NULL);
  9768. return(NULL);
  9769. }
  9770. cur = CUR;
  9771. if (!((cur >= '0') && (cur <= '9'))) {
  9772. xmlFree(buf);
  9773. return(NULL);
  9774. }
  9775. buf[len++] = cur;
  9776. NEXT;
  9777. cur=CUR;
  9778. if (cur != '.') {
  9779. xmlFree(buf);
  9780. return(NULL);
  9781. }
  9782. buf[len++] = cur;
  9783. NEXT;
  9784. cur=CUR;
  9785. while ((cur >= '0') && (cur <= '9')) {
  9786. if (len + 1 >= size) {
  9787. xmlChar *tmp;
  9788. size *= 2;
  9789. tmp = (xmlChar *) xmlRealloc(buf, size);
  9790. if (tmp == NULL) {
  9791. xmlFree(buf);
  9792. xmlErrMemory(ctxt, NULL);
  9793. return(NULL);
  9794. }
  9795. buf = tmp;
  9796. }
  9797. buf[len++] = cur;
  9798. NEXT;
  9799. cur=CUR;
  9800. }
  9801. buf[len] = 0;
  9802. return(buf);
  9803. }
  9804. /**
  9805. * xmlParseVersionInfo:
  9806. * @ctxt: an XML parser context
  9807. *
  9808. * DEPRECATED: Internal function, don't use.
  9809. *
  9810. * parse the XML version.
  9811. *
  9812. * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
  9813. *
  9814. * [25] Eq ::= S? '=' S?
  9815. *
  9816. * Returns the version string, e.g. "1.0"
  9817. */
  9818. xmlChar *
  9819. xmlParseVersionInfo(xmlParserCtxtPtr ctxt) {
  9820. xmlChar *version = NULL;
  9821. if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) {
  9822. SKIP(7);
  9823. SKIP_BLANKS;
  9824. if (RAW != '=') {
  9825. xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
  9826. return(NULL);
  9827. }
  9828. NEXT;
  9829. SKIP_BLANKS;
  9830. if (RAW == '"') {
  9831. NEXT;
  9832. version = xmlParseVersionNum(ctxt);
  9833. if (RAW != '"') {
  9834. xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
  9835. } else
  9836. NEXT;
  9837. } else if (RAW == '\''){
  9838. NEXT;
  9839. version = xmlParseVersionNum(ctxt);
  9840. if (RAW != '\'') {
  9841. xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
  9842. } else
  9843. NEXT;
  9844. } else {
  9845. xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
  9846. }
  9847. }
  9848. return(version);
  9849. }
  9850. /**
  9851. * xmlParseEncName:
  9852. * @ctxt: an XML parser context
  9853. *
  9854. * DEPRECATED: Internal function, don't use.
  9855. *
  9856. * parse the XML encoding name
  9857. *
  9858. * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
  9859. *
  9860. * Returns the encoding name value or NULL
  9861. */
  9862. xmlChar *
  9863. xmlParseEncName(xmlParserCtxtPtr ctxt) {
  9864. xmlChar *buf = NULL;
  9865. int len = 0;
  9866. int size = 10;
  9867. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  9868. XML_MAX_TEXT_LENGTH :
  9869. XML_MAX_NAME_LENGTH;
  9870. xmlChar cur;
  9871. cur = CUR;
  9872. if (((cur >= 'a') && (cur <= 'z')) ||
  9873. ((cur >= 'A') && (cur <= 'Z'))) {
  9874. buf = (xmlChar *) xmlMallocAtomic(size);
  9875. if (buf == NULL) {
  9876. xmlErrMemory(ctxt, NULL);
  9877. return(NULL);
  9878. }
  9879. buf[len++] = cur;
  9880. NEXT;
  9881. cur = CUR;
  9882. while (((cur >= 'a') && (cur <= 'z')) ||
  9883. ((cur >= 'A') && (cur <= 'Z')) ||
  9884. ((cur >= '0') && (cur <= '9')) ||
  9885. (cur == '.') || (cur == '_') ||
  9886. (cur == '-')) {
  9887. if (len + 1 >= size) {
  9888. xmlChar *tmp;
  9889. size *= 2;
  9890. tmp = (xmlChar *) xmlRealloc(buf, size);
  9891. if (tmp == NULL) {
  9892. xmlErrMemory(ctxt, NULL);
  9893. xmlFree(buf);
  9894. return(NULL);
  9895. }
  9896. buf = tmp;
  9897. }
  9898. buf[len++] = cur;
  9899. if (len > maxLength) {
  9900. xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "EncName");
  9901. xmlFree(buf);
  9902. return(NULL);
  9903. }
  9904. NEXT;
  9905. cur = CUR;
  9906. }
  9907. buf[len] = 0;
  9908. } else {
  9909. xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL);
  9910. }
  9911. return(buf);
  9912. }
  9913. /**
  9914. * xmlParseEncodingDecl:
  9915. * @ctxt: an XML parser context
  9916. *
  9917. * DEPRECATED: Internal function, don't use.
  9918. *
  9919. * parse the XML encoding declaration
  9920. *
  9921. * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'")
  9922. *
  9923. * this setups the conversion filters.
  9924. *
  9925. * Returns the encoding value or NULL
  9926. */
  9927. const xmlChar *
  9928. xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) {
  9929. xmlChar *encoding = NULL;
  9930. SKIP_BLANKS;
  9931. if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g') == 0)
  9932. return(NULL);
  9933. SKIP(8);
  9934. SKIP_BLANKS;
  9935. if (RAW != '=') {
  9936. xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
  9937. return(NULL);
  9938. }
  9939. NEXT;
  9940. SKIP_BLANKS;
  9941. if (RAW == '"') {
  9942. NEXT;
  9943. encoding = xmlParseEncName(ctxt);
  9944. if (RAW != '"') {
  9945. xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
  9946. xmlFree((xmlChar *) encoding);
  9947. return(NULL);
  9948. } else
  9949. NEXT;
  9950. } else if (RAW == '\''){
  9951. NEXT;
  9952. encoding = xmlParseEncName(ctxt);
  9953. if (RAW != '\'') {
  9954. xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
  9955. xmlFree((xmlChar *) encoding);
  9956. return(NULL);
  9957. } else
  9958. NEXT;
  9959. } else {
  9960. xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
  9961. }
  9962. if (encoding == NULL)
  9963. return(NULL);
  9964. xmlSetDeclaredEncoding(ctxt, encoding);
  9965. return(ctxt->encoding);
  9966. }
  9967. /**
  9968. * xmlParseSDDecl:
  9969. * @ctxt: an XML parser context
  9970. *
  9971. * DEPRECATED: Internal function, don't use.
  9972. *
  9973. * parse the XML standalone declaration
  9974. *
  9975. * [32] SDDecl ::= S 'standalone' Eq
  9976. * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"'))
  9977. *
  9978. * [ VC: Standalone Document Declaration ]
  9979. * TODO The standalone document declaration must have the value "no"
  9980. * if any external markup declarations contain declarations of:
  9981. * - attributes with default values, if elements to which these
  9982. * attributes apply appear in the document without specifications
  9983. * of values for these attributes, or
  9984. * - entities (other than amp, lt, gt, apos, quot), if references
  9985. * to those entities appear in the document, or
  9986. * - attributes with values subject to normalization, where the
  9987. * attribute appears in the document with a value which will change
  9988. * as a result of normalization, or
  9989. * - element types with element content, if white space occurs directly
  9990. * within any instance of those types.
  9991. *
  9992. * Returns:
  9993. * 1 if standalone="yes"
  9994. * 0 if standalone="no"
  9995. * -2 if standalone attribute is missing or invalid
  9996. * (A standalone value of -2 means that the XML declaration was found,
  9997. * but no value was specified for the standalone attribute).
  9998. */
  9999. int
  10000. xmlParseSDDecl(xmlParserCtxtPtr ctxt) {
  10001. int standalone = -2;
  10002. SKIP_BLANKS;
  10003. if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) {
  10004. SKIP(10);
  10005. SKIP_BLANKS;
  10006. if (RAW != '=') {
  10007. xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
  10008. return(standalone);
  10009. }
  10010. NEXT;
  10011. SKIP_BLANKS;
  10012. if (RAW == '\''){
  10013. NEXT;
  10014. if ((RAW == 'n') && (NXT(1) == 'o')) {
  10015. standalone = 0;
  10016. SKIP(2);
  10017. } else if ((RAW == 'y') && (NXT(1) == 'e') &&
  10018. (NXT(2) == 's')) {
  10019. standalone = 1;
  10020. SKIP(3);
  10021. } else {
  10022. xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
  10023. }
  10024. if (RAW != '\'') {
  10025. xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
  10026. } else
  10027. NEXT;
  10028. } else if (RAW == '"'){
  10029. NEXT;
  10030. if ((RAW == 'n') && (NXT(1) == 'o')) {
  10031. standalone = 0;
  10032. SKIP(2);
  10033. } else if ((RAW == 'y') && (NXT(1) == 'e') &&
  10034. (NXT(2) == 's')) {
  10035. standalone = 1;
  10036. SKIP(3);
  10037. } else {
  10038. xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
  10039. }
  10040. if (RAW != '"') {
  10041. xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
  10042. } else
  10043. NEXT;
  10044. } else {
  10045. xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
  10046. }
  10047. }
  10048. return(standalone);
  10049. }
  10050. /**
  10051. * xmlParseXMLDecl:
  10052. * @ctxt: an XML parser context
  10053. *
  10054. * DEPRECATED: Internal function, don't use.
  10055. *
  10056. * parse an XML declaration header
  10057. *
  10058. * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
  10059. */
  10060. void
  10061. xmlParseXMLDecl(xmlParserCtxtPtr ctxt) {
  10062. xmlChar *version;
  10063. /*
  10064. * This value for standalone indicates that the document has an
  10065. * XML declaration but it does not have a standalone attribute.
  10066. * It will be overwritten later if a standalone attribute is found.
  10067. */
  10068. ctxt->standalone = -2;
  10069. /*
  10070. * We know that '<?xml' is here.
  10071. */
  10072. SKIP(5);
  10073. if (!IS_BLANK_CH(RAW)) {
  10074. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
  10075. "Blank needed after '<?xml'\n");
  10076. }
  10077. SKIP_BLANKS;
  10078. /*
  10079. * We must have the VersionInfo here.
  10080. */
  10081. version = xmlParseVersionInfo(ctxt);
  10082. if (version == NULL) {
  10083. xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL);
  10084. } else {
  10085. if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) {
  10086. /*
  10087. * Changed here for XML-1.0 5th edition
  10088. */
  10089. if (ctxt->options & XML_PARSE_OLD10) {
  10090. xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION,
  10091. "Unsupported version '%s'\n",
  10092. version);
  10093. } else {
  10094. if ((version[0] == '1') && ((version[1] == '.'))) {
  10095. xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
  10096. "Unsupported version '%s'\n",
  10097. version, NULL);
  10098. } else {
  10099. xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION,
  10100. "Unsupported version '%s'\n",
  10101. version);
  10102. }
  10103. }
  10104. }
  10105. if (ctxt->version != NULL)
  10106. xmlFree((void *) ctxt->version);
  10107. ctxt->version = version;
  10108. }
  10109. /*
  10110. * We may have the encoding declaration
  10111. */
  10112. if (!IS_BLANK_CH(RAW)) {
  10113. if ((RAW == '?') && (NXT(1) == '>')) {
  10114. SKIP(2);
  10115. return;
  10116. }
  10117. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
  10118. }
  10119. xmlParseEncodingDecl(ctxt);
  10120. if ((ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) ||
  10121. (ctxt->instate == XML_PARSER_EOF)) {
  10122. /*
  10123. * The XML REC instructs us to stop parsing right here
  10124. */
  10125. return;
  10126. }
  10127. /*
  10128. * We may have the standalone status.
  10129. */
  10130. if ((ctxt->encoding != NULL) && (!IS_BLANK_CH(RAW))) {
  10131. if ((RAW == '?') && (NXT(1) == '>')) {
  10132. SKIP(2);
  10133. return;
  10134. }
  10135. xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
  10136. }
  10137. /*
  10138. * We can grow the input buffer freely at that point
  10139. */
  10140. GROW;
  10141. SKIP_BLANKS;
  10142. ctxt->standalone = xmlParseSDDecl(ctxt);
  10143. SKIP_BLANKS;
  10144. if ((RAW == '?') && (NXT(1) == '>')) {
  10145. SKIP(2);
  10146. } else if (RAW == '>') {
  10147. /* Deprecated old WD ... */
  10148. xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
  10149. NEXT;
  10150. } else {
  10151. int c;
  10152. xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
  10153. while ((c = CUR) != 0) {
  10154. NEXT;
  10155. if (c == '>')
  10156. break;
  10157. }
  10158. }
  10159. }
  10160. /**
  10161. * xmlParseMisc:
  10162. * @ctxt: an XML parser context
  10163. *
  10164. * DEPRECATED: Internal function, don't use.
  10165. *
  10166. * parse an XML Misc* optional field.
  10167. *
  10168. * [27] Misc ::= Comment | PI | S
  10169. */
  10170. void
  10171. xmlParseMisc(xmlParserCtxtPtr ctxt) {
  10172. while (ctxt->instate != XML_PARSER_EOF) {
  10173. SKIP_BLANKS;
  10174. GROW;
  10175. if ((RAW == '<') && (NXT(1) == '?')) {
  10176. xmlParsePI(ctxt);
  10177. } else if (CMP4(CUR_PTR, '<', '!', '-', '-')) {
  10178. xmlParseComment(ctxt);
  10179. } else {
  10180. break;
  10181. }
  10182. }
  10183. }
  10184. /**
  10185. * xmlParseDocument:
  10186. * @ctxt: an XML parser context
  10187. *
  10188. * parse an XML document (and build a tree if using the standard SAX
  10189. * interface).
  10190. *
  10191. * [1] document ::= prolog element Misc*
  10192. *
  10193. * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
  10194. *
  10195. * Returns 0, -1 in case of error. the parser context is augmented
  10196. * as a result of the parsing.
  10197. */
  10198. int
  10199. xmlParseDocument(xmlParserCtxtPtr ctxt) {
  10200. xmlInitParser();
  10201. if ((ctxt == NULL) || (ctxt->input == NULL))
  10202. return(-1);
  10203. GROW;
  10204. /*
  10205. * SAX: detecting the level.
  10206. */
  10207. xmlDetectSAX2(ctxt);
  10208. /*
  10209. * SAX: beginning of the document processing.
  10210. */
  10211. if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
  10212. ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
  10213. if (ctxt->instate == XML_PARSER_EOF)
  10214. return(-1);
  10215. xmlDetectEncoding(ctxt);
  10216. if (CUR == 0) {
  10217. xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
  10218. return(-1);
  10219. }
  10220. GROW;
  10221. if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
  10222. /*
  10223. * Note that we will switch encoding on the fly.
  10224. */
  10225. xmlParseXMLDecl(ctxt);
  10226. if ((ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) ||
  10227. (ctxt->instate == XML_PARSER_EOF)) {
  10228. /*
  10229. * The XML REC instructs us to stop parsing right here
  10230. */
  10231. return(-1);
  10232. }
  10233. SKIP_BLANKS;
  10234. } else {
  10235. ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
  10236. }
  10237. if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
  10238. ctxt->sax->startDocument(ctxt->userData);
  10239. if (ctxt->instate == XML_PARSER_EOF)
  10240. return(-1);
  10241. if ((ctxt->myDoc != NULL) && (ctxt->input != NULL) &&
  10242. (ctxt->input->buf != NULL) && (ctxt->input->buf->compressed >= 0)) {
  10243. ctxt->myDoc->compression = ctxt->input->buf->compressed;
  10244. }
  10245. /*
  10246. * The Misc part of the Prolog
  10247. */
  10248. xmlParseMisc(ctxt);
  10249. /*
  10250. * Then possibly doc type declaration(s) and more Misc
  10251. * (doctypedecl Misc*)?
  10252. */
  10253. GROW;
  10254. if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) {
  10255. ctxt->inSubset = 1;
  10256. xmlParseDocTypeDecl(ctxt);
  10257. if (RAW == '[') {
  10258. ctxt->instate = XML_PARSER_DTD;
  10259. xmlParseInternalSubset(ctxt);
  10260. if (ctxt->instate == XML_PARSER_EOF)
  10261. return(-1);
  10262. }
  10263. /*
  10264. * Create and update the external subset.
  10265. */
  10266. ctxt->inSubset = 2;
  10267. if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) &&
  10268. (!ctxt->disableSAX))
  10269. ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
  10270. ctxt->extSubSystem, ctxt->extSubURI);
  10271. if (ctxt->instate == XML_PARSER_EOF)
  10272. return(-1);
  10273. ctxt->inSubset = 0;
  10274. xmlCleanSpecialAttr(ctxt);
  10275. ctxt->instate = XML_PARSER_PROLOG;
  10276. xmlParseMisc(ctxt);
  10277. }
  10278. /*
  10279. * Time to start parsing the tree itself
  10280. */
  10281. GROW;
  10282. if (RAW != '<') {
  10283. xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
  10284. "Start tag expected, '<' not found\n");
  10285. } else {
  10286. ctxt->instate = XML_PARSER_CONTENT;
  10287. xmlParseElement(ctxt);
  10288. ctxt->instate = XML_PARSER_EPILOG;
  10289. /*
  10290. * The Misc part at the end
  10291. */
  10292. xmlParseMisc(ctxt);
  10293. if (ctxt->input->cur < ctxt->input->end) {
  10294. if (ctxt->errNo == XML_ERR_OK)
  10295. xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
  10296. } else if ((ctxt->input->buf != NULL) &&
  10297. (ctxt->input->buf->encoder != NULL) &&
  10298. (!xmlBufIsEmpty(ctxt->input->buf->raw))) {
  10299. xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
  10300. "Truncated multi-byte sequence at EOF\n");
  10301. }
  10302. ctxt->instate = XML_PARSER_EOF;
  10303. }
  10304. /*
  10305. * SAX: end of the document processing.
  10306. */
  10307. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  10308. ctxt->sax->endDocument(ctxt->userData);
  10309. /*
  10310. * Remove locally kept entity definitions if the tree was not built
  10311. */
  10312. if ((ctxt->myDoc != NULL) &&
  10313. (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
  10314. xmlFreeDoc(ctxt->myDoc);
  10315. ctxt->myDoc = NULL;
  10316. }
  10317. if ((ctxt->wellFormed) && (ctxt->myDoc != NULL)) {
  10318. ctxt->myDoc->properties |= XML_DOC_WELLFORMED;
  10319. if (ctxt->valid)
  10320. ctxt->myDoc->properties |= XML_DOC_DTDVALID;
  10321. if (ctxt->nsWellFormed)
  10322. ctxt->myDoc->properties |= XML_DOC_NSVALID;
  10323. if (ctxt->options & XML_PARSE_OLD10)
  10324. ctxt->myDoc->properties |= XML_DOC_OLD10;
  10325. }
  10326. if (! ctxt->wellFormed) {
  10327. ctxt->valid = 0;
  10328. return(-1);
  10329. }
  10330. return(0);
  10331. }
  10332. /**
  10333. * xmlParseExtParsedEnt:
  10334. * @ctxt: an XML parser context
  10335. *
  10336. * parse a general parsed entity
  10337. * An external general parsed entity is well-formed if it matches the
  10338. * production labeled extParsedEnt.
  10339. *
  10340. * [78] extParsedEnt ::= TextDecl? content
  10341. *
  10342. * Returns 0, -1 in case of error. the parser context is augmented
  10343. * as a result of the parsing.
  10344. */
  10345. int
  10346. xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) {
  10347. if ((ctxt == NULL) || (ctxt->input == NULL))
  10348. return(-1);
  10349. xmlDetectSAX2(ctxt);
  10350. /*
  10351. * SAX: beginning of the document processing.
  10352. */
  10353. if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
  10354. ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
  10355. xmlDetectEncoding(ctxt);
  10356. if (CUR == 0) {
  10357. xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
  10358. }
  10359. /*
  10360. * Check for the XMLDecl in the Prolog.
  10361. */
  10362. GROW;
  10363. if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
  10364. /*
  10365. * Note that we will switch encoding on the fly.
  10366. */
  10367. xmlParseXMLDecl(ctxt);
  10368. if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
  10369. /*
  10370. * The XML REC instructs us to stop parsing right here
  10371. */
  10372. return(-1);
  10373. }
  10374. SKIP_BLANKS;
  10375. } else {
  10376. ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
  10377. }
  10378. if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
  10379. ctxt->sax->startDocument(ctxt->userData);
  10380. if (ctxt->instate == XML_PARSER_EOF)
  10381. return(-1);
  10382. /*
  10383. * Doing validity checking on chunk doesn't make sense
  10384. */
  10385. ctxt->instate = XML_PARSER_CONTENT;
  10386. ctxt->validate = 0;
  10387. ctxt->loadsubset = 0;
  10388. ctxt->depth = 0;
  10389. xmlParseContent(ctxt);
  10390. if (ctxt->instate == XML_PARSER_EOF)
  10391. return(-1);
  10392. if ((RAW == '<') && (NXT(1) == '/')) {
  10393. xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
  10394. } else if (RAW != 0) {
  10395. xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
  10396. }
  10397. /*
  10398. * SAX: end of the document processing.
  10399. */
  10400. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  10401. ctxt->sax->endDocument(ctxt->userData);
  10402. if (! ctxt->wellFormed) return(-1);
  10403. return(0);
  10404. }
  10405. #ifdef LIBXML_PUSH_ENABLED
  10406. /************************************************************************
  10407. * *
  10408. * Progressive parsing interfaces *
  10409. * *
  10410. ************************************************************************/
  10411. /**
  10412. * xmlParseLookupChar:
  10413. * @ctxt: an XML parser context
  10414. * @c: character
  10415. *
  10416. * Check whether the input buffer contains a character.
  10417. */
  10418. static int
  10419. xmlParseLookupChar(xmlParserCtxtPtr ctxt, int c) {
  10420. const xmlChar *cur;
  10421. if (ctxt->checkIndex == 0) {
  10422. cur = ctxt->input->cur + 1;
  10423. } else {
  10424. cur = ctxt->input->cur + ctxt->checkIndex;
  10425. }
  10426. if (memchr(cur, c, ctxt->input->end - cur) == NULL) {
  10427. size_t index = ctxt->input->end - ctxt->input->cur;
  10428. if (index > LONG_MAX) {
  10429. ctxt->checkIndex = 0;
  10430. return(1);
  10431. }
  10432. ctxt->checkIndex = index;
  10433. return(0);
  10434. } else {
  10435. ctxt->checkIndex = 0;
  10436. return(1);
  10437. }
  10438. }
  10439. /**
  10440. * xmlParseLookupString:
  10441. * @ctxt: an XML parser context
  10442. * @startDelta: delta to apply at the start
  10443. * @str: string
  10444. * @strLen: length of string
  10445. *
  10446. * Check whether the input buffer contains a string.
  10447. */
  10448. static const xmlChar *
  10449. xmlParseLookupString(xmlParserCtxtPtr ctxt, size_t startDelta,
  10450. const char *str, size_t strLen) {
  10451. const xmlChar *cur, *term;
  10452. if (ctxt->checkIndex == 0) {
  10453. cur = ctxt->input->cur + startDelta;
  10454. } else {
  10455. cur = ctxt->input->cur + ctxt->checkIndex;
  10456. }
  10457. term = BAD_CAST strstr((const char *) cur, str);
  10458. if (term == NULL) {
  10459. const xmlChar *end = ctxt->input->end;
  10460. size_t index;
  10461. /* Rescan (strLen - 1) characters. */
  10462. if ((size_t) (end - cur) < strLen)
  10463. end = cur;
  10464. else
  10465. end -= strLen - 1;
  10466. index = end - ctxt->input->cur;
  10467. if (index > LONG_MAX) {
  10468. ctxt->checkIndex = 0;
  10469. return(ctxt->input->end - strLen);
  10470. }
  10471. ctxt->checkIndex = index;
  10472. } else {
  10473. ctxt->checkIndex = 0;
  10474. }
  10475. return(term);
  10476. }
  10477. /**
  10478. * xmlParseLookupCharData:
  10479. * @ctxt: an XML parser context
  10480. *
  10481. * Check whether the input buffer contains terminated char data.
  10482. */
  10483. static int
  10484. xmlParseLookupCharData(xmlParserCtxtPtr ctxt) {
  10485. const xmlChar *cur = ctxt->input->cur + ctxt->checkIndex;
  10486. const xmlChar *end = ctxt->input->end;
  10487. size_t index;
  10488. while (cur < end) {
  10489. if ((*cur == '<') || (*cur == '&')) {
  10490. ctxt->checkIndex = 0;
  10491. return(1);
  10492. }
  10493. cur++;
  10494. }
  10495. index = cur - ctxt->input->cur;
  10496. if (index > LONG_MAX) {
  10497. ctxt->checkIndex = 0;
  10498. return(1);
  10499. }
  10500. ctxt->checkIndex = index;
  10501. return(0);
  10502. }
  10503. /**
  10504. * xmlParseLookupGt:
  10505. * @ctxt: an XML parser context
  10506. *
  10507. * Check whether there's enough data in the input buffer to finish parsing
  10508. * a start tag. This has to take quotes into account.
  10509. */
  10510. static int
  10511. xmlParseLookupGt(xmlParserCtxtPtr ctxt) {
  10512. const xmlChar *cur;
  10513. const xmlChar *end = ctxt->input->end;
  10514. int state = ctxt->endCheckState;
  10515. size_t index;
  10516. if (ctxt->checkIndex == 0)
  10517. cur = ctxt->input->cur + 1;
  10518. else
  10519. cur = ctxt->input->cur + ctxt->checkIndex;
  10520. while (cur < end) {
  10521. if (state) {
  10522. if (*cur == state)
  10523. state = 0;
  10524. } else if (*cur == '\'' || *cur == '"') {
  10525. state = *cur;
  10526. } else if (*cur == '>') {
  10527. ctxt->checkIndex = 0;
  10528. ctxt->endCheckState = 0;
  10529. return(1);
  10530. }
  10531. cur++;
  10532. }
  10533. index = cur - ctxt->input->cur;
  10534. if (index > LONG_MAX) {
  10535. ctxt->checkIndex = 0;
  10536. ctxt->endCheckState = 0;
  10537. return(1);
  10538. }
  10539. ctxt->checkIndex = index;
  10540. ctxt->endCheckState = state;
  10541. return(0);
  10542. }
  10543. /**
  10544. * xmlParseLookupInternalSubset:
  10545. * @ctxt: an XML parser context
  10546. *
  10547. * Check whether there's enough data in the input buffer to finish parsing
  10548. * the internal subset.
  10549. */
  10550. static int
  10551. xmlParseLookupInternalSubset(xmlParserCtxtPtr ctxt) {
  10552. /*
  10553. * Sorry, but progressive parsing of the internal subset is not
  10554. * supported. We first check that the full content of the internal
  10555. * subset is available and parsing is launched only at that point.
  10556. * Internal subset ends with "']' S? '>'" in an unescaped section and
  10557. * not in a ']]>' sequence which are conditional sections.
  10558. */
  10559. const xmlChar *cur, *start;
  10560. const xmlChar *end = ctxt->input->end;
  10561. int state = ctxt->endCheckState;
  10562. size_t index;
  10563. if (ctxt->checkIndex == 0) {
  10564. cur = ctxt->input->cur + 1;
  10565. } else {
  10566. cur = ctxt->input->cur + ctxt->checkIndex;
  10567. }
  10568. start = cur;
  10569. while (cur < end) {
  10570. if (state == '-') {
  10571. if ((*cur == '-') &&
  10572. (cur[1] == '-') &&
  10573. (cur[2] == '>')) {
  10574. state = 0;
  10575. cur += 3;
  10576. start = cur;
  10577. continue;
  10578. }
  10579. }
  10580. else if (state == ']') {
  10581. if (*cur == '>') {
  10582. ctxt->checkIndex = 0;
  10583. ctxt->endCheckState = 0;
  10584. return(1);
  10585. }
  10586. if (IS_BLANK_CH(*cur)) {
  10587. state = ' ';
  10588. } else if (*cur != ']') {
  10589. state = 0;
  10590. start = cur;
  10591. continue;
  10592. }
  10593. }
  10594. else if (state == ' ') {
  10595. if (*cur == '>') {
  10596. ctxt->checkIndex = 0;
  10597. ctxt->endCheckState = 0;
  10598. return(1);
  10599. }
  10600. if (!IS_BLANK_CH(*cur)) {
  10601. state = 0;
  10602. start = cur;
  10603. continue;
  10604. }
  10605. }
  10606. else if (state != 0) {
  10607. if (*cur == state) {
  10608. state = 0;
  10609. start = cur + 1;
  10610. }
  10611. }
  10612. else if (*cur == '<') {
  10613. if ((cur[1] == '!') &&
  10614. (cur[2] == '-') &&
  10615. (cur[3] == '-')) {
  10616. state = '-';
  10617. cur += 4;
  10618. /* Don't treat <!--> as comment */
  10619. start = cur;
  10620. continue;
  10621. }
  10622. }
  10623. else if ((*cur == '"') || (*cur == '\'') || (*cur == ']')) {
  10624. state = *cur;
  10625. }
  10626. cur++;
  10627. }
  10628. /*
  10629. * Rescan the three last characters to detect "<!--" and "-->"
  10630. * split across chunks.
  10631. */
  10632. if ((state == 0) || (state == '-')) {
  10633. if (cur - start < 3)
  10634. cur = start;
  10635. else
  10636. cur -= 3;
  10637. }
  10638. index = cur - ctxt->input->cur;
  10639. if (index > LONG_MAX) {
  10640. ctxt->checkIndex = 0;
  10641. ctxt->endCheckState = 0;
  10642. return(1);
  10643. }
  10644. ctxt->checkIndex = index;
  10645. ctxt->endCheckState = state;
  10646. return(0);
  10647. }
  10648. /**
  10649. * xmlCheckCdataPush:
  10650. * @cur: pointer to the block of characters
  10651. * @len: length of the block in bytes
  10652. * @complete: 1 if complete CDATA block is passed in, 0 if partial block
  10653. *
  10654. * Check that the block of characters is okay as SCdata content [20]
  10655. *
  10656. * Returns the number of bytes to pass if okay, a negative index where an
  10657. * UTF-8 error occurred otherwise
  10658. */
  10659. static int
  10660. xmlCheckCdataPush(const xmlChar *utf, int len, int complete) {
  10661. int ix;
  10662. unsigned char c;
  10663. int codepoint;
  10664. if ((utf == NULL) || (len <= 0))
  10665. return(0);
  10666. for (ix = 0; ix < len;) { /* string is 0-terminated */
  10667. c = utf[ix];
  10668. if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */
  10669. if (c >= 0x20)
  10670. ix++;
  10671. else if ((c == 0xA) || (c == 0xD) || (c == 0x9))
  10672. ix++;
  10673. else
  10674. return(-ix);
  10675. } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */
  10676. if (ix + 2 > len) return(complete ? -ix : ix);
  10677. if ((utf[ix+1] & 0xc0 ) != 0x80)
  10678. return(-ix);
  10679. codepoint = (utf[ix] & 0x1f) << 6;
  10680. codepoint |= utf[ix+1] & 0x3f;
  10681. if (!xmlIsCharQ(codepoint))
  10682. return(-ix);
  10683. ix += 2;
  10684. } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */
  10685. if (ix + 3 > len) return(complete ? -ix : ix);
  10686. if (((utf[ix+1] & 0xc0) != 0x80) ||
  10687. ((utf[ix+2] & 0xc0) != 0x80))
  10688. return(-ix);
  10689. codepoint = (utf[ix] & 0xf) << 12;
  10690. codepoint |= (utf[ix+1] & 0x3f) << 6;
  10691. codepoint |= utf[ix+2] & 0x3f;
  10692. if (!xmlIsCharQ(codepoint))
  10693. return(-ix);
  10694. ix += 3;
  10695. } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */
  10696. if (ix + 4 > len) return(complete ? -ix : ix);
  10697. if (((utf[ix+1] & 0xc0) != 0x80) ||
  10698. ((utf[ix+2] & 0xc0) != 0x80) ||
  10699. ((utf[ix+3] & 0xc0) != 0x80))
  10700. return(-ix);
  10701. codepoint = (utf[ix] & 0x7) << 18;
  10702. codepoint |= (utf[ix+1] & 0x3f) << 12;
  10703. codepoint |= (utf[ix+2] & 0x3f) << 6;
  10704. codepoint |= utf[ix+3] & 0x3f;
  10705. if (!xmlIsCharQ(codepoint))
  10706. return(-ix);
  10707. ix += 4;
  10708. } else /* unknown encoding */
  10709. return(-ix);
  10710. }
  10711. return(ix);
  10712. }
  10713. /**
  10714. * xmlParseTryOrFinish:
  10715. * @ctxt: an XML parser context
  10716. * @terminate: last chunk indicator
  10717. *
  10718. * Try to progress on parsing
  10719. *
  10720. * Returns zero if no parsing was possible
  10721. */
  10722. static int
  10723. xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
  10724. int ret = 0;
  10725. size_t avail;
  10726. xmlChar cur, next;
  10727. if (ctxt->input == NULL)
  10728. return(0);
  10729. if ((ctxt->input != NULL) &&
  10730. (ctxt->input->cur - ctxt->input->base > 4096)) {
  10731. xmlParserShrink(ctxt);
  10732. }
  10733. while (ctxt->instate != XML_PARSER_EOF) {
  10734. if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
  10735. return(0);
  10736. avail = ctxt->input->end - ctxt->input->cur;
  10737. if (avail < 1)
  10738. goto done;
  10739. switch (ctxt->instate) {
  10740. case XML_PARSER_EOF:
  10741. /*
  10742. * Document parsing is done !
  10743. */
  10744. goto done;
  10745. case XML_PARSER_START:
  10746. /*
  10747. * Very first chars read from the document flow.
  10748. */
  10749. if ((!terminate) && (avail < 4))
  10750. goto done;
  10751. /*
  10752. * We need more bytes to detect EBCDIC code pages.
  10753. * See xmlDetectEBCDIC.
  10754. */
  10755. if ((CMP4(CUR_PTR, 0x4C, 0x6F, 0xA7, 0x94)) &&
  10756. (!terminate) && (avail < 200))
  10757. goto done;
  10758. xmlDetectEncoding(ctxt);
  10759. if (ctxt->instate == XML_PARSER_EOF)
  10760. goto done;
  10761. ctxt->instate = XML_PARSER_XML_DECL;
  10762. break;
  10763. case XML_PARSER_XML_DECL:
  10764. if ((!terminate) && (avail < 2))
  10765. goto done;
  10766. cur = ctxt->input->cur[0];
  10767. next = ctxt->input->cur[1];
  10768. if ((cur == '<') && (next == '?')) {
  10769. /* PI or XML decl */
  10770. if ((!terminate) &&
  10771. (!xmlParseLookupString(ctxt, 2, "?>", 2)))
  10772. goto done;
  10773. if ((ctxt->input->cur[2] == 'x') &&
  10774. (ctxt->input->cur[3] == 'm') &&
  10775. (ctxt->input->cur[4] == 'l') &&
  10776. (IS_BLANK_CH(ctxt->input->cur[5]))) {
  10777. ret += 5;
  10778. xmlParseXMLDecl(ctxt);
  10779. if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
  10780. /*
  10781. * The XML REC instructs us to stop parsing right
  10782. * here
  10783. */
  10784. xmlHaltParser(ctxt);
  10785. return(0);
  10786. }
  10787. } else {
  10788. ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
  10789. }
  10790. } else {
  10791. ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
  10792. if (ctxt->version == NULL) {
  10793. xmlErrMemory(ctxt, NULL);
  10794. break;
  10795. }
  10796. }
  10797. if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
  10798. ctxt->sax->setDocumentLocator(ctxt->userData,
  10799. &xmlDefaultSAXLocator);
  10800. if ((ctxt->sax) && (ctxt->sax->startDocument) &&
  10801. (!ctxt->disableSAX))
  10802. ctxt->sax->startDocument(ctxt->userData);
  10803. if (ctxt->instate == XML_PARSER_EOF)
  10804. goto done;
  10805. ctxt->instate = XML_PARSER_MISC;
  10806. break;
  10807. case XML_PARSER_START_TAG: {
  10808. const xmlChar *name;
  10809. const xmlChar *prefix = NULL;
  10810. const xmlChar *URI = NULL;
  10811. int line = ctxt->input->line;
  10812. int nbNs = 0;
  10813. if ((!terminate) && (avail < 2))
  10814. goto done;
  10815. cur = ctxt->input->cur[0];
  10816. if (cur != '<') {
  10817. xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
  10818. "Start tag expected, '<' not found");
  10819. xmlHaltParser(ctxt);
  10820. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  10821. ctxt->sax->endDocument(ctxt->userData);
  10822. goto done;
  10823. }
  10824. if ((!terminate) && (!xmlParseLookupGt(ctxt)))
  10825. goto done;
  10826. if (ctxt->spaceNr == 0)
  10827. spacePush(ctxt, -1);
  10828. else if (*ctxt->space == -2)
  10829. spacePush(ctxt, -1);
  10830. else
  10831. spacePush(ctxt, *ctxt->space);
  10832. #ifdef LIBXML_SAX1_ENABLED
  10833. if (ctxt->sax2)
  10834. #endif /* LIBXML_SAX1_ENABLED */
  10835. name = xmlParseStartTag2(ctxt, &prefix, &URI, &nbNs);
  10836. #ifdef LIBXML_SAX1_ENABLED
  10837. else
  10838. name = xmlParseStartTag(ctxt);
  10839. #endif /* LIBXML_SAX1_ENABLED */
  10840. if (ctxt->instate == XML_PARSER_EOF)
  10841. goto done;
  10842. if (name == NULL) {
  10843. spacePop(ctxt);
  10844. xmlHaltParser(ctxt);
  10845. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  10846. ctxt->sax->endDocument(ctxt->userData);
  10847. goto done;
  10848. }
  10849. #ifdef LIBXML_VALID_ENABLED
  10850. /*
  10851. * [ VC: Root Element Type ]
  10852. * The Name in the document type declaration must match
  10853. * the element type of the root element.
  10854. */
  10855. if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
  10856. ctxt->node && (ctxt->node == ctxt->myDoc->children))
  10857. ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
  10858. #endif /* LIBXML_VALID_ENABLED */
  10859. /*
  10860. * Check for an Empty Element.
  10861. */
  10862. if ((RAW == '/') && (NXT(1) == '>')) {
  10863. SKIP(2);
  10864. if (ctxt->sax2) {
  10865. if ((ctxt->sax != NULL) &&
  10866. (ctxt->sax->endElementNs != NULL) &&
  10867. (!ctxt->disableSAX))
  10868. ctxt->sax->endElementNs(ctxt->userData, name,
  10869. prefix, URI);
  10870. if (nbNs > 0)
  10871. xmlParserNsPop(ctxt, nbNs);
  10872. #ifdef LIBXML_SAX1_ENABLED
  10873. } else {
  10874. if ((ctxt->sax != NULL) &&
  10875. (ctxt->sax->endElement != NULL) &&
  10876. (!ctxt->disableSAX))
  10877. ctxt->sax->endElement(ctxt->userData, name);
  10878. #endif /* LIBXML_SAX1_ENABLED */
  10879. }
  10880. spacePop(ctxt);
  10881. } else if (RAW == '>') {
  10882. NEXT;
  10883. nameNsPush(ctxt, name, prefix, URI, line, nbNs);
  10884. } else {
  10885. xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED,
  10886. "Couldn't find end of Start Tag %s\n",
  10887. name);
  10888. nodePop(ctxt);
  10889. spacePop(ctxt);
  10890. if (nbNs > 0)
  10891. xmlParserNsPop(ctxt, nbNs);
  10892. }
  10893. if (ctxt->instate == XML_PARSER_EOF)
  10894. goto done;
  10895. if (ctxt->nameNr == 0)
  10896. ctxt->instate = XML_PARSER_EPILOG;
  10897. else
  10898. ctxt->instate = XML_PARSER_CONTENT;
  10899. break;
  10900. }
  10901. case XML_PARSER_CONTENT: {
  10902. cur = ctxt->input->cur[0];
  10903. if (cur == '<') {
  10904. if ((!terminate) && (avail < 2))
  10905. goto done;
  10906. next = ctxt->input->cur[1];
  10907. if (next == '/') {
  10908. ctxt->instate = XML_PARSER_END_TAG;
  10909. break;
  10910. } else if (next == '?') {
  10911. if ((!terminate) &&
  10912. (!xmlParseLookupString(ctxt, 2, "?>", 2)))
  10913. goto done;
  10914. xmlParsePI(ctxt);
  10915. if (ctxt->instate == XML_PARSER_EOF)
  10916. goto done;
  10917. ctxt->instate = XML_PARSER_CONTENT;
  10918. break;
  10919. } else if (next == '!') {
  10920. if ((!terminate) && (avail < 3))
  10921. goto done;
  10922. next = ctxt->input->cur[2];
  10923. if (next == '-') {
  10924. if ((!terminate) && (avail < 4))
  10925. goto done;
  10926. if (ctxt->input->cur[3] == '-') {
  10927. if ((!terminate) &&
  10928. (!xmlParseLookupString(ctxt, 4, "-->", 3)))
  10929. goto done;
  10930. xmlParseComment(ctxt);
  10931. if (ctxt->instate == XML_PARSER_EOF)
  10932. goto done;
  10933. ctxt->instate = XML_PARSER_CONTENT;
  10934. break;
  10935. }
  10936. } else if (next == '[') {
  10937. if ((!terminate) && (avail < 9))
  10938. goto done;
  10939. if ((ctxt->input->cur[2] == '[') &&
  10940. (ctxt->input->cur[3] == 'C') &&
  10941. (ctxt->input->cur[4] == 'D') &&
  10942. (ctxt->input->cur[5] == 'A') &&
  10943. (ctxt->input->cur[6] == 'T') &&
  10944. (ctxt->input->cur[7] == 'A') &&
  10945. (ctxt->input->cur[8] == '[')) {
  10946. SKIP(9);
  10947. ctxt->instate = XML_PARSER_CDATA_SECTION;
  10948. break;
  10949. }
  10950. }
  10951. }
  10952. } else if (cur == '&') {
  10953. if ((!terminate) && (!xmlParseLookupChar(ctxt, ';')))
  10954. goto done;
  10955. xmlParseReference(ctxt);
  10956. break;
  10957. } else {
  10958. /* TODO Avoid the extra copy, handle directly !!! */
  10959. /*
  10960. * Goal of the following test is:
  10961. * - minimize calls to the SAX 'character' callback
  10962. * when they are mergeable
  10963. * - handle an problem for isBlank when we only parse
  10964. * a sequence of blank chars and the next one is
  10965. * not available to check against '<' presence.
  10966. * - tries to homogenize the differences in SAX
  10967. * callbacks between the push and pull versions
  10968. * of the parser.
  10969. */
  10970. if (avail < XML_PARSER_BIG_BUFFER_SIZE) {
  10971. if ((!terminate) && (!xmlParseLookupCharData(ctxt)))
  10972. goto done;
  10973. }
  10974. ctxt->checkIndex = 0;
  10975. xmlParseCharDataInternal(ctxt, !terminate);
  10976. break;
  10977. }
  10978. ctxt->instate = XML_PARSER_START_TAG;
  10979. break;
  10980. }
  10981. case XML_PARSER_END_TAG:
  10982. if ((!terminate) && (!xmlParseLookupChar(ctxt, '>')))
  10983. goto done;
  10984. if (ctxt->sax2) {
  10985. xmlParseEndTag2(ctxt, &ctxt->pushTab[ctxt->nameNr - 1]);
  10986. nameNsPop(ctxt);
  10987. }
  10988. #ifdef LIBXML_SAX1_ENABLED
  10989. else
  10990. xmlParseEndTag1(ctxt, 0);
  10991. #endif /* LIBXML_SAX1_ENABLED */
  10992. if (ctxt->instate == XML_PARSER_EOF)
  10993. goto done;
  10994. if (ctxt->nameNr == 0) {
  10995. ctxt->instate = XML_PARSER_EPILOG;
  10996. } else {
  10997. ctxt->instate = XML_PARSER_CONTENT;
  10998. }
  10999. break;
  11000. case XML_PARSER_CDATA_SECTION: {
  11001. /*
  11002. * The Push mode need to have the SAX callback for
  11003. * cdataBlock merge back contiguous callbacks.
  11004. */
  11005. const xmlChar *term;
  11006. if (terminate) {
  11007. /*
  11008. * Don't call xmlParseLookupString. If 'terminate'
  11009. * is set, checkIndex is invalid.
  11010. */
  11011. term = BAD_CAST strstr((const char *) ctxt->input->cur,
  11012. "]]>");
  11013. } else {
  11014. term = xmlParseLookupString(ctxt, 0, "]]>", 3);
  11015. }
  11016. if (term == NULL) {
  11017. int tmp, size;
  11018. if (terminate) {
  11019. /* Unfinished CDATA section */
  11020. size = ctxt->input->end - ctxt->input->cur;
  11021. } else {
  11022. if (avail < XML_PARSER_BIG_BUFFER_SIZE + 2)
  11023. goto done;
  11024. ctxt->checkIndex = 0;
  11025. /* XXX: Why don't we pass the full buffer? */
  11026. size = XML_PARSER_BIG_BUFFER_SIZE;
  11027. }
  11028. tmp = xmlCheckCdataPush(ctxt->input->cur, size, 0);
  11029. if (tmp <= 0) {
  11030. tmp = -tmp;
  11031. ctxt->input->cur += tmp;
  11032. goto encoding_error;
  11033. }
  11034. if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
  11035. if (ctxt->sax->cdataBlock != NULL)
  11036. ctxt->sax->cdataBlock(ctxt->userData,
  11037. ctxt->input->cur, tmp);
  11038. else if (ctxt->sax->characters != NULL)
  11039. ctxt->sax->characters(ctxt->userData,
  11040. ctxt->input->cur, tmp);
  11041. }
  11042. if (ctxt->instate == XML_PARSER_EOF)
  11043. goto done;
  11044. SKIPL(tmp);
  11045. } else {
  11046. int base = term - CUR_PTR;
  11047. int tmp;
  11048. tmp = xmlCheckCdataPush(ctxt->input->cur, base, 1);
  11049. if ((tmp < 0) || (tmp != base)) {
  11050. tmp = -tmp;
  11051. ctxt->input->cur += tmp;
  11052. goto encoding_error;
  11053. }
  11054. if ((ctxt->sax != NULL) && (base == 0) &&
  11055. (ctxt->sax->cdataBlock != NULL) &&
  11056. (!ctxt->disableSAX)) {
  11057. /*
  11058. * Special case to provide identical behaviour
  11059. * between pull and push parsers on enpty CDATA
  11060. * sections
  11061. */
  11062. if ((ctxt->input->cur - ctxt->input->base >= 9) &&
  11063. (!strncmp((const char *)&ctxt->input->cur[-9],
  11064. "<![CDATA[", 9)))
  11065. ctxt->sax->cdataBlock(ctxt->userData,
  11066. BAD_CAST "", 0);
  11067. } else if ((ctxt->sax != NULL) && (base > 0) &&
  11068. (!ctxt->disableSAX)) {
  11069. if (ctxt->sax->cdataBlock != NULL)
  11070. ctxt->sax->cdataBlock(ctxt->userData,
  11071. ctxt->input->cur, base);
  11072. else if (ctxt->sax->characters != NULL)
  11073. ctxt->sax->characters(ctxt->userData,
  11074. ctxt->input->cur, base);
  11075. }
  11076. if (ctxt->instate == XML_PARSER_EOF)
  11077. goto done;
  11078. SKIPL(base + 3);
  11079. ctxt->instate = XML_PARSER_CONTENT;
  11080. }
  11081. break;
  11082. }
  11083. case XML_PARSER_MISC:
  11084. case XML_PARSER_PROLOG:
  11085. case XML_PARSER_EPILOG:
  11086. SKIP_BLANKS;
  11087. avail = ctxt->input->end - ctxt->input->cur;
  11088. if (avail < 1)
  11089. goto done;
  11090. if (ctxt->input->cur[0] == '<') {
  11091. if ((!terminate) && (avail < 2))
  11092. goto done;
  11093. next = ctxt->input->cur[1];
  11094. if (next == '?') {
  11095. if ((!terminate) &&
  11096. (!xmlParseLookupString(ctxt, 2, "?>", 2)))
  11097. goto done;
  11098. xmlParsePI(ctxt);
  11099. if (ctxt->instate == XML_PARSER_EOF)
  11100. goto done;
  11101. break;
  11102. } else if (next == '!') {
  11103. if ((!terminate) && (avail < 3))
  11104. goto done;
  11105. if (ctxt->input->cur[2] == '-') {
  11106. if ((!terminate) && (avail < 4))
  11107. goto done;
  11108. if (ctxt->input->cur[3] == '-') {
  11109. if ((!terminate) &&
  11110. (!xmlParseLookupString(ctxt, 4, "-->", 3)))
  11111. goto done;
  11112. xmlParseComment(ctxt);
  11113. if (ctxt->instate == XML_PARSER_EOF)
  11114. goto done;
  11115. break;
  11116. }
  11117. } else if (ctxt->instate == XML_PARSER_MISC) {
  11118. if ((!terminate) && (avail < 9))
  11119. goto done;
  11120. if ((ctxt->input->cur[2] == 'D') &&
  11121. (ctxt->input->cur[3] == 'O') &&
  11122. (ctxt->input->cur[4] == 'C') &&
  11123. (ctxt->input->cur[5] == 'T') &&
  11124. (ctxt->input->cur[6] == 'Y') &&
  11125. (ctxt->input->cur[7] == 'P') &&
  11126. (ctxt->input->cur[8] == 'E')) {
  11127. if ((!terminate) && (!xmlParseLookupGt(ctxt)))
  11128. goto done;
  11129. ctxt->inSubset = 1;
  11130. xmlParseDocTypeDecl(ctxt);
  11131. if (ctxt->instate == XML_PARSER_EOF)
  11132. goto done;
  11133. if (RAW == '[') {
  11134. ctxt->instate = XML_PARSER_DTD;
  11135. } else {
  11136. /*
  11137. * Create and update the external subset.
  11138. */
  11139. ctxt->inSubset = 2;
  11140. if ((ctxt->sax != NULL) &&
  11141. (!ctxt->disableSAX) &&
  11142. (ctxt->sax->externalSubset != NULL))
  11143. ctxt->sax->externalSubset(
  11144. ctxt->userData,
  11145. ctxt->intSubName,
  11146. ctxt->extSubSystem,
  11147. ctxt->extSubURI);
  11148. ctxt->inSubset = 0;
  11149. xmlCleanSpecialAttr(ctxt);
  11150. if (ctxt->instate == XML_PARSER_EOF)
  11151. goto done;
  11152. ctxt->instate = XML_PARSER_PROLOG;
  11153. }
  11154. break;
  11155. }
  11156. }
  11157. }
  11158. }
  11159. if (ctxt->instate == XML_PARSER_EPILOG) {
  11160. if (ctxt->errNo == XML_ERR_OK)
  11161. xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
  11162. ctxt->instate = XML_PARSER_EOF;
  11163. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  11164. ctxt->sax->endDocument(ctxt->userData);
  11165. } else {
  11166. ctxt->instate = XML_PARSER_START_TAG;
  11167. }
  11168. break;
  11169. case XML_PARSER_DTD: {
  11170. if ((!terminate) && (!xmlParseLookupInternalSubset(ctxt)))
  11171. goto done;
  11172. xmlParseInternalSubset(ctxt);
  11173. if (ctxt->instate == XML_PARSER_EOF)
  11174. goto done;
  11175. ctxt->inSubset = 2;
  11176. if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
  11177. (ctxt->sax->externalSubset != NULL))
  11178. ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
  11179. ctxt->extSubSystem, ctxt->extSubURI);
  11180. ctxt->inSubset = 0;
  11181. xmlCleanSpecialAttr(ctxt);
  11182. if (ctxt->instate == XML_PARSER_EOF)
  11183. goto done;
  11184. ctxt->instate = XML_PARSER_PROLOG;
  11185. break;
  11186. }
  11187. default:
  11188. xmlGenericError(xmlGenericErrorContext,
  11189. "PP: internal error\n");
  11190. ctxt->instate = XML_PARSER_EOF;
  11191. break;
  11192. }
  11193. }
  11194. done:
  11195. return(ret);
  11196. encoding_error:
  11197. if (ctxt->input->end - ctxt->input->cur < 4) {
  11198. __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
  11199. "Input is not proper UTF-8, indicate encoding !\n",
  11200. NULL, NULL);
  11201. } else {
  11202. char buffer[150];
  11203. snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
  11204. ctxt->input->cur[0], ctxt->input->cur[1],
  11205. ctxt->input->cur[2], ctxt->input->cur[3]);
  11206. __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
  11207. "Input is not proper UTF-8, indicate encoding !\n%s",
  11208. BAD_CAST buffer, NULL);
  11209. }
  11210. return(0);
  11211. }
  11212. /**
  11213. * xmlParseChunk:
  11214. * @ctxt: an XML parser context
  11215. * @chunk: an char array
  11216. * @size: the size in byte of the chunk
  11217. * @terminate: last chunk indicator
  11218. *
  11219. * Parse a Chunk of memory
  11220. *
  11221. * Returns zero if no error, the xmlParserErrors otherwise.
  11222. */
  11223. int
  11224. xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size,
  11225. int terminate) {
  11226. int end_in_lf = 0;
  11227. if (ctxt == NULL)
  11228. return(XML_ERR_INTERNAL_ERROR);
  11229. if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
  11230. return(ctxt->errNo);
  11231. if (ctxt->instate == XML_PARSER_EOF)
  11232. return(-1);
  11233. if (ctxt->input == NULL)
  11234. return(-1);
  11235. ctxt->progressive = 1;
  11236. if (ctxt->instate == XML_PARSER_START)
  11237. xmlDetectSAX2(ctxt);
  11238. if ((size > 0) && (chunk != NULL) && (!terminate) &&
  11239. (chunk[size - 1] == '\r')) {
  11240. end_in_lf = 1;
  11241. size--;
  11242. }
  11243. if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
  11244. (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
  11245. size_t pos = ctxt->input->cur - ctxt->input->base;
  11246. int res;
  11247. res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
  11248. xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
  11249. if (res < 0) {
  11250. xmlFatalErr(ctxt, ctxt->input->buf->error, NULL);
  11251. xmlHaltParser(ctxt);
  11252. return(ctxt->errNo);
  11253. }
  11254. }
  11255. xmlParseTryOrFinish(ctxt, terminate);
  11256. if (ctxt->instate == XML_PARSER_EOF)
  11257. return(ctxt->errNo);
  11258. if ((ctxt->input != NULL) &&
  11259. (((ctxt->input->end - ctxt->input->cur) > XML_MAX_LOOKUP_LIMIT) ||
  11260. ((ctxt->input->cur - ctxt->input->base) > XML_MAX_LOOKUP_LIMIT)) &&
  11261. ((ctxt->options & XML_PARSE_HUGE) == 0)) {
  11262. xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup");
  11263. xmlHaltParser(ctxt);
  11264. }
  11265. if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
  11266. return(ctxt->errNo);
  11267. if ((end_in_lf == 1) && (ctxt->input != NULL) &&
  11268. (ctxt->input->buf != NULL)) {
  11269. size_t pos = ctxt->input->cur - ctxt->input->base;
  11270. int res;
  11271. res = xmlParserInputBufferPush(ctxt->input->buf, 1, "\r");
  11272. xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
  11273. if (res < 0) {
  11274. xmlFatalErr(ctxt, ctxt->input->buf->error, NULL);
  11275. xmlHaltParser(ctxt);
  11276. return(ctxt->errNo);
  11277. }
  11278. }
  11279. if (terminate) {
  11280. /*
  11281. * Check for termination
  11282. */
  11283. if ((ctxt->instate != XML_PARSER_EOF) &&
  11284. (ctxt->instate != XML_PARSER_EPILOG)) {
  11285. if (ctxt->nameNr > 0) {
  11286. const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
  11287. int line = ctxt->pushTab[ctxt->nameNr - 1].line;
  11288. xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
  11289. "Premature end of data in tag %s line %d\n",
  11290. name, line, NULL);
  11291. } else if (ctxt->instate == XML_PARSER_START) {
  11292. xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
  11293. } else {
  11294. xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
  11295. "Start tag expected, '<' not found\n");
  11296. }
  11297. } else if ((ctxt->input->buf != NULL) &&
  11298. (ctxt->input->buf->encoder != NULL) &&
  11299. (!xmlBufIsEmpty(ctxt->input->buf->raw))) {
  11300. xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
  11301. "Truncated multi-byte sequence at EOF\n");
  11302. }
  11303. if (ctxt->instate != XML_PARSER_EOF) {
  11304. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  11305. ctxt->sax->endDocument(ctxt->userData);
  11306. }
  11307. ctxt->instate = XML_PARSER_EOF;
  11308. }
  11309. if (ctxt->wellFormed == 0)
  11310. return((xmlParserErrors) ctxt->errNo);
  11311. else
  11312. return(0);
  11313. }
  11314. /************************************************************************
  11315. * *
  11316. * I/O front end functions to the parser *
  11317. * *
  11318. ************************************************************************/
  11319. /**
  11320. * xmlCreatePushParserCtxt:
  11321. * @sax: a SAX handler
  11322. * @user_data: The user data returned on SAX callbacks
  11323. * @chunk: a pointer to an array of chars
  11324. * @size: number of chars in the array
  11325. * @filename: an optional file name or URI
  11326. *
  11327. * Create a parser context for using the XML parser in push mode.
  11328. * If @buffer and @size are non-NULL, the data is used to detect
  11329. * the encoding. The remaining characters will be parsed so they
  11330. * don't need to be fed in again through xmlParseChunk.
  11331. * To allow content encoding detection, @size should be >= 4
  11332. * The value of @filename is used for fetching external entities
  11333. * and error/warning reports.
  11334. *
  11335. * Returns the new parser context or NULL
  11336. */
  11337. xmlParserCtxtPtr
  11338. xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
  11339. const char *chunk, int size, const char *filename) {
  11340. xmlParserCtxtPtr ctxt;
  11341. xmlParserInputPtr inputStream;
  11342. xmlParserInputBufferPtr buf;
  11343. buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
  11344. if (buf == NULL) return(NULL);
  11345. ctxt = xmlNewSAXParserCtxt(sax, user_data);
  11346. if (ctxt == NULL) {
  11347. xmlErrMemory(NULL, "creating parser: out of memory\n");
  11348. xmlFreeParserInputBuffer(buf);
  11349. return(NULL);
  11350. }
  11351. ctxt->dictNames = 1;
  11352. if (filename == NULL) {
  11353. ctxt->directory = NULL;
  11354. } else {
  11355. ctxt->directory = xmlParserGetDirectory(filename);
  11356. }
  11357. inputStream = xmlNewInputStream(ctxt);
  11358. if (inputStream == NULL) {
  11359. xmlFreeParserCtxt(ctxt);
  11360. xmlFreeParserInputBuffer(buf);
  11361. return(NULL);
  11362. }
  11363. if (filename == NULL)
  11364. inputStream->filename = NULL;
  11365. else {
  11366. inputStream->filename = (char *)
  11367. xmlCanonicPath((const xmlChar *) filename);
  11368. if (inputStream->filename == NULL) {
  11369. xmlFreeInputStream(inputStream);
  11370. xmlFreeParserCtxt(ctxt);
  11371. xmlFreeParserInputBuffer(buf);
  11372. return(NULL);
  11373. }
  11374. }
  11375. inputStream->buf = buf;
  11376. xmlBufResetInput(inputStream->buf->buffer, inputStream);
  11377. inputPush(ctxt, inputStream);
  11378. if ((size != 0) && (chunk != NULL) &&
  11379. (ctxt->input != NULL) && (ctxt->input->buf != NULL)) {
  11380. size_t pos = ctxt->input->cur - ctxt->input->base;
  11381. int res;
  11382. res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
  11383. xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
  11384. if (res < 0) {
  11385. xmlFatalErr(ctxt, ctxt->input->buf->error, NULL);
  11386. xmlHaltParser(ctxt);
  11387. }
  11388. }
  11389. return(ctxt);
  11390. }
  11391. #endif /* LIBXML_PUSH_ENABLED */
  11392. /**
  11393. * xmlStopParser:
  11394. * @ctxt: an XML parser context
  11395. *
  11396. * Blocks further parser processing
  11397. */
  11398. void
  11399. xmlStopParser(xmlParserCtxtPtr ctxt) {
  11400. if (ctxt == NULL)
  11401. return;
  11402. xmlHaltParser(ctxt);
  11403. ctxt->errNo = XML_ERR_USER_STOP;
  11404. }
  11405. /**
  11406. * xmlCreateIOParserCtxt:
  11407. * @sax: a SAX handler
  11408. * @user_data: The user data returned on SAX callbacks
  11409. * @ioread: an I/O read function
  11410. * @ioclose: an I/O close function
  11411. * @ioctx: an I/O handler
  11412. * @enc: the charset encoding if known
  11413. *
  11414. * Create a parser context for using the XML parser with an existing
  11415. * I/O stream
  11416. *
  11417. * Returns the new parser context or NULL
  11418. */
  11419. xmlParserCtxtPtr
  11420. xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
  11421. xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
  11422. void *ioctx, xmlCharEncoding enc) {
  11423. xmlParserCtxtPtr ctxt;
  11424. xmlParserInputPtr inputStream;
  11425. xmlParserInputBufferPtr buf;
  11426. if (ioread == NULL) return(NULL);
  11427. buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc);
  11428. if (buf == NULL) {
  11429. if (ioclose != NULL)
  11430. ioclose(ioctx);
  11431. return (NULL);
  11432. }
  11433. ctxt = xmlNewSAXParserCtxt(sax, user_data);
  11434. if (ctxt == NULL) {
  11435. xmlFreeParserInputBuffer(buf);
  11436. return(NULL);
  11437. }
  11438. inputStream = xmlNewIOInputStream(ctxt, buf, enc);
  11439. if (inputStream == NULL) {
  11440. xmlFreeParserCtxt(ctxt);
  11441. return(NULL);
  11442. }
  11443. inputPush(ctxt, inputStream);
  11444. return(ctxt);
  11445. }
  11446. #ifdef LIBXML_VALID_ENABLED
  11447. /************************************************************************
  11448. * *
  11449. * Front ends when parsing a DTD *
  11450. * *
  11451. ************************************************************************/
  11452. /**
  11453. * xmlIOParseDTD:
  11454. * @sax: the SAX handler block or NULL
  11455. * @input: an Input Buffer
  11456. * @enc: the charset encoding if known
  11457. *
  11458. * Load and parse a DTD
  11459. *
  11460. * Returns the resulting xmlDtdPtr or NULL in case of error.
  11461. * @input will be freed by the function in any case.
  11462. */
  11463. xmlDtdPtr
  11464. xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
  11465. xmlCharEncoding enc) {
  11466. xmlDtdPtr ret = NULL;
  11467. xmlParserCtxtPtr ctxt;
  11468. xmlParserInputPtr pinput = NULL;
  11469. if (input == NULL)
  11470. return(NULL);
  11471. ctxt = xmlNewSAXParserCtxt(sax, NULL);
  11472. if (ctxt == NULL) {
  11473. xmlFreeParserInputBuffer(input);
  11474. return(NULL);
  11475. }
  11476. /* We are loading a DTD */
  11477. ctxt->options |= XML_PARSE_DTDLOAD;
  11478. xmlDetectSAX2(ctxt);
  11479. /*
  11480. * generate a parser input from the I/O handler
  11481. */
  11482. pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  11483. if (pinput == NULL) {
  11484. xmlFreeParserInputBuffer(input);
  11485. xmlFreeParserCtxt(ctxt);
  11486. return(NULL);
  11487. }
  11488. /*
  11489. * plug some encoding conversion routines here.
  11490. */
  11491. if (xmlPushInput(ctxt, pinput) < 0) {
  11492. xmlFreeParserCtxt(ctxt);
  11493. return(NULL);
  11494. }
  11495. if (enc != XML_CHAR_ENCODING_NONE) {
  11496. xmlSwitchEncoding(ctxt, enc);
  11497. }
  11498. /*
  11499. * let's parse that entity knowing it's an external subset.
  11500. */
  11501. ctxt->inSubset = 2;
  11502. ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
  11503. if (ctxt->myDoc == NULL) {
  11504. xmlErrMemory(ctxt, "New Doc failed");
  11505. return(NULL);
  11506. }
  11507. ctxt->myDoc->properties = XML_DOC_INTERNAL;
  11508. ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
  11509. BAD_CAST "none", BAD_CAST "none");
  11510. xmlDetectEncoding(ctxt);
  11511. xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none");
  11512. if (ctxt->myDoc != NULL) {
  11513. if (ctxt->wellFormed) {
  11514. ret = ctxt->myDoc->extSubset;
  11515. ctxt->myDoc->extSubset = NULL;
  11516. if (ret != NULL) {
  11517. xmlNodePtr tmp;
  11518. ret->doc = NULL;
  11519. tmp = ret->children;
  11520. while (tmp != NULL) {
  11521. tmp->doc = NULL;
  11522. tmp = tmp->next;
  11523. }
  11524. }
  11525. } else {
  11526. ret = NULL;
  11527. }
  11528. xmlFreeDoc(ctxt->myDoc);
  11529. ctxt->myDoc = NULL;
  11530. }
  11531. xmlFreeParserCtxt(ctxt);
  11532. return(ret);
  11533. }
  11534. /**
  11535. * xmlSAXParseDTD:
  11536. * @sax: the SAX handler block
  11537. * @ExternalID: a NAME* containing the External ID of the DTD
  11538. * @SystemID: a NAME* containing the URL to the DTD
  11539. *
  11540. * DEPRECATED: Don't use.
  11541. *
  11542. * Load and parse an external subset.
  11543. *
  11544. * Returns the resulting xmlDtdPtr or NULL in case of error.
  11545. */
  11546. xmlDtdPtr
  11547. xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
  11548. const xmlChar *SystemID) {
  11549. xmlDtdPtr ret = NULL;
  11550. xmlParserCtxtPtr ctxt;
  11551. xmlParserInputPtr input = NULL;
  11552. xmlChar* systemIdCanonic;
  11553. if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL);
  11554. ctxt = xmlNewSAXParserCtxt(sax, NULL);
  11555. if (ctxt == NULL) {
  11556. return(NULL);
  11557. }
  11558. /* We are loading a DTD */
  11559. ctxt->options |= XML_PARSE_DTDLOAD;
  11560. /*
  11561. * Canonicalise the system ID
  11562. */
  11563. systemIdCanonic = xmlCanonicPath(SystemID);
  11564. if ((SystemID != NULL) && (systemIdCanonic == NULL)) {
  11565. xmlFreeParserCtxt(ctxt);
  11566. return(NULL);
  11567. }
  11568. /*
  11569. * Ask the Entity resolver to load the damn thing
  11570. */
  11571. if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
  11572. input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
  11573. systemIdCanonic);
  11574. if (input == NULL) {
  11575. xmlFreeParserCtxt(ctxt);
  11576. if (systemIdCanonic != NULL)
  11577. xmlFree(systemIdCanonic);
  11578. return(NULL);
  11579. }
  11580. /*
  11581. * plug some encoding conversion routines here.
  11582. */
  11583. if (xmlPushInput(ctxt, input) < 0) {
  11584. xmlFreeParserCtxt(ctxt);
  11585. if (systemIdCanonic != NULL)
  11586. xmlFree(systemIdCanonic);
  11587. return(NULL);
  11588. }
  11589. xmlDetectEncoding(ctxt);
  11590. if (input->filename == NULL)
  11591. input->filename = (char *) systemIdCanonic;
  11592. else
  11593. xmlFree(systemIdCanonic);
  11594. /*
  11595. * let's parse that entity knowing it's an external subset.
  11596. */
  11597. ctxt->inSubset = 2;
  11598. ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
  11599. if (ctxt->myDoc == NULL) {
  11600. xmlErrMemory(ctxt, "New Doc failed");
  11601. xmlFreeParserCtxt(ctxt);
  11602. return(NULL);
  11603. }
  11604. ctxt->myDoc->properties = XML_DOC_INTERNAL;
  11605. ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
  11606. ExternalID, SystemID);
  11607. xmlParseExternalSubset(ctxt, ExternalID, SystemID);
  11608. if (ctxt->myDoc != NULL) {
  11609. if (ctxt->wellFormed) {
  11610. ret = ctxt->myDoc->extSubset;
  11611. ctxt->myDoc->extSubset = NULL;
  11612. if (ret != NULL) {
  11613. xmlNodePtr tmp;
  11614. ret->doc = NULL;
  11615. tmp = ret->children;
  11616. while (tmp != NULL) {
  11617. tmp->doc = NULL;
  11618. tmp = tmp->next;
  11619. }
  11620. }
  11621. } else {
  11622. ret = NULL;
  11623. }
  11624. xmlFreeDoc(ctxt->myDoc);
  11625. ctxt->myDoc = NULL;
  11626. }
  11627. xmlFreeParserCtxt(ctxt);
  11628. return(ret);
  11629. }
  11630. /**
  11631. * xmlParseDTD:
  11632. * @ExternalID: a NAME* containing the External ID of the DTD
  11633. * @SystemID: a NAME* containing the URL to the DTD
  11634. *
  11635. * Load and parse an external subset.
  11636. *
  11637. * Returns the resulting xmlDtdPtr or NULL in case of error.
  11638. */
  11639. xmlDtdPtr
  11640. xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) {
  11641. return(xmlSAXParseDTD(NULL, ExternalID, SystemID));
  11642. }
  11643. #endif /* LIBXML_VALID_ENABLED */
  11644. /************************************************************************
  11645. * *
  11646. * Front ends when parsing an Entity *
  11647. * *
  11648. ************************************************************************/
  11649. /**
  11650. * xmlParseCtxtExternalEntity:
  11651. * @ctx: the existing parsing context
  11652. * @URL: the URL for the entity to load
  11653. * @ID: the System ID for the entity to load
  11654. * @lst: the return value for the set of parsed nodes
  11655. *
  11656. * Parse an external general entity within an existing parsing context
  11657. * An external general parsed entity is well-formed if it matches the
  11658. * production labeled extParsedEnt.
  11659. *
  11660. * [78] extParsedEnt ::= TextDecl? content
  11661. *
  11662. * Returns 0 if the entity is well formed, -1 in case of args problem and
  11663. * the parser error code otherwise
  11664. */
  11665. int
  11666. xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL,
  11667. const xmlChar *ID, xmlNodePtr *lst) {
  11668. void *userData;
  11669. if (ctx == NULL) return(-1);
  11670. /*
  11671. * If the user provided their own SAX callbacks, then reuse the
  11672. * userData callback field, otherwise the expected setup in a
  11673. * DOM builder is to have userData == ctxt
  11674. */
  11675. if (ctx->userData == ctx)
  11676. userData = NULL;
  11677. else
  11678. userData = ctx->userData;
  11679. return xmlParseExternalEntityPrivate(ctx->myDoc, ctx, ctx->sax,
  11680. userData, ctx->depth + 1,
  11681. URL, ID, lst);
  11682. }
  11683. /**
  11684. * xmlParseExternalEntityPrivate:
  11685. * @doc: the document the chunk pertains to
  11686. * @oldctxt: the previous parser context if available
  11687. * @sax: the SAX handler block (possibly NULL)
  11688. * @user_data: The user data returned on SAX callbacks (possibly NULL)
  11689. * @depth: Used for loop detection, use 0
  11690. * @URL: the URL for the entity to load
  11691. * @ID: the System ID for the entity to load
  11692. * @list: the return value for the set of parsed nodes
  11693. *
  11694. * Private version of xmlParseExternalEntity()
  11695. *
  11696. * Returns 0 if the entity is well formed, -1 in case of args problem and
  11697. * the parser error code otherwise
  11698. */
  11699. static xmlParserErrors
  11700. xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
  11701. xmlSAXHandlerPtr sax,
  11702. void *user_data, int depth, const xmlChar *URL,
  11703. const xmlChar *ID, xmlNodePtr *list) {
  11704. xmlParserCtxtPtr ctxt;
  11705. xmlDocPtr newDoc;
  11706. xmlNodePtr newRoot;
  11707. xmlParserErrors ret = XML_ERR_OK;
  11708. if (((depth > 40) &&
  11709. ((oldctxt == NULL) || (oldctxt->options & XML_PARSE_HUGE) == 0)) ||
  11710. (depth > 100)) {
  11711. xmlFatalErrMsg(oldctxt, XML_ERR_ENTITY_LOOP,
  11712. "Maximum entity nesting depth exceeded");
  11713. return(XML_ERR_ENTITY_LOOP);
  11714. }
  11715. if (list != NULL)
  11716. *list = NULL;
  11717. if ((URL == NULL) && (ID == NULL))
  11718. return(XML_ERR_INTERNAL_ERROR);
  11719. if (doc == NULL)
  11720. return(XML_ERR_INTERNAL_ERROR);
  11721. ctxt = xmlCreateEntityParserCtxtInternal(sax, user_data, URL, ID, NULL,
  11722. oldctxt);
  11723. if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
  11724. if (oldctxt != NULL) {
  11725. ctxt->nbErrors = oldctxt->nbErrors;
  11726. ctxt->nbWarnings = oldctxt->nbWarnings;
  11727. }
  11728. xmlDetectSAX2(ctxt);
  11729. newDoc = xmlNewDoc(BAD_CAST "1.0");
  11730. if (newDoc == NULL) {
  11731. xmlFreeParserCtxt(ctxt);
  11732. return(XML_ERR_INTERNAL_ERROR);
  11733. }
  11734. newDoc->properties = XML_DOC_INTERNAL;
  11735. if (doc) {
  11736. newDoc->intSubset = doc->intSubset;
  11737. newDoc->extSubset = doc->extSubset;
  11738. if (doc->dict) {
  11739. newDoc->dict = doc->dict;
  11740. xmlDictReference(newDoc->dict);
  11741. }
  11742. if (doc->URL != NULL) {
  11743. newDoc->URL = xmlStrdup(doc->URL);
  11744. }
  11745. }
  11746. newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
  11747. if (newRoot == NULL) {
  11748. if (sax != NULL)
  11749. xmlFreeParserCtxt(ctxt);
  11750. newDoc->intSubset = NULL;
  11751. newDoc->extSubset = NULL;
  11752. xmlFreeDoc(newDoc);
  11753. return(XML_ERR_INTERNAL_ERROR);
  11754. }
  11755. xmlAddChild((xmlNodePtr) newDoc, newRoot);
  11756. nodePush(ctxt, newDoc->children);
  11757. if (doc == NULL) {
  11758. ctxt->myDoc = newDoc;
  11759. } else {
  11760. ctxt->myDoc = doc;
  11761. newRoot->doc = doc;
  11762. }
  11763. xmlDetectEncoding(ctxt);
  11764. /*
  11765. * Parse a possible text declaration first
  11766. */
  11767. if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
  11768. xmlParseTextDecl(ctxt);
  11769. /*
  11770. * An XML-1.0 document can't reference an entity not XML-1.0
  11771. */
  11772. if ((xmlStrEqual(oldctxt->version, BAD_CAST "1.0")) &&
  11773. (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) {
  11774. xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH,
  11775. "Version mismatch between document and entity\n");
  11776. }
  11777. }
  11778. ctxt->instate = XML_PARSER_CONTENT;
  11779. ctxt->depth = depth;
  11780. if (oldctxt != NULL) {
  11781. ctxt->_private = oldctxt->_private;
  11782. ctxt->loadsubset = oldctxt->loadsubset;
  11783. ctxt->validate = oldctxt->validate;
  11784. ctxt->valid = oldctxt->valid;
  11785. ctxt->replaceEntities = oldctxt->replaceEntities;
  11786. if (oldctxt->validate) {
  11787. ctxt->vctxt.error = oldctxt->vctxt.error;
  11788. ctxt->vctxt.warning = oldctxt->vctxt.warning;
  11789. ctxt->vctxt.userData = oldctxt->vctxt.userData;
  11790. ctxt->vctxt.flags = oldctxt->vctxt.flags;
  11791. }
  11792. ctxt->external = oldctxt->external;
  11793. if (ctxt->dict) xmlDictFree(ctxt->dict);
  11794. ctxt->dict = oldctxt->dict;
  11795. ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
  11796. ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
  11797. ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
  11798. ctxt->dictNames = oldctxt->dictNames;
  11799. ctxt->attsDefault = oldctxt->attsDefault;
  11800. ctxt->attsSpecial = oldctxt->attsSpecial;
  11801. ctxt->linenumbers = oldctxt->linenumbers;
  11802. ctxt->record_info = oldctxt->record_info;
  11803. ctxt->node_seq.maximum = oldctxt->node_seq.maximum;
  11804. ctxt->node_seq.length = oldctxt->node_seq.length;
  11805. ctxt->node_seq.buffer = oldctxt->node_seq.buffer;
  11806. } else {
  11807. /*
  11808. * Doing validity checking on chunk without context
  11809. * doesn't make sense
  11810. */
  11811. ctxt->_private = NULL;
  11812. ctxt->validate = 0;
  11813. ctxt->external = 2;
  11814. ctxt->loadsubset = 0;
  11815. }
  11816. xmlParseContent(ctxt);
  11817. if ((RAW == '<') && (NXT(1) == '/')) {
  11818. xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
  11819. } else if (RAW != 0) {
  11820. xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
  11821. }
  11822. if (ctxt->node != newDoc->children) {
  11823. xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
  11824. }
  11825. if (!ctxt->wellFormed) {
  11826. ret = (xmlParserErrors)ctxt->errNo;
  11827. if (oldctxt != NULL) {
  11828. oldctxt->errNo = ctxt->errNo;
  11829. oldctxt->wellFormed = 0;
  11830. xmlCopyError(&ctxt->lastError, &oldctxt->lastError);
  11831. }
  11832. } else {
  11833. if (list != NULL) {
  11834. xmlNodePtr cur;
  11835. /*
  11836. * Return the newly created nodeset after unlinking it from
  11837. * they pseudo parent.
  11838. */
  11839. cur = newDoc->children->children;
  11840. *list = cur;
  11841. while (cur != NULL) {
  11842. cur->parent = NULL;
  11843. cur = cur->next;
  11844. }
  11845. newDoc->children->children = NULL;
  11846. }
  11847. ret = XML_ERR_OK;
  11848. }
  11849. /*
  11850. * Also record the size of the entity parsed
  11851. */
  11852. if (ctxt->input != NULL && oldctxt != NULL) {
  11853. unsigned long consumed = ctxt->input->consumed;
  11854. xmlSaturatedAddSizeT(&consumed, ctxt->input->cur - ctxt->input->base);
  11855. xmlSaturatedAdd(&oldctxt->sizeentities, consumed);
  11856. xmlSaturatedAdd(&oldctxt->sizeentities, ctxt->sizeentities);
  11857. xmlSaturatedAdd(&oldctxt->sizeentcopy, consumed);
  11858. xmlSaturatedAdd(&oldctxt->sizeentcopy, ctxt->sizeentcopy);
  11859. }
  11860. if (oldctxt != NULL) {
  11861. ctxt->dict = NULL;
  11862. ctxt->attsDefault = NULL;
  11863. ctxt->attsSpecial = NULL;
  11864. oldctxt->nbErrors = ctxt->nbErrors;
  11865. oldctxt->nbWarnings = ctxt->nbWarnings;
  11866. oldctxt->validate = ctxt->validate;
  11867. oldctxt->valid = ctxt->valid;
  11868. oldctxt->node_seq.maximum = ctxt->node_seq.maximum;
  11869. oldctxt->node_seq.length = ctxt->node_seq.length;
  11870. oldctxt->node_seq.buffer = ctxt->node_seq.buffer;
  11871. }
  11872. ctxt->node_seq.maximum = 0;
  11873. ctxt->node_seq.length = 0;
  11874. ctxt->node_seq.buffer = NULL;
  11875. xmlFreeParserCtxt(ctxt);
  11876. newDoc->intSubset = NULL;
  11877. newDoc->extSubset = NULL;
  11878. xmlFreeDoc(newDoc);
  11879. return(ret);
  11880. }
  11881. #ifdef LIBXML_SAX1_ENABLED
  11882. /**
  11883. * xmlParseExternalEntity:
  11884. * @doc: the document the chunk pertains to
  11885. * @sax: the SAX handler block (possibly NULL)
  11886. * @user_data: The user data returned on SAX callbacks (possibly NULL)
  11887. * @depth: Used for loop detection, use 0
  11888. * @URL: the URL for the entity to load
  11889. * @ID: the System ID for the entity to load
  11890. * @lst: the return value for the set of parsed nodes
  11891. *
  11892. * Parse an external general entity
  11893. * An external general parsed entity is well-formed if it matches the
  11894. * production labeled extParsedEnt.
  11895. *
  11896. * [78] extParsedEnt ::= TextDecl? content
  11897. *
  11898. * Returns 0 if the entity is well formed, -1 in case of args problem and
  11899. * the parser error code otherwise
  11900. */
  11901. int
  11902. xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data,
  11903. int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) {
  11904. return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL,
  11905. ID, lst));
  11906. }
  11907. /**
  11908. * xmlParseBalancedChunkMemory:
  11909. * @doc: the document the chunk pertains to (must not be NULL)
  11910. * @sax: the SAX handler block (possibly NULL)
  11911. * @user_data: The user data returned on SAX callbacks (possibly NULL)
  11912. * @depth: Used for loop detection, use 0
  11913. * @string: the input string in UTF8 or ISO-Latin (zero terminated)
  11914. * @lst: the return value for the set of parsed nodes
  11915. *
  11916. * Parse a well-balanced chunk of an XML document
  11917. * called by the parser
  11918. * The allowed sequence for the Well Balanced Chunk is the one defined by
  11919. * the content production in the XML grammar:
  11920. *
  11921. * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
  11922. *
  11923. * Returns 0 if the chunk is well balanced, -1 in case of args problem and
  11924. * the parser error code otherwise
  11925. */
  11926. int
  11927. xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax,
  11928. void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) {
  11929. return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data,
  11930. depth, string, lst, 0 );
  11931. }
  11932. #endif /* LIBXML_SAX1_ENABLED */
  11933. /**
  11934. * xmlParseBalancedChunkMemoryInternal:
  11935. * @oldctxt: the existing parsing context
  11936. * @string: the input string in UTF8 or ISO-Latin (zero terminated)
  11937. * @user_data: the user data field for the parser context
  11938. * @lst: the return value for the set of parsed nodes
  11939. *
  11940. *
  11941. * Parse a well-balanced chunk of an XML document
  11942. * called by the parser
  11943. * The allowed sequence for the Well Balanced Chunk is the one defined by
  11944. * the content production in the XML grammar:
  11945. *
  11946. * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
  11947. *
  11948. * Returns XML_ERR_OK if the chunk is well balanced, and the parser
  11949. * error code otherwise
  11950. *
  11951. * In case recover is set to 1, the nodelist will not be empty even if
  11952. * the parsed chunk is not well balanced.
  11953. */
  11954. static xmlParserErrors
  11955. xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
  11956. const xmlChar *string, void *user_data, xmlNodePtr *lst) {
  11957. xmlParserCtxtPtr ctxt;
  11958. xmlDocPtr newDoc = NULL;
  11959. xmlNodePtr newRoot;
  11960. xmlSAXHandlerPtr oldsax = NULL;
  11961. xmlNodePtr content = NULL;
  11962. xmlNodePtr last = NULL;
  11963. xmlParserErrors ret = XML_ERR_OK;
  11964. xmlHashedString hprefix, huri;
  11965. unsigned i;
  11966. if (((oldctxt->depth > 40) && ((oldctxt->options & XML_PARSE_HUGE) == 0)) ||
  11967. (oldctxt->depth > 100)) {
  11968. xmlFatalErrMsg(oldctxt, XML_ERR_ENTITY_LOOP,
  11969. "Maximum entity nesting depth exceeded");
  11970. return(XML_ERR_ENTITY_LOOP);
  11971. }
  11972. if (lst != NULL)
  11973. *lst = NULL;
  11974. if (string == NULL)
  11975. return(XML_ERR_INTERNAL_ERROR);
  11976. ctxt = xmlCreateDocParserCtxt(string);
  11977. if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
  11978. ctxt->nbErrors = oldctxt->nbErrors;
  11979. ctxt->nbWarnings = oldctxt->nbWarnings;
  11980. if (user_data != NULL)
  11981. ctxt->userData = user_data;
  11982. else
  11983. ctxt->userData = ctxt;
  11984. if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
  11985. ctxt->dict = oldctxt->dict;
  11986. ctxt->input_id = oldctxt->input_id;
  11987. ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
  11988. ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
  11989. ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
  11990. /*
  11991. * Propagate namespaces down the entity
  11992. *
  11993. * Making entities and namespaces work correctly requires additional
  11994. * changes, see xmlParseReference.
  11995. */
  11996. /* Default namespace */
  11997. hprefix.name = NULL;
  11998. hprefix.hashValue = 0;
  11999. huri.name = xmlParserNsLookupUri(oldctxt, &hprefix);
  12000. huri.hashValue = 0;
  12001. if (huri.name != NULL)
  12002. xmlParserNsPush(ctxt, NULL, &huri, NULL, 0);
  12003. for (i = 0; i < oldctxt->nsdb->hashSize; i++) {
  12004. xmlParserNsBucket *bucket = &oldctxt->nsdb->hash[i];
  12005. const xmlChar **ns;
  12006. xmlParserNsExtra *extra;
  12007. unsigned nsIndex;
  12008. if ((bucket->hashValue != 0) &&
  12009. (bucket->index != INT_MAX)) {
  12010. nsIndex = bucket->index;
  12011. ns = &oldctxt->nsTab[nsIndex * 2];
  12012. extra = &oldctxt->nsdb->extra[nsIndex];
  12013. hprefix.name = ns[0];
  12014. hprefix.hashValue = bucket->hashValue;
  12015. huri.name = ns[1];
  12016. huri.hashValue = extra->uriHashValue;
  12017. /*
  12018. * Don't copy SAX data to avoid a use-after-free with XML reader.
  12019. * This matches the pre-2.12 behavior.
  12020. */
  12021. xmlParserNsPush(ctxt, &hprefix, &huri, NULL, 0);
  12022. }
  12023. }
  12024. oldsax = ctxt->sax;
  12025. ctxt->sax = oldctxt->sax;
  12026. xmlDetectSAX2(ctxt);
  12027. ctxt->replaceEntities = oldctxt->replaceEntities;
  12028. ctxt->options = oldctxt->options;
  12029. ctxt->_private = oldctxt->_private;
  12030. if (oldctxt->myDoc == NULL) {
  12031. newDoc = xmlNewDoc(BAD_CAST "1.0");
  12032. if (newDoc == NULL) {
  12033. ret = XML_ERR_INTERNAL_ERROR;
  12034. goto error;
  12035. }
  12036. newDoc->properties = XML_DOC_INTERNAL;
  12037. newDoc->dict = ctxt->dict;
  12038. xmlDictReference(newDoc->dict);
  12039. ctxt->myDoc = newDoc;
  12040. } else {
  12041. ctxt->myDoc = oldctxt->myDoc;
  12042. content = ctxt->myDoc->children;
  12043. last = ctxt->myDoc->last;
  12044. }
  12045. newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL);
  12046. if (newRoot == NULL) {
  12047. ret = XML_ERR_INTERNAL_ERROR;
  12048. goto error;
  12049. }
  12050. ctxt->myDoc->children = NULL;
  12051. ctxt->myDoc->last = NULL;
  12052. xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot);
  12053. nodePush(ctxt, ctxt->myDoc->children);
  12054. ctxt->instate = XML_PARSER_CONTENT;
  12055. ctxt->depth = oldctxt->depth;
  12056. ctxt->validate = 0;
  12057. ctxt->loadsubset = oldctxt->loadsubset;
  12058. if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) {
  12059. /*
  12060. * ID/IDREF registration will be done in xmlValidateElement below
  12061. */
  12062. ctxt->loadsubset |= XML_SKIP_IDS;
  12063. }
  12064. ctxt->dictNames = oldctxt->dictNames;
  12065. ctxt->attsDefault = oldctxt->attsDefault;
  12066. ctxt->attsSpecial = oldctxt->attsSpecial;
  12067. xmlParseContent(ctxt);
  12068. if ((RAW == '<') && (NXT(1) == '/')) {
  12069. xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
  12070. } else if (RAW != 0) {
  12071. xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
  12072. }
  12073. if (ctxt->node != ctxt->myDoc->children) {
  12074. xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
  12075. }
  12076. if (!ctxt->wellFormed) {
  12077. ret = (xmlParserErrors)ctxt->errNo;
  12078. oldctxt->errNo = ctxt->errNo;
  12079. oldctxt->wellFormed = 0;
  12080. xmlCopyError(&ctxt->lastError, &oldctxt->lastError);
  12081. } else {
  12082. ret = XML_ERR_OK;
  12083. }
  12084. if ((lst != NULL) && (ret == XML_ERR_OK)) {
  12085. xmlNodePtr cur;
  12086. /*
  12087. * Return the newly created nodeset after unlinking it from
  12088. * they pseudo parent.
  12089. */
  12090. cur = ctxt->myDoc->children->children;
  12091. *lst = cur;
  12092. while (cur != NULL) {
  12093. #ifdef LIBXML_VALID_ENABLED
  12094. if ((oldctxt->validate) && (oldctxt->wellFormed) &&
  12095. (oldctxt->myDoc) && (oldctxt->myDoc->intSubset) &&
  12096. (cur->type == XML_ELEMENT_NODE)) {
  12097. oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt,
  12098. oldctxt->myDoc, cur);
  12099. }
  12100. #endif /* LIBXML_VALID_ENABLED */
  12101. cur->parent = NULL;
  12102. cur = cur->next;
  12103. }
  12104. ctxt->myDoc->children->children = NULL;
  12105. }
  12106. if (ctxt->myDoc != NULL) {
  12107. xmlFreeNode(ctxt->myDoc->children);
  12108. ctxt->myDoc->children = content;
  12109. ctxt->myDoc->last = last;
  12110. }
  12111. /*
  12112. * Also record the size of the entity parsed
  12113. */
  12114. if (ctxt->input != NULL && oldctxt != NULL) {
  12115. unsigned long consumed = ctxt->input->consumed;
  12116. xmlSaturatedAddSizeT(&consumed, ctxt->input->cur - ctxt->input->base);
  12117. xmlSaturatedAdd(&oldctxt->sizeentcopy, consumed);
  12118. xmlSaturatedAdd(&oldctxt->sizeentcopy, ctxt->sizeentcopy);
  12119. }
  12120. oldctxt->nbErrors = ctxt->nbErrors;
  12121. oldctxt->nbWarnings = ctxt->nbWarnings;
  12122. error:
  12123. ctxt->sax = oldsax;
  12124. ctxt->dict = NULL;
  12125. ctxt->attsDefault = NULL;
  12126. ctxt->attsSpecial = NULL;
  12127. xmlFreeParserCtxt(ctxt);
  12128. if (newDoc != NULL) {
  12129. xmlFreeDoc(newDoc);
  12130. }
  12131. return(ret);
  12132. }
  12133. /**
  12134. * xmlParseInNodeContext:
  12135. * @node: the context node
  12136. * @data: the input string
  12137. * @datalen: the input string length in bytes
  12138. * @options: a combination of xmlParserOption
  12139. * @lst: the return value for the set of parsed nodes
  12140. *
  12141. * Parse a well-balanced chunk of an XML document
  12142. * within the context (DTD, namespaces, etc ...) of the given node.
  12143. *
  12144. * The allowed sequence for the data is a Well Balanced Chunk defined by
  12145. * the content production in the XML grammar:
  12146. *
  12147. * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
  12148. *
  12149. * Returns XML_ERR_OK if the chunk is well balanced, and the parser
  12150. * error code otherwise
  12151. */
  12152. xmlParserErrors
  12153. xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen,
  12154. int options, xmlNodePtr *lst) {
  12155. xmlParserCtxtPtr ctxt;
  12156. xmlDocPtr doc = NULL;
  12157. xmlNodePtr fake, cur;
  12158. int nsnr = 0;
  12159. xmlParserErrors ret = XML_ERR_OK;
  12160. /*
  12161. * check all input parameters, grab the document
  12162. */
  12163. if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0))
  12164. return(XML_ERR_INTERNAL_ERROR);
  12165. switch (node->type) {
  12166. case XML_ELEMENT_NODE:
  12167. case XML_ATTRIBUTE_NODE:
  12168. case XML_TEXT_NODE:
  12169. case XML_CDATA_SECTION_NODE:
  12170. case XML_ENTITY_REF_NODE:
  12171. case XML_PI_NODE:
  12172. case XML_COMMENT_NODE:
  12173. case XML_DOCUMENT_NODE:
  12174. case XML_HTML_DOCUMENT_NODE:
  12175. break;
  12176. default:
  12177. return(XML_ERR_INTERNAL_ERROR);
  12178. }
  12179. while ((node != NULL) && (node->type != XML_ELEMENT_NODE) &&
  12180. (node->type != XML_DOCUMENT_NODE) &&
  12181. (node->type != XML_HTML_DOCUMENT_NODE))
  12182. node = node->parent;
  12183. if (node == NULL)
  12184. return(XML_ERR_INTERNAL_ERROR);
  12185. if (node->type == XML_ELEMENT_NODE)
  12186. doc = node->doc;
  12187. else
  12188. doc = (xmlDocPtr) node;
  12189. if (doc == NULL)
  12190. return(XML_ERR_INTERNAL_ERROR);
  12191. /*
  12192. * allocate a context and set-up everything not related to the
  12193. * node position in the tree
  12194. */
  12195. if (doc->type == XML_DOCUMENT_NODE)
  12196. ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen);
  12197. #ifdef LIBXML_HTML_ENABLED
  12198. else if (doc->type == XML_HTML_DOCUMENT_NODE) {
  12199. ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen);
  12200. /*
  12201. * When parsing in context, it makes no sense to add implied
  12202. * elements like html/body/etc...
  12203. */
  12204. options |= HTML_PARSE_NOIMPLIED;
  12205. }
  12206. #endif
  12207. else
  12208. return(XML_ERR_INTERNAL_ERROR);
  12209. if (ctxt == NULL)
  12210. return(XML_ERR_NO_MEMORY);
  12211. /*
  12212. * Use input doc's dict if present, else assure XML_PARSE_NODICT is set.
  12213. * We need a dictionary for xmlDetectSAX2, so if there's no doc dict
  12214. * we must wait until the last moment to free the original one.
  12215. */
  12216. if (doc->dict != NULL) {
  12217. if (ctxt->dict != NULL)
  12218. xmlDictFree(ctxt->dict);
  12219. ctxt->dict = doc->dict;
  12220. } else
  12221. options |= XML_PARSE_NODICT;
  12222. if (doc->encoding != NULL) {
  12223. xmlCharEncodingHandlerPtr hdlr;
  12224. hdlr = xmlFindCharEncodingHandler((const char *) doc->encoding);
  12225. if (hdlr != NULL) {
  12226. xmlSwitchToEncoding(ctxt, hdlr);
  12227. } else {
  12228. return(XML_ERR_UNSUPPORTED_ENCODING);
  12229. }
  12230. }
  12231. xmlCtxtUseOptionsInternal(ctxt, options);
  12232. xmlDetectSAX2(ctxt);
  12233. ctxt->myDoc = doc;
  12234. /* parsing in context, i.e. as within existing content */
  12235. ctxt->input_id = 2;
  12236. ctxt->instate = XML_PARSER_CONTENT;
  12237. fake = xmlNewDocComment(node->doc, NULL);
  12238. if (fake == NULL) {
  12239. xmlFreeParserCtxt(ctxt);
  12240. return(XML_ERR_NO_MEMORY);
  12241. }
  12242. xmlAddChild(node, fake);
  12243. if (node->type == XML_ELEMENT_NODE)
  12244. nodePush(ctxt, node);
  12245. if ((ctxt->html == 0) && (node->type == XML_ELEMENT_NODE)) {
  12246. /*
  12247. * initialize the SAX2 namespaces stack
  12248. */
  12249. cur = node;
  12250. while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) {
  12251. xmlNsPtr ns = cur->nsDef;
  12252. xmlHashedString hprefix, huri;
  12253. while (ns != NULL) {
  12254. hprefix = xmlDictLookupHashed(ctxt->dict, ns->prefix, -1);
  12255. huri = xmlDictLookupHashed(ctxt->dict, ns->href, -1);
  12256. if (xmlParserNsPush(ctxt, &hprefix, &huri, ns, 1) > 0)
  12257. nsnr++;
  12258. ns = ns->next;
  12259. }
  12260. cur = cur->parent;
  12261. }
  12262. }
  12263. if ((ctxt->validate) || (ctxt->replaceEntities != 0)) {
  12264. /*
  12265. * ID/IDREF registration will be done in xmlValidateElement below
  12266. */
  12267. ctxt->loadsubset |= XML_SKIP_IDS;
  12268. }
  12269. #ifdef LIBXML_HTML_ENABLED
  12270. if (doc->type == XML_HTML_DOCUMENT_NODE)
  12271. __htmlParseContent(ctxt);
  12272. else
  12273. #endif
  12274. xmlParseContent(ctxt);
  12275. xmlParserNsPop(ctxt, nsnr);
  12276. if ((RAW == '<') && (NXT(1) == '/')) {
  12277. xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
  12278. } else if (RAW != 0) {
  12279. xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
  12280. }
  12281. if ((ctxt->node != NULL) && (ctxt->node != node)) {
  12282. xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
  12283. ctxt->wellFormed = 0;
  12284. }
  12285. if (!ctxt->wellFormed) {
  12286. if (ctxt->errNo == 0)
  12287. ret = XML_ERR_INTERNAL_ERROR;
  12288. else
  12289. ret = (xmlParserErrors)ctxt->errNo;
  12290. } else {
  12291. ret = XML_ERR_OK;
  12292. }
  12293. /*
  12294. * Return the newly created nodeset after unlinking it from
  12295. * the pseudo sibling.
  12296. */
  12297. cur = fake->next;
  12298. fake->next = NULL;
  12299. node->last = fake;
  12300. if (cur != NULL) {
  12301. cur->prev = NULL;
  12302. }
  12303. *lst = cur;
  12304. while (cur != NULL) {
  12305. cur->parent = NULL;
  12306. cur = cur->next;
  12307. }
  12308. xmlUnlinkNode(fake);
  12309. xmlFreeNode(fake);
  12310. if (ret != XML_ERR_OK) {
  12311. xmlFreeNodeList(*lst);
  12312. *lst = NULL;
  12313. }
  12314. if (doc->dict != NULL)
  12315. ctxt->dict = NULL;
  12316. xmlFreeParserCtxt(ctxt);
  12317. return(ret);
  12318. }
  12319. #ifdef LIBXML_SAX1_ENABLED
  12320. /**
  12321. * xmlParseBalancedChunkMemoryRecover:
  12322. * @doc: the document the chunk pertains to (must not be NULL)
  12323. * @sax: the SAX handler block (possibly NULL)
  12324. * @user_data: The user data returned on SAX callbacks (possibly NULL)
  12325. * @depth: Used for loop detection, use 0
  12326. * @string: the input string in UTF8 or ISO-Latin (zero terminated)
  12327. * @lst: the return value for the set of parsed nodes
  12328. * @recover: return nodes even if the data is broken (use 0)
  12329. *
  12330. *
  12331. * Parse a well-balanced chunk of an XML document
  12332. * called by the parser
  12333. * The allowed sequence for the Well Balanced Chunk is the one defined by
  12334. * the content production in the XML grammar:
  12335. *
  12336. * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
  12337. *
  12338. * Returns 0 if the chunk is well balanced, -1 in case of args problem and
  12339. * the parser error code otherwise
  12340. *
  12341. * In case recover is set to 1, the nodelist will not be empty even if
  12342. * the parsed chunk is not well balanced, assuming the parsing succeeded to
  12343. * some extent.
  12344. */
  12345. int
  12346. xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax,
  12347. void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst,
  12348. int recover) {
  12349. xmlParserCtxtPtr ctxt;
  12350. xmlDocPtr newDoc;
  12351. xmlSAXHandlerPtr oldsax = NULL;
  12352. xmlNodePtr content, newRoot;
  12353. int ret = 0;
  12354. if (depth > 40) {
  12355. return(XML_ERR_ENTITY_LOOP);
  12356. }
  12357. if (lst != NULL)
  12358. *lst = NULL;
  12359. if (string == NULL)
  12360. return(-1);
  12361. ctxt = xmlCreateDocParserCtxt(string);
  12362. if (ctxt == NULL) return(-1);
  12363. ctxt->userData = ctxt;
  12364. if (sax != NULL) {
  12365. oldsax = ctxt->sax;
  12366. ctxt->sax = sax;
  12367. if (user_data != NULL)
  12368. ctxt->userData = user_data;
  12369. }
  12370. newDoc = xmlNewDoc(BAD_CAST "1.0");
  12371. if (newDoc == NULL) {
  12372. xmlFreeParserCtxt(ctxt);
  12373. return(-1);
  12374. }
  12375. newDoc->properties = XML_DOC_INTERNAL;
  12376. if ((doc != NULL) && (doc->dict != NULL)) {
  12377. xmlDictFree(ctxt->dict);
  12378. ctxt->dict = doc->dict;
  12379. xmlDictReference(ctxt->dict);
  12380. ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
  12381. ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
  12382. ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
  12383. ctxt->dictNames = 1;
  12384. newDoc->dict = ctxt->dict;
  12385. xmlDictReference(newDoc->dict);
  12386. } else {
  12387. xmlCtxtUseOptionsInternal(ctxt, XML_PARSE_NODICT);
  12388. }
  12389. /* doc == NULL is only supported for historic reasons */
  12390. if (doc != NULL) {
  12391. newDoc->intSubset = doc->intSubset;
  12392. newDoc->extSubset = doc->extSubset;
  12393. }
  12394. newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
  12395. if (newRoot == NULL) {
  12396. if (sax != NULL)
  12397. ctxt->sax = oldsax;
  12398. xmlFreeParserCtxt(ctxt);
  12399. newDoc->intSubset = NULL;
  12400. newDoc->extSubset = NULL;
  12401. xmlFreeDoc(newDoc);
  12402. return(-1);
  12403. }
  12404. xmlAddChild((xmlNodePtr) newDoc, newRoot);
  12405. nodePush(ctxt, newRoot);
  12406. /* doc == NULL is only supported for historic reasons */
  12407. if (doc == NULL) {
  12408. ctxt->myDoc = newDoc;
  12409. } else {
  12410. ctxt->myDoc = newDoc;
  12411. /* Ensure that doc has XML spec namespace */
  12412. xmlSearchNsByHref(doc, (xmlNodePtr)doc, XML_XML_NAMESPACE);
  12413. newDoc->oldNs = doc->oldNs;
  12414. }
  12415. ctxt->instate = XML_PARSER_CONTENT;
  12416. ctxt->input_id = 2;
  12417. ctxt->depth = depth;
  12418. /*
  12419. * Doing validity checking on chunk doesn't make sense
  12420. */
  12421. ctxt->validate = 0;
  12422. ctxt->loadsubset = 0;
  12423. xmlDetectSAX2(ctxt);
  12424. if ( doc != NULL ){
  12425. content = doc->children;
  12426. doc->children = NULL;
  12427. xmlParseContent(ctxt);
  12428. doc->children = content;
  12429. }
  12430. else {
  12431. xmlParseContent(ctxt);
  12432. }
  12433. if ((RAW == '<') && (NXT(1) == '/')) {
  12434. xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
  12435. } else if (RAW != 0) {
  12436. xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
  12437. }
  12438. if (ctxt->node != newDoc->children) {
  12439. xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
  12440. }
  12441. if (!ctxt->wellFormed) {
  12442. if (ctxt->errNo == 0)
  12443. ret = 1;
  12444. else
  12445. ret = ctxt->errNo;
  12446. } else {
  12447. ret = 0;
  12448. }
  12449. if ((lst != NULL) && ((ret == 0) || (recover == 1))) {
  12450. xmlNodePtr cur;
  12451. /*
  12452. * Return the newly created nodeset after unlinking it from
  12453. * they pseudo parent.
  12454. */
  12455. cur = newDoc->children->children;
  12456. *lst = cur;
  12457. while (cur != NULL) {
  12458. xmlSetTreeDoc(cur, doc);
  12459. cur->parent = NULL;
  12460. cur = cur->next;
  12461. }
  12462. newDoc->children->children = NULL;
  12463. }
  12464. if (sax != NULL)
  12465. ctxt->sax = oldsax;
  12466. xmlFreeParserCtxt(ctxt);
  12467. newDoc->intSubset = NULL;
  12468. newDoc->extSubset = NULL;
  12469. /* This leaks the namespace list if doc == NULL */
  12470. newDoc->oldNs = NULL;
  12471. xmlFreeDoc(newDoc);
  12472. return(ret);
  12473. }
  12474. /**
  12475. * xmlSAXParseEntity:
  12476. * @sax: the SAX handler block
  12477. * @filename: the filename
  12478. *
  12479. * DEPRECATED: Don't use.
  12480. *
  12481. * parse an XML external entity out of context and build a tree.
  12482. * It use the given SAX function block to handle the parsing callback.
  12483. * If sax is NULL, fallback to the default DOM tree building routines.
  12484. *
  12485. * [78] extParsedEnt ::= TextDecl? content
  12486. *
  12487. * This correspond to a "Well Balanced" chunk
  12488. *
  12489. * Returns the resulting document tree
  12490. */
  12491. xmlDocPtr
  12492. xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) {
  12493. xmlDocPtr ret;
  12494. xmlParserCtxtPtr ctxt;
  12495. ctxt = xmlCreateFileParserCtxt(filename);
  12496. if (ctxt == NULL) {
  12497. return(NULL);
  12498. }
  12499. if (sax != NULL) {
  12500. if (ctxt->sax != NULL)
  12501. xmlFree(ctxt->sax);
  12502. ctxt->sax = sax;
  12503. ctxt->userData = NULL;
  12504. }
  12505. xmlParseExtParsedEnt(ctxt);
  12506. if (ctxt->wellFormed)
  12507. ret = ctxt->myDoc;
  12508. else {
  12509. ret = NULL;
  12510. xmlFreeDoc(ctxt->myDoc);
  12511. ctxt->myDoc = NULL;
  12512. }
  12513. if (sax != NULL)
  12514. ctxt->sax = NULL;
  12515. xmlFreeParserCtxt(ctxt);
  12516. return(ret);
  12517. }
  12518. /**
  12519. * xmlParseEntity:
  12520. * @filename: the filename
  12521. *
  12522. * parse an XML external entity out of context and build a tree.
  12523. *
  12524. * [78] extParsedEnt ::= TextDecl? content
  12525. *
  12526. * This correspond to a "Well Balanced" chunk
  12527. *
  12528. * Returns the resulting document tree
  12529. */
  12530. xmlDocPtr
  12531. xmlParseEntity(const char *filename) {
  12532. return(xmlSAXParseEntity(NULL, filename));
  12533. }
  12534. #endif /* LIBXML_SAX1_ENABLED */
  12535. /**
  12536. * xmlCreateEntityParserCtxtInternal:
  12537. * @URL: the entity URL
  12538. * @ID: the entity PUBLIC ID
  12539. * @base: a possible base for the target URI
  12540. * @pctx: parser context used to set options on new context
  12541. *
  12542. * Create a parser context for an external entity
  12543. * Automatic support for ZLIB/Compress compressed document is provided
  12544. * by default if found at compile-time.
  12545. *
  12546. * Returns the new parser context or NULL
  12547. */
  12548. static xmlParserCtxtPtr
  12549. xmlCreateEntityParserCtxtInternal(xmlSAXHandlerPtr sax, void *userData,
  12550. const xmlChar *URL, const xmlChar *ID, const xmlChar *base,
  12551. xmlParserCtxtPtr pctx) {
  12552. xmlParserCtxtPtr ctxt;
  12553. xmlParserInputPtr inputStream;
  12554. char *directory = NULL;
  12555. xmlChar *uri;
  12556. ctxt = xmlNewSAXParserCtxt(sax, userData);
  12557. if (ctxt == NULL) {
  12558. return(NULL);
  12559. }
  12560. if (pctx != NULL) {
  12561. ctxt->options = pctx->options;
  12562. ctxt->_private = pctx->_private;
  12563. ctxt->input_id = pctx->input_id;
  12564. }
  12565. /* Don't read from stdin. */
  12566. if (xmlStrcmp(URL, BAD_CAST "-") == 0)
  12567. URL = BAD_CAST "./-";
  12568. uri = xmlBuildURI(URL, base);
  12569. if (uri == NULL) {
  12570. inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt);
  12571. if (inputStream == NULL) {
  12572. xmlFreeParserCtxt(ctxt);
  12573. return(NULL);
  12574. }
  12575. inputPush(ctxt, inputStream);
  12576. if ((ctxt->directory == NULL) && (directory == NULL))
  12577. directory = xmlParserGetDirectory((char *)URL);
  12578. if ((ctxt->directory == NULL) && (directory != NULL))
  12579. ctxt->directory = directory;
  12580. } else {
  12581. inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt);
  12582. if (inputStream == NULL) {
  12583. xmlFree(uri);
  12584. xmlFreeParserCtxt(ctxt);
  12585. return(NULL);
  12586. }
  12587. inputPush(ctxt, inputStream);
  12588. if ((ctxt->directory == NULL) && (directory == NULL))
  12589. directory = xmlParserGetDirectory((char *)uri);
  12590. if ((ctxt->directory == NULL) && (directory != NULL))
  12591. ctxt->directory = directory;
  12592. xmlFree(uri);
  12593. }
  12594. return(ctxt);
  12595. }
  12596. /**
  12597. * xmlCreateEntityParserCtxt:
  12598. * @URL: the entity URL
  12599. * @ID: the entity PUBLIC ID
  12600. * @base: a possible base for the target URI
  12601. *
  12602. * Create a parser context for an external entity
  12603. * Automatic support for ZLIB/Compress compressed document is provided
  12604. * by default if found at compile-time.
  12605. *
  12606. * Returns the new parser context or NULL
  12607. */
  12608. xmlParserCtxtPtr
  12609. xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
  12610. const xmlChar *base) {
  12611. return xmlCreateEntityParserCtxtInternal(NULL, NULL, URL, ID, base, NULL);
  12612. }
  12613. /************************************************************************
  12614. * *
  12615. * Front ends when parsing from a file *
  12616. * *
  12617. ************************************************************************/
  12618. /**
  12619. * xmlCreateURLParserCtxt:
  12620. * @filename: the filename or URL
  12621. * @options: a combination of xmlParserOption
  12622. *
  12623. * Create a parser context for a file or URL content.
  12624. * Automatic support for ZLIB/Compress compressed document is provided
  12625. * by default if found at compile-time and for file accesses
  12626. *
  12627. * Returns the new parser context or NULL
  12628. */
  12629. xmlParserCtxtPtr
  12630. xmlCreateURLParserCtxt(const char *filename, int options)
  12631. {
  12632. xmlParserCtxtPtr ctxt;
  12633. xmlParserInputPtr inputStream;
  12634. char *directory = NULL;
  12635. ctxt = xmlNewParserCtxt();
  12636. if (ctxt == NULL) {
  12637. xmlErrMemory(NULL, "cannot allocate parser context");
  12638. return(NULL);
  12639. }
  12640. if (options)
  12641. xmlCtxtUseOptionsInternal(ctxt, options);
  12642. ctxt->linenumbers = 1;
  12643. inputStream = xmlLoadExternalEntity(filename, NULL, ctxt);
  12644. if (inputStream == NULL) {
  12645. xmlFreeParserCtxt(ctxt);
  12646. return(NULL);
  12647. }
  12648. inputPush(ctxt, inputStream);
  12649. if ((ctxt->directory == NULL) && (directory == NULL))
  12650. directory = xmlParserGetDirectory(filename);
  12651. if ((ctxt->directory == NULL) && (directory != NULL))
  12652. ctxt->directory = directory;
  12653. return(ctxt);
  12654. }
  12655. /**
  12656. * xmlCreateFileParserCtxt:
  12657. * @filename: the filename
  12658. *
  12659. * Create a parser context for a file content.
  12660. * Automatic support for ZLIB/Compress compressed document is provided
  12661. * by default if found at compile-time.
  12662. *
  12663. * Returns the new parser context or NULL
  12664. */
  12665. xmlParserCtxtPtr
  12666. xmlCreateFileParserCtxt(const char *filename)
  12667. {
  12668. return(xmlCreateURLParserCtxt(filename, 0));
  12669. }
  12670. #ifdef LIBXML_SAX1_ENABLED
  12671. /**
  12672. * xmlSAXParseFileWithData:
  12673. * @sax: the SAX handler block
  12674. * @filename: the filename
  12675. * @recovery: work in recovery mode, i.e. tries to read no Well Formed
  12676. * documents
  12677. * @data: the userdata
  12678. *
  12679. * DEPRECATED: Use xmlNewSAXParserCtxt and xmlCtxtReadFile.
  12680. *
  12681. * parse an XML file and build a tree. Automatic support for ZLIB/Compress
  12682. * compressed document is provided by default if found at compile-time.
  12683. * It use the given SAX function block to handle the parsing callback.
  12684. * If sax is NULL, fallback to the default DOM tree building routines.
  12685. *
  12686. * User data (void *) is stored within the parser context in the
  12687. * context's _private member, so it is available nearly everywhere in libxml
  12688. *
  12689. * Returns the resulting document tree
  12690. */
  12691. xmlDocPtr
  12692. xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename,
  12693. int recovery, void *data) {
  12694. xmlDocPtr ret;
  12695. xmlParserCtxtPtr ctxt;
  12696. xmlInitParser();
  12697. ctxt = xmlCreateFileParserCtxt(filename);
  12698. if (ctxt == NULL) {
  12699. return(NULL);
  12700. }
  12701. if (sax != NULL) {
  12702. if (ctxt->sax != NULL)
  12703. xmlFree(ctxt->sax);
  12704. ctxt->sax = sax;
  12705. }
  12706. xmlDetectSAX2(ctxt);
  12707. if (data!=NULL) {
  12708. ctxt->_private = data;
  12709. }
  12710. if (ctxt->directory == NULL)
  12711. ctxt->directory = xmlParserGetDirectory(filename);
  12712. ctxt->recovery = recovery;
  12713. xmlParseDocument(ctxt);
  12714. if ((ctxt->wellFormed) || recovery) {
  12715. ret = ctxt->myDoc;
  12716. if ((ret != NULL) && (ctxt->input->buf != NULL)) {
  12717. if (ctxt->input->buf->compressed > 0)
  12718. ret->compression = 9;
  12719. else
  12720. ret->compression = ctxt->input->buf->compressed;
  12721. }
  12722. }
  12723. else {
  12724. ret = NULL;
  12725. xmlFreeDoc(ctxt->myDoc);
  12726. ctxt->myDoc = NULL;
  12727. }
  12728. if (sax != NULL)
  12729. ctxt->sax = NULL;
  12730. xmlFreeParserCtxt(ctxt);
  12731. return(ret);
  12732. }
  12733. /**
  12734. * xmlSAXParseFile:
  12735. * @sax: the SAX handler block
  12736. * @filename: the filename
  12737. * @recovery: work in recovery mode, i.e. tries to read no Well Formed
  12738. * documents
  12739. *
  12740. * DEPRECATED: Use xmlNewSAXParserCtxt and xmlCtxtReadFile.
  12741. *
  12742. * parse an XML file and build a tree. Automatic support for ZLIB/Compress
  12743. * compressed document is provided by default if found at compile-time.
  12744. * It use the given SAX function block to handle the parsing callback.
  12745. * If sax is NULL, fallback to the default DOM tree building routines.
  12746. *
  12747. * Returns the resulting document tree
  12748. */
  12749. xmlDocPtr
  12750. xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename,
  12751. int recovery) {
  12752. return(xmlSAXParseFileWithData(sax,filename,recovery,NULL));
  12753. }
  12754. /**
  12755. * xmlRecoverDoc:
  12756. * @cur: a pointer to an array of xmlChar
  12757. *
  12758. * DEPRECATED: Use xmlReadDoc with XML_PARSE_RECOVER.
  12759. *
  12760. * parse an XML in-memory document and build a tree.
  12761. * In the case the document is not Well Formed, a attempt to build a
  12762. * tree is tried anyway
  12763. *
  12764. * Returns the resulting document tree or NULL in case of failure
  12765. */
  12766. xmlDocPtr
  12767. xmlRecoverDoc(const xmlChar *cur) {
  12768. return(xmlSAXParseDoc(NULL, cur, 1));
  12769. }
  12770. /**
  12771. * xmlParseFile:
  12772. * @filename: the filename
  12773. *
  12774. * DEPRECATED: Use xmlReadFile.
  12775. *
  12776. * parse an XML file and build a tree. Automatic support for ZLIB/Compress
  12777. * compressed document is provided by default if found at compile-time.
  12778. *
  12779. * Returns the resulting document tree if the file was wellformed,
  12780. * NULL otherwise.
  12781. */
  12782. xmlDocPtr
  12783. xmlParseFile(const char *filename) {
  12784. return(xmlSAXParseFile(NULL, filename, 0));
  12785. }
  12786. /**
  12787. * xmlRecoverFile:
  12788. * @filename: the filename
  12789. *
  12790. * DEPRECATED: Use xmlReadFile with XML_PARSE_RECOVER.
  12791. *
  12792. * parse an XML file and build a tree. Automatic support for ZLIB/Compress
  12793. * compressed document is provided by default if found at compile-time.
  12794. * In the case the document is not Well Formed, it attempts to build
  12795. * a tree anyway
  12796. *
  12797. * Returns the resulting document tree or NULL in case of failure
  12798. */
  12799. xmlDocPtr
  12800. xmlRecoverFile(const char *filename) {
  12801. return(xmlSAXParseFile(NULL, filename, 1));
  12802. }
  12803. /**
  12804. * xmlSetupParserForBuffer:
  12805. * @ctxt: an XML parser context
  12806. * @buffer: a xmlChar * buffer
  12807. * @filename: a file name
  12808. *
  12809. * DEPRECATED: Don't use.
  12810. *
  12811. * Setup the parser context to parse a new buffer; Clears any prior
  12812. * contents from the parser context. The buffer parameter must not be
  12813. * NULL, but the filename parameter can be
  12814. */
  12815. void
  12816. xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer,
  12817. const char* filename)
  12818. {
  12819. xmlParserInputPtr input;
  12820. if ((ctxt == NULL) || (buffer == NULL))
  12821. return;
  12822. input = xmlNewInputStream(ctxt);
  12823. if (input == NULL) {
  12824. xmlErrMemory(NULL, "parsing new buffer: out of memory\n");
  12825. xmlClearParserCtxt(ctxt);
  12826. return;
  12827. }
  12828. xmlClearParserCtxt(ctxt);
  12829. if (filename != NULL)
  12830. input->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
  12831. input->base = buffer;
  12832. input->cur = buffer;
  12833. input->end = &buffer[xmlStrlen(buffer)];
  12834. inputPush(ctxt, input);
  12835. }
  12836. /**
  12837. * xmlSAXUserParseFile:
  12838. * @sax: a SAX handler
  12839. * @user_data: The user data returned on SAX callbacks
  12840. * @filename: a file name
  12841. *
  12842. * DEPRECATED: Use xmlNewSAXParserCtxt and xmlCtxtReadFile.
  12843. *
  12844. * parse an XML file and call the given SAX handler routines.
  12845. * Automatic support for ZLIB/Compress compressed document is provided
  12846. *
  12847. * Returns 0 in case of success or a error number otherwise
  12848. */
  12849. int
  12850. xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data,
  12851. const char *filename) {
  12852. int ret = 0;
  12853. xmlParserCtxtPtr ctxt;
  12854. ctxt = xmlCreateFileParserCtxt(filename);
  12855. if (ctxt == NULL) return -1;
  12856. if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
  12857. xmlFree(ctxt->sax);
  12858. ctxt->sax = sax;
  12859. xmlDetectSAX2(ctxt);
  12860. if (user_data != NULL)
  12861. ctxt->userData = user_data;
  12862. xmlParseDocument(ctxt);
  12863. if (ctxt->wellFormed)
  12864. ret = 0;
  12865. else {
  12866. if (ctxt->errNo != 0)
  12867. ret = ctxt->errNo;
  12868. else
  12869. ret = -1;
  12870. }
  12871. if (sax != NULL)
  12872. ctxt->sax = NULL;
  12873. if (ctxt->myDoc != NULL) {
  12874. xmlFreeDoc(ctxt->myDoc);
  12875. ctxt->myDoc = NULL;
  12876. }
  12877. xmlFreeParserCtxt(ctxt);
  12878. return ret;
  12879. }
  12880. #endif /* LIBXML_SAX1_ENABLED */
  12881. /************************************************************************
  12882. * *
  12883. * Front ends when parsing from memory *
  12884. * *
  12885. ************************************************************************/
  12886. /**
  12887. * xmlCreateMemoryParserCtxt:
  12888. * @buffer: a pointer to a char array
  12889. * @size: the size of the array
  12890. *
  12891. * Create a parser context for an XML in-memory document.
  12892. *
  12893. * Returns the new parser context or NULL
  12894. */
  12895. xmlParserCtxtPtr
  12896. xmlCreateMemoryParserCtxt(const char *buffer, int size) {
  12897. xmlParserCtxtPtr ctxt;
  12898. xmlParserInputPtr input;
  12899. xmlParserInputBufferPtr buf;
  12900. if (buffer == NULL)
  12901. return(NULL);
  12902. if (size <= 0)
  12903. return(NULL);
  12904. ctxt = xmlNewParserCtxt();
  12905. if (ctxt == NULL)
  12906. return(NULL);
  12907. buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
  12908. if (buf == NULL) {
  12909. xmlFreeParserCtxt(ctxt);
  12910. return(NULL);
  12911. }
  12912. input = xmlNewInputStream(ctxt);
  12913. if (input == NULL) {
  12914. xmlFreeParserInputBuffer(buf);
  12915. xmlFreeParserCtxt(ctxt);
  12916. return(NULL);
  12917. }
  12918. input->filename = NULL;
  12919. input->buf = buf;
  12920. xmlBufResetInput(input->buf->buffer, input);
  12921. inputPush(ctxt, input);
  12922. return(ctxt);
  12923. }
  12924. #ifdef LIBXML_SAX1_ENABLED
  12925. /**
  12926. * xmlSAXParseMemoryWithData:
  12927. * @sax: the SAX handler block
  12928. * @buffer: an pointer to a char array
  12929. * @size: the size of the array
  12930. * @recovery: work in recovery mode, i.e. tries to read no Well Formed
  12931. * documents
  12932. * @data: the userdata
  12933. *
  12934. * DEPRECATED: Use xmlNewSAXParserCtxt and xmlCtxtReadMemory.
  12935. *
  12936. * parse an XML in-memory block and use the given SAX function block
  12937. * to handle the parsing callback. If sax is NULL, fallback to the default
  12938. * DOM tree building routines.
  12939. *
  12940. * User data (void *) is stored within the parser context in the
  12941. * context's _private member, so it is available nearly everywhere in libxml
  12942. *
  12943. * Returns the resulting document tree
  12944. */
  12945. xmlDocPtr
  12946. xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer,
  12947. int size, int recovery, void *data) {
  12948. xmlDocPtr ret;
  12949. xmlParserCtxtPtr ctxt;
  12950. xmlInitParser();
  12951. ctxt = xmlCreateMemoryParserCtxt(buffer, size);
  12952. if (ctxt == NULL) return(NULL);
  12953. if (sax != NULL) {
  12954. if (ctxt->sax != NULL)
  12955. xmlFree(ctxt->sax);
  12956. ctxt->sax = sax;
  12957. }
  12958. xmlDetectSAX2(ctxt);
  12959. if (data!=NULL) {
  12960. ctxt->_private=data;
  12961. }
  12962. ctxt->recovery = recovery;
  12963. xmlParseDocument(ctxt);
  12964. if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
  12965. else {
  12966. ret = NULL;
  12967. xmlFreeDoc(ctxt->myDoc);
  12968. ctxt->myDoc = NULL;
  12969. }
  12970. if (sax != NULL)
  12971. ctxt->sax = NULL;
  12972. xmlFreeParserCtxt(ctxt);
  12973. return(ret);
  12974. }
  12975. /**
  12976. * xmlSAXParseMemory:
  12977. * @sax: the SAX handler block
  12978. * @buffer: an pointer to a char array
  12979. * @size: the size of the array
  12980. * @recovery: work in recovery mode, i.e. tries to read not Well Formed
  12981. * documents
  12982. *
  12983. * DEPRECATED: Use xmlNewSAXParserCtxt and xmlCtxtReadMemory.
  12984. *
  12985. * parse an XML in-memory block and use the given SAX function block
  12986. * to handle the parsing callback. If sax is NULL, fallback to the default
  12987. * DOM tree building routines.
  12988. *
  12989. * Returns the resulting document tree
  12990. */
  12991. xmlDocPtr
  12992. xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer,
  12993. int size, int recovery) {
  12994. return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL);
  12995. }
  12996. /**
  12997. * xmlParseMemory:
  12998. * @buffer: an pointer to a char array
  12999. * @size: the size of the array
  13000. *
  13001. * DEPRECATED: Use xmlReadMemory.
  13002. *
  13003. * parse an XML in-memory block and build a tree.
  13004. *
  13005. * Returns the resulting document tree
  13006. */
  13007. xmlDocPtr xmlParseMemory(const char *buffer, int size) {
  13008. return(xmlSAXParseMemory(NULL, buffer, size, 0));
  13009. }
  13010. /**
  13011. * xmlRecoverMemory:
  13012. * @buffer: an pointer to a char array
  13013. * @size: the size of the array
  13014. *
  13015. * DEPRECATED: Use xmlReadMemory with XML_PARSE_RECOVER.
  13016. *
  13017. * parse an XML in-memory block and build a tree.
  13018. * In the case the document is not Well Formed, an attempt to
  13019. * build a tree is tried anyway
  13020. *
  13021. * Returns the resulting document tree or NULL in case of error
  13022. */
  13023. xmlDocPtr xmlRecoverMemory(const char *buffer, int size) {
  13024. return(xmlSAXParseMemory(NULL, buffer, size, 1));
  13025. }
  13026. /**
  13027. * xmlSAXUserParseMemory:
  13028. * @sax: a SAX handler
  13029. * @user_data: The user data returned on SAX callbacks
  13030. * @buffer: an in-memory XML document input
  13031. * @size: the length of the XML document in bytes
  13032. *
  13033. * DEPRECATED: Use xmlNewSAXParserCtxt and xmlCtxtReadMemory.
  13034. *
  13035. * parse an XML in-memory buffer and call the given SAX handler routines.
  13036. *
  13037. * Returns 0 in case of success or a error number otherwise
  13038. */
  13039. int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data,
  13040. const char *buffer, int size) {
  13041. int ret = 0;
  13042. xmlParserCtxtPtr ctxt;
  13043. xmlInitParser();
  13044. ctxt = xmlCreateMemoryParserCtxt(buffer, size);
  13045. if (ctxt == NULL) return -1;
  13046. if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
  13047. xmlFree(ctxt->sax);
  13048. ctxt->sax = sax;
  13049. xmlDetectSAX2(ctxt);
  13050. if (user_data != NULL)
  13051. ctxt->userData = user_data;
  13052. xmlParseDocument(ctxt);
  13053. if (ctxt->wellFormed)
  13054. ret = 0;
  13055. else {
  13056. if (ctxt->errNo != 0)
  13057. ret = ctxt->errNo;
  13058. else
  13059. ret = -1;
  13060. }
  13061. if (sax != NULL)
  13062. ctxt->sax = NULL;
  13063. if (ctxt->myDoc != NULL) {
  13064. xmlFreeDoc(ctxt->myDoc);
  13065. ctxt->myDoc = NULL;
  13066. }
  13067. xmlFreeParserCtxt(ctxt);
  13068. return ret;
  13069. }
  13070. #endif /* LIBXML_SAX1_ENABLED */
  13071. /**
  13072. * xmlCreateDocParserCtxt:
  13073. * @str: a pointer to an array of xmlChar
  13074. *
  13075. * Creates a parser context for an XML in-memory document.
  13076. *
  13077. * Returns the new parser context or NULL
  13078. */
  13079. xmlParserCtxtPtr
  13080. xmlCreateDocParserCtxt(const xmlChar *str) {
  13081. xmlParserCtxtPtr ctxt;
  13082. xmlParserInputPtr input;
  13083. xmlParserInputBufferPtr buf;
  13084. if (str == NULL)
  13085. return(NULL);
  13086. ctxt = xmlNewParserCtxt();
  13087. if (ctxt == NULL)
  13088. return(NULL);
  13089. buf = xmlParserInputBufferCreateString(str);
  13090. if (buf == NULL) {
  13091. xmlFreeParserCtxt(ctxt);
  13092. return(NULL);
  13093. }
  13094. input = xmlNewInputStream(ctxt);
  13095. if (input == NULL) {
  13096. xmlFreeParserInputBuffer(buf);
  13097. xmlFreeParserCtxt(ctxt);
  13098. return(NULL);
  13099. }
  13100. input->filename = NULL;
  13101. input->buf = buf;
  13102. xmlBufResetInput(input->buf->buffer, input);
  13103. inputPush(ctxt, input);
  13104. return(ctxt);
  13105. }
  13106. #ifdef LIBXML_SAX1_ENABLED
  13107. /**
  13108. * xmlSAXParseDoc:
  13109. * @sax: the SAX handler block
  13110. * @cur: a pointer to an array of xmlChar
  13111. * @recovery: work in recovery mode, i.e. tries to read no Well Formed
  13112. * documents
  13113. *
  13114. * DEPRECATED: Use xmlNewSAXParserCtxt and xmlCtxtReadDoc.
  13115. *
  13116. * parse an XML in-memory document and build a tree.
  13117. * It use the given SAX function block to handle the parsing callback.
  13118. * If sax is NULL, fallback to the default DOM tree building routines.
  13119. *
  13120. * Returns the resulting document tree
  13121. */
  13122. xmlDocPtr
  13123. xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) {
  13124. xmlDocPtr ret;
  13125. xmlParserCtxtPtr ctxt;
  13126. xmlSAXHandlerPtr oldsax = NULL;
  13127. if (cur == NULL) return(NULL);
  13128. ctxt = xmlCreateDocParserCtxt(cur);
  13129. if (ctxt == NULL) return(NULL);
  13130. if (sax != NULL) {
  13131. oldsax = ctxt->sax;
  13132. ctxt->sax = sax;
  13133. ctxt->userData = NULL;
  13134. }
  13135. xmlDetectSAX2(ctxt);
  13136. xmlParseDocument(ctxt);
  13137. if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
  13138. else {
  13139. ret = NULL;
  13140. xmlFreeDoc(ctxt->myDoc);
  13141. ctxt->myDoc = NULL;
  13142. }
  13143. if (sax != NULL)
  13144. ctxt->sax = oldsax;
  13145. xmlFreeParserCtxt(ctxt);
  13146. return(ret);
  13147. }
  13148. /**
  13149. * xmlParseDoc:
  13150. * @cur: a pointer to an array of xmlChar
  13151. *
  13152. * DEPRECATED: Use xmlReadDoc.
  13153. *
  13154. * parse an XML in-memory document and build a tree.
  13155. *
  13156. * Returns the resulting document tree
  13157. */
  13158. xmlDocPtr
  13159. xmlParseDoc(const xmlChar *cur) {
  13160. return(xmlSAXParseDoc(NULL, cur, 0));
  13161. }
  13162. #endif /* LIBXML_SAX1_ENABLED */
  13163. #ifdef LIBXML_LEGACY_ENABLED
  13164. /************************************************************************
  13165. * *
  13166. * Specific function to keep track of entities references *
  13167. * and used by the XSLT debugger *
  13168. * *
  13169. ************************************************************************/
  13170. static xmlEntityReferenceFunc xmlEntityRefFunc = NULL;
  13171. /**
  13172. * xmlAddEntityReference:
  13173. * @ent : A valid entity
  13174. * @firstNode : A valid first node for children of entity
  13175. * @lastNode : A valid last node of children entity
  13176. *
  13177. * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY
  13178. */
  13179. static void
  13180. xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
  13181. xmlNodePtr lastNode)
  13182. {
  13183. if (xmlEntityRefFunc != NULL) {
  13184. (*xmlEntityRefFunc) (ent, firstNode, lastNode);
  13185. }
  13186. }
  13187. /**
  13188. * xmlSetEntityReferenceFunc:
  13189. * @func: A valid function
  13190. *
  13191. * Set the function to call call back when a xml reference has been made
  13192. */
  13193. void
  13194. xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func)
  13195. {
  13196. xmlEntityRefFunc = func;
  13197. }
  13198. #endif /* LIBXML_LEGACY_ENABLED */
  13199. /************************************************************************
  13200. * *
  13201. * New set (2.6.0) of simpler and more flexible APIs *
  13202. * *
  13203. ************************************************************************/
  13204. /**
  13205. * DICT_FREE:
  13206. * @str: a string
  13207. *
  13208. * Free a string if it is not owned by the "dict" dictionary in the
  13209. * current scope
  13210. */
  13211. #define DICT_FREE(str) \
  13212. if ((str) && ((!dict) || \
  13213. (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
  13214. xmlFree((char *)(str));
  13215. /**
  13216. * xmlCtxtReset:
  13217. * @ctxt: an XML parser context
  13218. *
  13219. * Reset a parser context
  13220. */
  13221. void
  13222. xmlCtxtReset(xmlParserCtxtPtr ctxt)
  13223. {
  13224. xmlParserInputPtr input;
  13225. xmlDictPtr dict;
  13226. if (ctxt == NULL)
  13227. return;
  13228. dict = ctxt->dict;
  13229. while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
  13230. xmlFreeInputStream(input);
  13231. }
  13232. ctxt->inputNr = 0;
  13233. ctxt->input = NULL;
  13234. ctxt->spaceNr = 0;
  13235. if (ctxt->spaceTab != NULL) {
  13236. ctxt->spaceTab[0] = -1;
  13237. ctxt->space = &ctxt->spaceTab[0];
  13238. } else {
  13239. ctxt->space = NULL;
  13240. }
  13241. ctxt->nodeNr = 0;
  13242. ctxt->node = NULL;
  13243. ctxt->nameNr = 0;
  13244. ctxt->name = NULL;
  13245. ctxt->nsNr = 0;
  13246. xmlParserNsReset(ctxt->nsdb);
  13247. DICT_FREE(ctxt->version);
  13248. ctxt->version = NULL;
  13249. DICT_FREE(ctxt->encoding);
  13250. ctxt->encoding = NULL;
  13251. DICT_FREE(ctxt->directory);
  13252. ctxt->directory = NULL;
  13253. DICT_FREE(ctxt->extSubURI);
  13254. ctxt->extSubURI = NULL;
  13255. DICT_FREE(ctxt->extSubSystem);
  13256. ctxt->extSubSystem = NULL;
  13257. if (ctxt->myDoc != NULL)
  13258. xmlFreeDoc(ctxt->myDoc);
  13259. ctxt->myDoc = NULL;
  13260. ctxt->standalone = -1;
  13261. ctxt->hasExternalSubset = 0;
  13262. ctxt->hasPErefs = 0;
  13263. ctxt->html = 0;
  13264. ctxt->external = 0;
  13265. ctxt->instate = XML_PARSER_START;
  13266. ctxt->token = 0;
  13267. ctxt->wellFormed = 1;
  13268. ctxt->nsWellFormed = 1;
  13269. ctxt->disableSAX = 0;
  13270. ctxt->valid = 1;
  13271. #if 0
  13272. ctxt->vctxt.userData = ctxt;
  13273. ctxt->vctxt.error = xmlParserValidityError;
  13274. ctxt->vctxt.warning = xmlParserValidityWarning;
  13275. #endif
  13276. ctxt->record_info = 0;
  13277. ctxt->checkIndex = 0;
  13278. ctxt->endCheckState = 0;
  13279. ctxt->inSubset = 0;
  13280. ctxt->errNo = XML_ERR_OK;
  13281. ctxt->depth = 0;
  13282. ctxt->catalogs = NULL;
  13283. ctxt->sizeentities = 0;
  13284. ctxt->sizeentcopy = 0;
  13285. xmlInitNodeInfoSeq(&ctxt->node_seq);
  13286. if (ctxt->attsDefault != NULL) {
  13287. xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator);
  13288. ctxt->attsDefault = NULL;
  13289. }
  13290. if (ctxt->attsSpecial != NULL) {
  13291. xmlHashFree(ctxt->attsSpecial, NULL);
  13292. ctxt->attsSpecial = NULL;
  13293. }
  13294. #ifdef LIBXML_CATALOG_ENABLED
  13295. if (ctxt->catalogs != NULL)
  13296. xmlCatalogFreeLocal(ctxt->catalogs);
  13297. #endif
  13298. ctxt->nbErrors = 0;
  13299. ctxt->nbWarnings = 0;
  13300. if (ctxt->lastError.code != XML_ERR_OK)
  13301. xmlResetError(&ctxt->lastError);
  13302. }
  13303. /**
  13304. * xmlCtxtResetPush:
  13305. * @ctxt: an XML parser context
  13306. * @chunk: a pointer to an array of chars
  13307. * @size: number of chars in the array
  13308. * @filename: an optional file name or URI
  13309. * @encoding: the document encoding, or NULL
  13310. *
  13311. * Reset a push parser context
  13312. *
  13313. * Returns 0 in case of success and 1 in case of error
  13314. */
  13315. int
  13316. xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk,
  13317. int size, const char *filename, const char *encoding)
  13318. {
  13319. xmlParserInputPtr inputStream;
  13320. xmlParserInputBufferPtr buf;
  13321. if (ctxt == NULL)
  13322. return(1);
  13323. buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
  13324. if (buf == NULL)
  13325. return(1);
  13326. if (ctxt == NULL) {
  13327. xmlFreeParserInputBuffer(buf);
  13328. return(1);
  13329. }
  13330. xmlCtxtReset(ctxt);
  13331. if (filename == NULL) {
  13332. ctxt->directory = NULL;
  13333. } else {
  13334. ctxt->directory = xmlParserGetDirectory(filename);
  13335. }
  13336. inputStream = xmlNewInputStream(ctxt);
  13337. if (inputStream == NULL) {
  13338. xmlFreeParserInputBuffer(buf);
  13339. return(1);
  13340. }
  13341. if (filename == NULL)
  13342. inputStream->filename = NULL;
  13343. else
  13344. inputStream->filename = (char *)
  13345. xmlCanonicPath((const xmlChar *) filename);
  13346. inputStream->buf = buf;
  13347. xmlBufResetInput(buf->buffer, inputStream);
  13348. inputPush(ctxt, inputStream);
  13349. if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
  13350. (ctxt->input->buf != NULL)) {
  13351. size_t pos = ctxt->input->cur - ctxt->input->base;
  13352. int res;
  13353. res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
  13354. xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
  13355. if (res < 0) {
  13356. xmlFatalErr(ctxt, ctxt->input->buf->error, NULL);
  13357. xmlHaltParser(ctxt);
  13358. return(1);
  13359. }
  13360. }
  13361. if (encoding != NULL) {
  13362. xmlCharEncodingHandlerPtr hdlr;
  13363. hdlr = xmlFindCharEncodingHandler(encoding);
  13364. if (hdlr != NULL) {
  13365. xmlSwitchToEncoding(ctxt, hdlr);
  13366. } else {
  13367. xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  13368. "Unsupported encoding %s\n", BAD_CAST encoding);
  13369. }
  13370. }
  13371. return(0);
  13372. }
  13373. /**
  13374. * xmlCtxtUseOptionsInternal:
  13375. * @ctxt: an XML parser context
  13376. * @options: a combination of xmlParserOption
  13377. * @encoding: the user provided encoding to use
  13378. *
  13379. * Applies the options to the parser context
  13380. *
  13381. * Returns 0 in case of success, the set of unknown or unimplemented options
  13382. * in case of error.
  13383. */
  13384. static int
  13385. xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options)
  13386. {
  13387. if (ctxt == NULL)
  13388. return(-1);
  13389. if (options & XML_PARSE_RECOVER) {
  13390. ctxt->recovery = 1;
  13391. options -= XML_PARSE_RECOVER;
  13392. ctxt->options |= XML_PARSE_RECOVER;
  13393. } else
  13394. ctxt->recovery = 0;
  13395. if (options & XML_PARSE_DTDLOAD) {
  13396. ctxt->loadsubset = XML_DETECT_IDS;
  13397. options -= XML_PARSE_DTDLOAD;
  13398. ctxt->options |= XML_PARSE_DTDLOAD;
  13399. } else
  13400. ctxt->loadsubset = 0;
  13401. if (options & XML_PARSE_DTDATTR) {
  13402. ctxt->loadsubset |= XML_COMPLETE_ATTRS;
  13403. options -= XML_PARSE_DTDATTR;
  13404. ctxt->options |= XML_PARSE_DTDATTR;
  13405. }
  13406. if (options & XML_PARSE_NOENT) {
  13407. ctxt->replaceEntities = 1;
  13408. /* ctxt->loadsubset |= XML_DETECT_IDS; */
  13409. options -= XML_PARSE_NOENT;
  13410. ctxt->options |= XML_PARSE_NOENT;
  13411. } else
  13412. ctxt->replaceEntities = 0;
  13413. if (options & XML_PARSE_PEDANTIC) {
  13414. ctxt->pedantic = 1;
  13415. options -= XML_PARSE_PEDANTIC;
  13416. ctxt->options |= XML_PARSE_PEDANTIC;
  13417. } else
  13418. ctxt->pedantic = 0;
  13419. if (options & XML_PARSE_NOBLANKS) {
  13420. ctxt->keepBlanks = 0;
  13421. ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
  13422. options -= XML_PARSE_NOBLANKS;
  13423. ctxt->options |= XML_PARSE_NOBLANKS;
  13424. } else
  13425. ctxt->keepBlanks = 1;
  13426. if (options & XML_PARSE_DTDVALID) {
  13427. ctxt->validate = 1;
  13428. if (options & XML_PARSE_NOWARNING)
  13429. ctxt->vctxt.warning = NULL;
  13430. if (options & XML_PARSE_NOERROR)
  13431. ctxt->vctxt.error = NULL;
  13432. options -= XML_PARSE_DTDVALID;
  13433. ctxt->options |= XML_PARSE_DTDVALID;
  13434. } else
  13435. ctxt->validate = 0;
  13436. if (options & XML_PARSE_NOWARNING) {
  13437. ctxt->sax->warning = NULL;
  13438. options -= XML_PARSE_NOWARNING;
  13439. }
  13440. if (options & XML_PARSE_NOERROR) {
  13441. ctxt->sax->error = NULL;
  13442. ctxt->sax->fatalError = NULL;
  13443. options -= XML_PARSE_NOERROR;
  13444. }
  13445. #ifdef LIBXML_SAX1_ENABLED
  13446. if (options & XML_PARSE_SAX1) {
  13447. ctxt->sax->startElementNs = NULL;
  13448. ctxt->sax->endElementNs = NULL;
  13449. ctxt->sax->initialized = 1;
  13450. options -= XML_PARSE_SAX1;
  13451. ctxt->options |= XML_PARSE_SAX1;
  13452. }
  13453. #endif /* LIBXML_SAX1_ENABLED */
  13454. if (options & XML_PARSE_NODICT) {
  13455. ctxt->dictNames = 0;
  13456. options -= XML_PARSE_NODICT;
  13457. ctxt->options |= XML_PARSE_NODICT;
  13458. } else {
  13459. ctxt->dictNames = 1;
  13460. }
  13461. if (options & XML_PARSE_NOCDATA) {
  13462. ctxt->sax->cdataBlock = NULL;
  13463. options -= XML_PARSE_NOCDATA;
  13464. ctxt->options |= XML_PARSE_NOCDATA;
  13465. }
  13466. if (options & XML_PARSE_NSCLEAN) {
  13467. ctxt->options |= XML_PARSE_NSCLEAN;
  13468. options -= XML_PARSE_NSCLEAN;
  13469. }
  13470. if (options & XML_PARSE_NONET) {
  13471. ctxt->options |= XML_PARSE_NONET;
  13472. options -= XML_PARSE_NONET;
  13473. }
  13474. if (options & XML_PARSE_COMPACT) {
  13475. ctxt->options |= XML_PARSE_COMPACT;
  13476. options -= XML_PARSE_COMPACT;
  13477. }
  13478. if (options & XML_PARSE_OLD10) {
  13479. ctxt->options |= XML_PARSE_OLD10;
  13480. options -= XML_PARSE_OLD10;
  13481. }
  13482. if (options & XML_PARSE_NOBASEFIX) {
  13483. ctxt->options |= XML_PARSE_NOBASEFIX;
  13484. options -= XML_PARSE_NOBASEFIX;
  13485. }
  13486. if (options & XML_PARSE_HUGE) {
  13487. ctxt->options |= XML_PARSE_HUGE;
  13488. options -= XML_PARSE_HUGE;
  13489. if (ctxt->dict != NULL)
  13490. xmlDictSetLimit(ctxt->dict, 0);
  13491. }
  13492. if (options & XML_PARSE_OLDSAX) {
  13493. ctxt->options |= XML_PARSE_OLDSAX;
  13494. options -= XML_PARSE_OLDSAX;
  13495. }
  13496. if (options & XML_PARSE_IGNORE_ENC) {
  13497. ctxt->options |= XML_PARSE_IGNORE_ENC;
  13498. options -= XML_PARSE_IGNORE_ENC;
  13499. }
  13500. if (options & XML_PARSE_BIG_LINES) {
  13501. ctxt->options |= XML_PARSE_BIG_LINES;
  13502. options -= XML_PARSE_BIG_LINES;
  13503. }
  13504. ctxt->linenumbers = 1;
  13505. return (options);
  13506. }
  13507. /**
  13508. * xmlCtxtUseOptions:
  13509. * @ctxt: an XML parser context
  13510. * @options: a combination of xmlParserOption
  13511. *
  13512. * Applies the options to the parser context
  13513. *
  13514. * Returns 0 in case of success, the set of unknown or unimplemented options
  13515. * in case of error.
  13516. */
  13517. int
  13518. xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options)
  13519. {
  13520. return(xmlCtxtUseOptionsInternal(ctxt, options));
  13521. }
  13522. /**
  13523. * xmlCtxtSetMaxAmplification:
  13524. * @ctxt: an XML parser context
  13525. * @maxAmpl: maximum amplification factor
  13526. *
  13527. * To protect against exponential entity expansion ("billion laughs"), the
  13528. * size of serialized output is (roughly) limited to the input size
  13529. * multiplied by this factor. The default value is 5.
  13530. *
  13531. * When working with documents making heavy use of entity expansion, it can
  13532. * be necessary to increase the value. For security reasons, this should only
  13533. * be considered when processing trusted input.
  13534. */
  13535. void
  13536. xmlCtxtSetMaxAmplification(xmlParserCtxtPtr ctxt, unsigned maxAmpl)
  13537. {
  13538. ctxt->maxAmpl = maxAmpl;
  13539. }
  13540. /**
  13541. * xmlDoRead:
  13542. * @ctxt: an XML parser context
  13543. * @URL: the base URL to use for the document
  13544. * @encoding: the document encoding, or NULL
  13545. * @options: a combination of xmlParserOption
  13546. * @reuse: keep the context for reuse
  13547. *
  13548. * Common front-end for the xmlRead functions
  13549. *
  13550. * Returns the resulting document tree or NULL
  13551. */
  13552. static xmlDocPtr
  13553. xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
  13554. int options, int reuse)
  13555. {
  13556. xmlDocPtr ret;
  13557. xmlCtxtUseOptionsInternal(ctxt, options);
  13558. if (encoding != NULL) {
  13559. xmlCharEncodingHandlerPtr hdlr;
  13560. /*
  13561. * TODO: We should consider to set XML_PARSE_IGNORE_ENC if the
  13562. * caller provided an encoding. Otherwise, we might switch to
  13563. * the encoding from the XML declaration which is likely to
  13564. * break things. Also see xmlSwitchInputEncoding.
  13565. */
  13566. hdlr = xmlFindCharEncodingHandler(encoding);
  13567. if (hdlr != NULL)
  13568. xmlSwitchToEncoding(ctxt, hdlr);
  13569. }
  13570. if ((URL != NULL) && (ctxt->input != NULL) &&
  13571. (ctxt->input->filename == NULL))
  13572. ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
  13573. xmlParseDocument(ctxt);
  13574. if ((ctxt->wellFormed) || ctxt->recovery)
  13575. ret = ctxt->myDoc;
  13576. else {
  13577. ret = NULL;
  13578. if (ctxt->myDoc != NULL) {
  13579. xmlFreeDoc(ctxt->myDoc);
  13580. }
  13581. }
  13582. ctxt->myDoc = NULL;
  13583. if (!reuse) {
  13584. xmlFreeParserCtxt(ctxt);
  13585. }
  13586. return (ret);
  13587. }
  13588. /**
  13589. * xmlReadDoc:
  13590. * @cur: a pointer to a zero terminated string
  13591. * @URL: the base URL to use for the document
  13592. * @encoding: the document encoding, or NULL
  13593. * @options: a combination of xmlParserOption
  13594. *
  13595. * parse an XML in-memory document and build a tree.
  13596. *
  13597. * Returns the resulting document tree
  13598. */
  13599. xmlDocPtr
  13600. xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
  13601. {
  13602. xmlParserCtxtPtr ctxt;
  13603. if (cur == NULL)
  13604. return (NULL);
  13605. xmlInitParser();
  13606. ctxt = xmlCreateDocParserCtxt(cur);
  13607. if (ctxt == NULL)
  13608. return (NULL);
  13609. return (xmlDoRead(ctxt, URL, encoding, options, 0));
  13610. }
  13611. /**
  13612. * xmlReadFile:
  13613. * @filename: a file or URL
  13614. * @encoding: the document encoding, or NULL
  13615. * @options: a combination of xmlParserOption
  13616. *
  13617. * parse an XML file from the filesystem or the network.
  13618. *
  13619. * Returns the resulting document tree
  13620. */
  13621. xmlDocPtr
  13622. xmlReadFile(const char *filename, const char *encoding, int options)
  13623. {
  13624. xmlParserCtxtPtr ctxt;
  13625. xmlInitParser();
  13626. ctxt = xmlCreateURLParserCtxt(filename, options);
  13627. if (ctxt == NULL)
  13628. return (NULL);
  13629. return (xmlDoRead(ctxt, NULL, encoding, options, 0));
  13630. }
  13631. /**
  13632. * xmlReadMemory:
  13633. * @buffer: a pointer to a char array
  13634. * @size: the size of the array
  13635. * @URL: the base URL to use for the document
  13636. * @encoding: the document encoding, or NULL
  13637. * @options: a combination of xmlParserOption
  13638. *
  13639. * parse an XML in-memory document and build a tree.
  13640. *
  13641. * Returns the resulting document tree
  13642. */
  13643. xmlDocPtr
  13644. xmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
  13645. {
  13646. xmlParserCtxtPtr ctxt;
  13647. xmlInitParser();
  13648. ctxt = xmlCreateMemoryParserCtxt(buffer, size);
  13649. if (ctxt == NULL)
  13650. return (NULL);
  13651. return (xmlDoRead(ctxt, URL, encoding, options, 0));
  13652. }
  13653. /**
  13654. * xmlReadFd:
  13655. * @fd: an open file descriptor
  13656. * @URL: the base URL to use for the document
  13657. * @encoding: the document encoding, or NULL
  13658. * @options: a combination of xmlParserOption
  13659. *
  13660. * parse an XML from a file descriptor and build a tree.
  13661. * NOTE that the file descriptor will not be closed when the
  13662. * reader is closed or reset.
  13663. *
  13664. * Returns the resulting document tree
  13665. */
  13666. xmlDocPtr
  13667. xmlReadFd(int fd, const char *URL, const char *encoding, int options)
  13668. {
  13669. xmlParserCtxtPtr ctxt;
  13670. xmlParserInputBufferPtr input;
  13671. xmlParserInputPtr stream;
  13672. if (fd < 0)
  13673. return (NULL);
  13674. xmlInitParser();
  13675. input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
  13676. if (input == NULL)
  13677. return (NULL);
  13678. input->closecallback = NULL;
  13679. ctxt = xmlNewParserCtxt();
  13680. if (ctxt == NULL) {
  13681. xmlFreeParserInputBuffer(input);
  13682. return (NULL);
  13683. }
  13684. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  13685. if (stream == NULL) {
  13686. xmlFreeParserInputBuffer(input);
  13687. xmlFreeParserCtxt(ctxt);
  13688. return (NULL);
  13689. }
  13690. inputPush(ctxt, stream);
  13691. return (xmlDoRead(ctxt, URL, encoding, options, 0));
  13692. }
  13693. /**
  13694. * xmlReadIO:
  13695. * @ioread: an I/O read function
  13696. * @ioclose: an I/O close function
  13697. * @ioctx: an I/O handler
  13698. * @URL: the base URL to use for the document
  13699. * @encoding: the document encoding, or NULL
  13700. * @options: a combination of xmlParserOption
  13701. *
  13702. * parse an XML document from I/O functions and source and build a tree.
  13703. *
  13704. * Returns the resulting document tree
  13705. */
  13706. xmlDocPtr
  13707. xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
  13708. void *ioctx, const char *URL, const char *encoding, int options)
  13709. {
  13710. xmlParserCtxtPtr ctxt;
  13711. xmlParserInputBufferPtr input;
  13712. xmlParserInputPtr stream;
  13713. if (ioread == NULL)
  13714. return (NULL);
  13715. xmlInitParser();
  13716. input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
  13717. XML_CHAR_ENCODING_NONE);
  13718. if (input == NULL) {
  13719. if (ioclose != NULL)
  13720. ioclose(ioctx);
  13721. return (NULL);
  13722. }
  13723. ctxt = xmlNewParserCtxt();
  13724. if (ctxt == NULL) {
  13725. xmlFreeParserInputBuffer(input);
  13726. return (NULL);
  13727. }
  13728. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  13729. if (stream == NULL) {
  13730. xmlFreeParserInputBuffer(input);
  13731. xmlFreeParserCtxt(ctxt);
  13732. return (NULL);
  13733. }
  13734. inputPush(ctxt, stream);
  13735. return (xmlDoRead(ctxt, URL, encoding, options, 0));
  13736. }
  13737. /**
  13738. * xmlCtxtReadDoc:
  13739. * @ctxt: an XML parser context
  13740. * @str: a pointer to a zero terminated string
  13741. * @URL: the base URL to use for the document
  13742. * @encoding: the document encoding, or NULL
  13743. * @options: a combination of xmlParserOption
  13744. *
  13745. * parse an XML in-memory document and build a tree.
  13746. * This reuses the existing @ctxt parser context
  13747. *
  13748. * Returns the resulting document tree
  13749. */
  13750. xmlDocPtr
  13751. xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar *str,
  13752. const char *URL, const char *encoding, int options)
  13753. {
  13754. xmlParserInputBufferPtr input;
  13755. xmlParserInputPtr stream;
  13756. if (ctxt == NULL)
  13757. return (NULL);
  13758. if (str == NULL)
  13759. return (NULL);
  13760. xmlInitParser();
  13761. xmlCtxtReset(ctxt);
  13762. input = xmlParserInputBufferCreateString(str);
  13763. if (input == NULL) {
  13764. return(NULL);
  13765. }
  13766. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  13767. if (stream == NULL) {
  13768. xmlFreeParserInputBuffer(input);
  13769. return(NULL);
  13770. }
  13771. inputPush(ctxt, stream);
  13772. return (xmlDoRead(ctxt, URL, encoding, options, 1));
  13773. }
  13774. /**
  13775. * xmlCtxtReadFile:
  13776. * @ctxt: an XML parser context
  13777. * @filename: a file or URL
  13778. * @encoding: the document encoding, or NULL
  13779. * @options: a combination of xmlParserOption
  13780. *
  13781. * parse an XML file from the filesystem or the network.
  13782. * This reuses the existing @ctxt parser context
  13783. *
  13784. * Returns the resulting document tree
  13785. */
  13786. xmlDocPtr
  13787. xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename,
  13788. const char *encoding, int options)
  13789. {
  13790. xmlParserInputPtr stream;
  13791. if (filename == NULL)
  13792. return (NULL);
  13793. if (ctxt == NULL)
  13794. return (NULL);
  13795. xmlInitParser();
  13796. xmlCtxtReset(ctxt);
  13797. stream = xmlLoadExternalEntity(filename, NULL, ctxt);
  13798. if (stream == NULL) {
  13799. return (NULL);
  13800. }
  13801. inputPush(ctxt, stream);
  13802. return (xmlDoRead(ctxt, NULL, encoding, options, 1));
  13803. }
  13804. /**
  13805. * xmlCtxtReadMemory:
  13806. * @ctxt: an XML parser context
  13807. * @buffer: a pointer to a char array
  13808. * @size: the size of the array
  13809. * @URL: the base URL to use for the document
  13810. * @encoding: the document encoding, or NULL
  13811. * @options: a combination of xmlParserOption
  13812. *
  13813. * parse an XML in-memory document and build a tree.
  13814. * This reuses the existing @ctxt parser context
  13815. *
  13816. * Returns the resulting document tree
  13817. */
  13818. xmlDocPtr
  13819. xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size,
  13820. const char *URL, const char *encoding, int options)
  13821. {
  13822. xmlParserInputBufferPtr input;
  13823. xmlParserInputPtr stream;
  13824. if (ctxt == NULL)
  13825. return (NULL);
  13826. if (buffer == NULL)
  13827. return (NULL);
  13828. xmlInitParser();
  13829. xmlCtxtReset(ctxt);
  13830. input = xmlParserInputBufferCreateStatic(buffer, size,
  13831. XML_CHAR_ENCODING_NONE);
  13832. if (input == NULL) {
  13833. return(NULL);
  13834. }
  13835. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  13836. if (stream == NULL) {
  13837. xmlFreeParserInputBuffer(input);
  13838. return(NULL);
  13839. }
  13840. inputPush(ctxt, stream);
  13841. return (xmlDoRead(ctxt, URL, encoding, options, 1));
  13842. }
  13843. /**
  13844. * xmlCtxtReadFd:
  13845. * @ctxt: an XML parser context
  13846. * @fd: an open file descriptor
  13847. * @URL: the base URL to use for the document
  13848. * @encoding: the document encoding, or NULL
  13849. * @options: a combination of xmlParserOption
  13850. *
  13851. * parse an XML from a file descriptor and build a tree.
  13852. * This reuses the existing @ctxt parser context
  13853. * NOTE that the file descriptor will not be closed when the
  13854. * reader is closed or reset.
  13855. *
  13856. * Returns the resulting document tree
  13857. */
  13858. xmlDocPtr
  13859. xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd,
  13860. const char *URL, const char *encoding, int options)
  13861. {
  13862. xmlParserInputBufferPtr input;
  13863. xmlParserInputPtr stream;
  13864. if (fd < 0)
  13865. return (NULL);
  13866. if (ctxt == NULL)
  13867. return (NULL);
  13868. xmlInitParser();
  13869. xmlCtxtReset(ctxt);
  13870. input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
  13871. if (input == NULL)
  13872. return (NULL);
  13873. input->closecallback = NULL;
  13874. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  13875. if (stream == NULL) {
  13876. xmlFreeParserInputBuffer(input);
  13877. return (NULL);
  13878. }
  13879. inputPush(ctxt, stream);
  13880. return (xmlDoRead(ctxt, URL, encoding, options, 1));
  13881. }
  13882. /**
  13883. * xmlCtxtReadIO:
  13884. * @ctxt: an XML parser context
  13885. * @ioread: an I/O read function
  13886. * @ioclose: an I/O close function
  13887. * @ioctx: an I/O handler
  13888. * @URL: the base URL to use for the document
  13889. * @encoding: the document encoding, or NULL
  13890. * @options: a combination of xmlParserOption
  13891. *
  13892. * parse an XML document from I/O functions and source and build a tree.
  13893. * This reuses the existing @ctxt parser context
  13894. *
  13895. * Returns the resulting document tree
  13896. */
  13897. xmlDocPtr
  13898. xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
  13899. xmlInputCloseCallback ioclose, void *ioctx,
  13900. const char *URL,
  13901. const char *encoding, int options)
  13902. {
  13903. xmlParserInputBufferPtr input;
  13904. xmlParserInputPtr stream;
  13905. if (ioread == NULL)
  13906. return (NULL);
  13907. if (ctxt == NULL)
  13908. return (NULL);
  13909. xmlInitParser();
  13910. xmlCtxtReset(ctxt);
  13911. input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
  13912. XML_CHAR_ENCODING_NONE);
  13913. if (input == NULL) {
  13914. if (ioclose != NULL)
  13915. ioclose(ioctx);
  13916. return (NULL);
  13917. }
  13918. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  13919. if (stream == NULL) {
  13920. xmlFreeParserInputBuffer(input);
  13921. return (NULL);
  13922. }
  13923. inputPush(ctxt, stream);
  13924. return (xmlDoRead(ctxt, URL, encoding, options, 1));
  13925. }