xmlparse.c 205 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830
  1. /* Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
  2. See the file COPYING for copying permission.
  3. 77fea421d361dca90041d0040ecf1dca651167fadf2af79e990e35168d70d933 (2.2.1+)
  4. */
  5. #define _GNU_SOURCE /* syscall prototype */
  6. #include <stddef.h>
  7. #include <string.h> /* memset(), memcpy() */
  8. #include <assert.h>
  9. #include <limits.h> /* UINT_MAX */
  10. #include <stdio.h> /* fprintf */
  11. #include <stdlib.h> /* getenv */
  12. #ifdef _WIN32
  13. #define getpid GetCurrentProcessId
  14. #else
  15. #include <sys/time.h> /* gettimeofday() */
  16. #include <sys/types.h> /* getpid() */
  17. #include <unistd.h> /* getpid() */
  18. #endif
  19. #define XML_BUILDING_EXPAT 1
  20. #ifdef _WIN32
  21. #include "winconfig.h"
  22. #elif defined(HAVE_EXPAT_CONFIG_H)
  23. #include <expat_config.h>
  24. #endif /* ndef _WIN32 */
  25. #include "ascii.h"
  26. #include "expat.h"
  27. #include "siphash.h"
  28. #ifdef XML_UNICODE
  29. #define XML_ENCODE_MAX XML_UTF16_ENCODE_MAX
  30. #define XmlConvert XmlUtf16Convert
  31. #define XmlGetInternalEncoding XmlGetUtf16InternalEncoding
  32. #define XmlGetInternalEncodingNS XmlGetUtf16InternalEncodingNS
  33. #define XmlEncode XmlUtf16Encode
  34. /* Using pointer subtraction to convert to integer type. */
  35. #define MUST_CONVERT(enc, s) (!(enc)->isUtf16 || (((char *)(s) - (char *)NULL) & 1))
  36. typedef unsigned short ICHAR;
  37. #else
  38. #define XML_ENCODE_MAX XML_UTF8_ENCODE_MAX
  39. #define XmlConvert XmlUtf8Convert
  40. #define XmlGetInternalEncoding XmlGetUtf8InternalEncoding
  41. #define XmlGetInternalEncodingNS XmlGetUtf8InternalEncodingNS
  42. #define XmlEncode XmlUtf8Encode
  43. #define MUST_CONVERT(enc, s) (!(enc)->isUtf8)
  44. typedef char ICHAR;
  45. #endif
  46. #ifndef XML_NS
  47. #define XmlInitEncodingNS XmlInitEncoding
  48. #define XmlInitUnknownEncodingNS XmlInitUnknownEncoding
  49. #undef XmlGetInternalEncodingNS
  50. #define XmlGetInternalEncodingNS XmlGetInternalEncoding
  51. #define XmlParseXmlDeclNS XmlParseXmlDecl
  52. #endif
  53. #ifdef XML_UNICODE
  54. #ifdef XML_UNICODE_WCHAR_T
  55. #define XML_T(x) (const wchar_t)x
  56. #define XML_L(x) L ## x
  57. #else
  58. #define XML_T(x) (const unsigned short)x
  59. #define XML_L(x) x
  60. #endif
  61. #else
  62. #define XML_T(x) x
  63. #define XML_L(x) x
  64. #endif
  65. /* Round up n to be a multiple of sz, where sz is a power of 2. */
  66. #define ROUND_UP(n, sz) (((n) + ((sz) - 1)) & ~((sz) - 1))
  67. /* Handle the case where memmove() doesn't exist. */
  68. #ifndef HAVE_MEMMOVE
  69. #ifdef HAVE_BCOPY
  70. #define memmove(d,s,l) bcopy((s),(d),(l))
  71. #else
  72. #error memmove does not exist on this platform, nor is a substitute available
  73. #endif /* HAVE_BCOPY */
  74. #endif /* HAVE_MEMMOVE */
  75. #include "internal.h"
  76. #include "xmltok.h"
  77. #include "xmlrole.h"
  78. typedef const XML_Char *KEY;
  79. typedef struct {
  80. KEY name;
  81. } NAMED;
  82. typedef struct {
  83. NAMED **v;
  84. unsigned char power;
  85. size_t size;
  86. size_t used;
  87. const XML_Memory_Handling_Suite *mem;
  88. } HASH_TABLE;
  89. static size_t
  90. keylen(KEY s);
  91. static void
  92. copy_salt_to_sipkey(XML_Parser parser, struct sipkey * key);
  93. /* For probing (after a collision) we need a step size relative prime
  94. to the hash table size, which is a power of 2. We use double-hashing,
  95. since we can calculate a second hash value cheaply by taking those bits
  96. of the first hash value that were discarded (masked out) when the table
  97. index was calculated: index = hash & mask, where mask = table->size - 1.
  98. We limit the maximum step size to table->size / 4 (mask >> 2) and make
  99. it odd, since odd numbers are always relative prime to a power of 2.
  100. */
  101. #define SECOND_HASH(hash, mask, power) \
  102. ((((hash) & ~(mask)) >> ((power) - 1)) & ((mask) >> 2))
  103. #define PROBE_STEP(hash, mask, power) \
  104. ((unsigned char)((SECOND_HASH(hash, mask, power)) | 1))
  105. typedef struct {
  106. NAMED **p;
  107. NAMED **end;
  108. } HASH_TABLE_ITER;
  109. #define INIT_TAG_BUF_SIZE 32 /* must be a multiple of sizeof(XML_Char) */
  110. #define INIT_DATA_BUF_SIZE 1024
  111. #define INIT_ATTS_SIZE 16
  112. #define INIT_ATTS_VERSION 0xFFFFFFFF
  113. #define INIT_BLOCK_SIZE 1024
  114. #define INIT_BUFFER_SIZE 1024
  115. #define EXPAND_SPARE 24
  116. typedef struct binding {
  117. struct prefix *prefix;
  118. struct binding *nextTagBinding;
  119. struct binding *prevPrefixBinding;
  120. const struct attribute_id *attId;
  121. XML_Char *uri;
  122. int uriLen;
  123. int uriAlloc;
  124. } BINDING;
  125. typedef struct prefix {
  126. const XML_Char *name;
  127. BINDING *binding;
  128. } PREFIX;
  129. typedef struct {
  130. const XML_Char *str;
  131. const XML_Char *localPart;
  132. const XML_Char *prefix;
  133. int strLen;
  134. int uriLen;
  135. int prefixLen;
  136. } TAG_NAME;
  137. /* TAG represents an open element.
  138. The name of the element is stored in both the document and API
  139. encodings. The memory buffer 'buf' is a separately-allocated
  140. memory area which stores the name. During the XML_Parse()/
  141. XMLParseBuffer() when the element is open, the memory for the 'raw'
  142. version of the name (in the document encoding) is shared with the
  143. document buffer. If the element is open across calls to
  144. XML_Parse()/XML_ParseBuffer(), the buffer is re-allocated to
  145. contain the 'raw' name as well.
  146. A parser re-uses these structures, maintaining a list of allocated
  147. TAG objects in a free list.
  148. */
  149. typedef struct tag {
  150. struct tag *parent; /* parent of this element */
  151. const char *rawName; /* tagName in the original encoding */
  152. int rawNameLength;
  153. TAG_NAME name; /* tagName in the API encoding */
  154. char *buf; /* buffer for name components */
  155. char *bufEnd; /* end of the buffer */
  156. BINDING *bindings;
  157. } TAG;
  158. typedef struct {
  159. const XML_Char *name;
  160. const XML_Char *textPtr;
  161. int textLen; /* length in XML_Chars */
  162. int processed; /* # of processed bytes - when suspended */
  163. const XML_Char *systemId;
  164. const XML_Char *base;
  165. const XML_Char *publicId;
  166. const XML_Char *notation;
  167. XML_Bool open;
  168. XML_Bool is_param;
  169. XML_Bool is_internal; /* true if declared in internal subset outside PE */
  170. } ENTITY;
  171. typedef struct {
  172. enum XML_Content_Type type;
  173. enum XML_Content_Quant quant;
  174. const XML_Char * name;
  175. int firstchild;
  176. int lastchild;
  177. int childcnt;
  178. int nextsib;
  179. } CONTENT_SCAFFOLD;
  180. #define INIT_SCAFFOLD_ELEMENTS 32
  181. typedef struct block {
  182. struct block *next;
  183. int size;
  184. XML_Char s[1];
  185. } BLOCK;
  186. typedef struct {
  187. BLOCK *blocks;
  188. BLOCK *freeBlocks;
  189. const XML_Char *end;
  190. XML_Char *ptr;
  191. XML_Char *start;
  192. const XML_Memory_Handling_Suite *mem;
  193. } STRING_POOL;
  194. /* The XML_Char before the name is used to determine whether
  195. an attribute has been specified. */
  196. typedef struct attribute_id {
  197. XML_Char *name;
  198. PREFIX *prefix;
  199. XML_Bool maybeTokenized;
  200. XML_Bool xmlns;
  201. } ATTRIBUTE_ID;
  202. typedef struct {
  203. const ATTRIBUTE_ID *id;
  204. XML_Bool isCdata;
  205. const XML_Char *value;
  206. } DEFAULT_ATTRIBUTE;
  207. typedef struct {
  208. unsigned long version;
  209. unsigned long hash;
  210. const XML_Char *uriName;
  211. } NS_ATT;
  212. typedef struct {
  213. const XML_Char *name;
  214. PREFIX *prefix;
  215. const ATTRIBUTE_ID *idAtt;
  216. int nDefaultAtts;
  217. int allocDefaultAtts;
  218. DEFAULT_ATTRIBUTE *defaultAtts;
  219. } ELEMENT_TYPE;
  220. typedef struct {
  221. HASH_TABLE generalEntities;
  222. HASH_TABLE elementTypes;
  223. HASH_TABLE attributeIds;
  224. HASH_TABLE prefixes;
  225. STRING_POOL pool;
  226. STRING_POOL entityValuePool;
  227. /* false once a parameter entity reference has been skipped */
  228. XML_Bool keepProcessing;
  229. /* true once an internal or external PE reference has been encountered;
  230. this includes the reference to an external subset */
  231. XML_Bool hasParamEntityRefs;
  232. XML_Bool standalone;
  233. #ifdef XML_DTD
  234. /* indicates if external PE has been read */
  235. XML_Bool paramEntityRead;
  236. HASH_TABLE paramEntities;
  237. #endif /* XML_DTD */
  238. PREFIX defaultPrefix;
  239. /* === scaffolding for building content model === */
  240. XML_Bool in_eldecl;
  241. CONTENT_SCAFFOLD *scaffold;
  242. unsigned contentStringLen;
  243. unsigned scaffSize;
  244. unsigned scaffCount;
  245. int scaffLevel;
  246. int *scaffIndex;
  247. } DTD;
  248. typedef struct open_internal_entity {
  249. const char *internalEventPtr;
  250. const char *internalEventEndPtr;
  251. struct open_internal_entity *next;
  252. ENTITY *entity;
  253. int startTagLevel;
  254. XML_Bool betweenDecl; /* WFC: PE Between Declarations */
  255. } OPEN_INTERNAL_ENTITY;
  256. typedef enum XML_Error PTRCALL Processor(XML_Parser parser,
  257. const char *start,
  258. const char *end,
  259. const char **endPtr);
  260. static Processor prologProcessor;
  261. static Processor prologInitProcessor;
  262. static Processor contentProcessor;
  263. static Processor cdataSectionProcessor;
  264. #ifdef XML_DTD
  265. static Processor ignoreSectionProcessor;
  266. static Processor externalParEntProcessor;
  267. static Processor externalParEntInitProcessor;
  268. static Processor entityValueProcessor;
  269. static Processor entityValueInitProcessor;
  270. #endif /* XML_DTD */
  271. static Processor epilogProcessor;
  272. static Processor errorProcessor;
  273. static Processor externalEntityInitProcessor;
  274. static Processor externalEntityInitProcessor2;
  275. static Processor externalEntityInitProcessor3;
  276. static Processor externalEntityContentProcessor;
  277. static Processor internalEntityProcessor;
  278. static enum XML_Error
  279. handleUnknownEncoding(XML_Parser parser, const XML_Char *encodingName);
  280. static enum XML_Error
  281. processXmlDecl(XML_Parser parser, int isGeneralTextEntity,
  282. const char *s, const char *next);
  283. static enum XML_Error
  284. initializeEncoding(XML_Parser parser);
  285. static enum XML_Error
  286. doProlog(XML_Parser parser, const ENCODING *enc, const char *s,
  287. const char *end, int tok, const char *next, const char **nextPtr,
  288. XML_Bool haveMore);
  289. static enum XML_Error
  290. processInternalEntity(XML_Parser parser, ENTITY *entity,
  291. XML_Bool betweenDecl);
  292. static enum XML_Error
  293. doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc,
  294. const char *start, const char *end, const char **endPtr,
  295. XML_Bool haveMore);
  296. static enum XML_Error
  297. doCdataSection(XML_Parser parser, const ENCODING *, const char **startPtr,
  298. const char *end, const char **nextPtr, XML_Bool haveMore);
  299. #ifdef XML_DTD
  300. static enum XML_Error
  301. doIgnoreSection(XML_Parser parser, const ENCODING *, const char **startPtr,
  302. const char *end, const char **nextPtr, XML_Bool haveMore);
  303. #endif /* XML_DTD */
  304. static void
  305. freeBindings(XML_Parser parser, BINDING *bindings);
  306. static enum XML_Error
  307. storeAtts(XML_Parser parser, const ENCODING *, const char *s,
  308. TAG_NAME *tagNamePtr, BINDING **bindingsPtr);
  309. static enum XML_Error
  310. addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
  311. const XML_Char *uri, BINDING **bindingsPtr);
  312. static int
  313. defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *, XML_Bool isCdata,
  314. XML_Bool isId, const XML_Char *dfltValue, XML_Parser parser);
  315. static enum XML_Error
  316. storeAttributeValue(XML_Parser parser, const ENCODING *, XML_Bool isCdata,
  317. const char *, const char *, STRING_POOL *);
  318. static enum XML_Error
  319. appendAttributeValue(XML_Parser parser, const ENCODING *, XML_Bool isCdata,
  320. const char *, const char *, STRING_POOL *);
  321. static ATTRIBUTE_ID *
  322. getAttributeId(XML_Parser parser, const ENCODING *enc, const char *start,
  323. const char *end);
  324. static int
  325. setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *);
  326. static enum XML_Error
  327. storeEntityValue(XML_Parser parser, const ENCODING *enc, const char *start,
  328. const char *end);
  329. static int
  330. reportProcessingInstruction(XML_Parser parser, const ENCODING *enc,
  331. const char *start, const char *end);
  332. static int
  333. reportComment(XML_Parser parser, const ENCODING *enc, const char *start,
  334. const char *end);
  335. static void
  336. reportDefault(XML_Parser parser, const ENCODING *enc, const char *start,
  337. const char *end);
  338. static const XML_Char * getContext(XML_Parser parser);
  339. static XML_Bool
  340. setContext(XML_Parser parser, const XML_Char *context);
  341. static void FASTCALL normalizePublicId(XML_Char *s);
  342. static DTD * dtdCreate(const XML_Memory_Handling_Suite *ms);
  343. /* do not call if parentParser != NULL */
  344. static void dtdReset(DTD *p, const XML_Memory_Handling_Suite *ms);
  345. static void
  346. dtdDestroy(DTD *p, XML_Bool isDocEntity, const XML_Memory_Handling_Suite *ms);
  347. static int
  348. dtdCopy(XML_Parser oldParser,
  349. DTD *newDtd, const DTD *oldDtd, const XML_Memory_Handling_Suite *ms);
  350. static int
  351. copyEntityTable(XML_Parser oldParser,
  352. HASH_TABLE *, STRING_POOL *, const HASH_TABLE *);
  353. static NAMED *
  354. lookup(XML_Parser parser, HASH_TABLE *table, KEY name, size_t createSize);
  355. static void FASTCALL
  356. hashTableInit(HASH_TABLE *, const XML_Memory_Handling_Suite *ms);
  357. static void FASTCALL hashTableClear(HASH_TABLE *);
  358. static void FASTCALL hashTableDestroy(HASH_TABLE *);
  359. static void FASTCALL
  360. hashTableIterInit(HASH_TABLE_ITER *, const HASH_TABLE *);
  361. static NAMED * FASTCALL hashTableIterNext(HASH_TABLE_ITER *);
  362. static void FASTCALL
  363. poolInit(STRING_POOL *, const XML_Memory_Handling_Suite *ms);
  364. static void FASTCALL poolClear(STRING_POOL *);
  365. static void FASTCALL poolDestroy(STRING_POOL *);
  366. static XML_Char *
  367. poolAppend(STRING_POOL *pool, const ENCODING *enc,
  368. const char *ptr, const char *end);
  369. static XML_Char *
  370. poolStoreString(STRING_POOL *pool, const ENCODING *enc,
  371. const char *ptr, const char *end);
  372. static XML_Bool FASTCALL poolGrow(STRING_POOL *pool);
  373. static const XML_Char * FASTCALL
  374. poolCopyString(STRING_POOL *pool, const XML_Char *s);
  375. static const XML_Char *
  376. poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n);
  377. static const XML_Char * FASTCALL
  378. poolAppendString(STRING_POOL *pool, const XML_Char *s);
  379. static int FASTCALL nextScaffoldPart(XML_Parser parser);
  380. static XML_Content * build_model(XML_Parser parser);
  381. static ELEMENT_TYPE *
  382. getElementType(XML_Parser parser, const ENCODING *enc,
  383. const char *ptr, const char *end);
  384. static unsigned long generate_hash_secret_salt(XML_Parser parser);
  385. static XML_Bool startParsing(XML_Parser parser);
  386. static XML_Parser
  387. parserCreate(const XML_Char *encodingName,
  388. const XML_Memory_Handling_Suite *memsuite,
  389. const XML_Char *nameSep,
  390. DTD *dtd);
  391. static void
  392. parserInit(XML_Parser parser, const XML_Char *encodingName);
  393. #define poolStart(pool) ((pool)->start)
  394. #define poolEnd(pool) ((pool)->ptr)
  395. #define poolLength(pool) ((pool)->ptr - (pool)->start)
  396. #define poolChop(pool) ((void)--(pool->ptr))
  397. #define poolLastChar(pool) (((pool)->ptr)[-1])
  398. #define poolDiscard(pool) ((pool)->ptr = (pool)->start)
  399. #define poolFinish(pool) ((pool)->start = (pool)->ptr)
  400. #define poolAppendChar(pool, c) \
  401. (((pool)->ptr == (pool)->end && !poolGrow(pool)) \
  402. ? 0 \
  403. : ((*((pool)->ptr)++ = c), 1))
  404. struct XML_ParserStruct {
  405. /* The first member must be userData so that the XML_GetUserData
  406. macro works. */
  407. void *m_userData;
  408. void *m_handlerArg;
  409. char *m_buffer;
  410. const XML_Memory_Handling_Suite m_mem;
  411. /* first character to be parsed */
  412. const char *m_bufferPtr;
  413. /* past last character to be parsed */
  414. char *m_bufferEnd;
  415. /* allocated end of buffer */
  416. const char *m_bufferLim;
  417. XML_Index m_parseEndByteIndex;
  418. const char *m_parseEndPtr;
  419. XML_Char *m_dataBuf;
  420. XML_Char *m_dataBufEnd;
  421. XML_StartElementHandler m_startElementHandler;
  422. XML_EndElementHandler m_endElementHandler;
  423. XML_CharacterDataHandler m_characterDataHandler;
  424. XML_ProcessingInstructionHandler m_processingInstructionHandler;
  425. XML_CommentHandler m_commentHandler;
  426. XML_StartCdataSectionHandler m_startCdataSectionHandler;
  427. XML_EndCdataSectionHandler m_endCdataSectionHandler;
  428. XML_DefaultHandler m_defaultHandler;
  429. XML_StartDoctypeDeclHandler m_startDoctypeDeclHandler;
  430. XML_EndDoctypeDeclHandler m_endDoctypeDeclHandler;
  431. XML_UnparsedEntityDeclHandler m_unparsedEntityDeclHandler;
  432. XML_NotationDeclHandler m_notationDeclHandler;
  433. XML_StartNamespaceDeclHandler m_startNamespaceDeclHandler;
  434. XML_EndNamespaceDeclHandler m_endNamespaceDeclHandler;
  435. XML_NotStandaloneHandler m_notStandaloneHandler;
  436. XML_ExternalEntityRefHandler m_externalEntityRefHandler;
  437. XML_Parser m_externalEntityRefHandlerArg;
  438. XML_SkippedEntityHandler m_skippedEntityHandler;
  439. XML_UnknownEncodingHandler m_unknownEncodingHandler;
  440. XML_ElementDeclHandler m_elementDeclHandler;
  441. XML_AttlistDeclHandler m_attlistDeclHandler;
  442. XML_EntityDeclHandler m_entityDeclHandler;
  443. XML_XmlDeclHandler m_xmlDeclHandler;
  444. const ENCODING *m_encoding;
  445. INIT_ENCODING m_initEncoding;
  446. const ENCODING *m_internalEncoding;
  447. const XML_Char *m_protocolEncodingName;
  448. XML_Bool m_ns;
  449. XML_Bool m_ns_triplets;
  450. void *m_unknownEncodingMem;
  451. void *m_unknownEncodingData;
  452. void *m_unknownEncodingHandlerData;
  453. void (XMLCALL *m_unknownEncodingRelease)(void *);
  454. PROLOG_STATE m_prologState;
  455. Processor *m_processor;
  456. enum XML_Error m_errorCode;
  457. const char *m_eventPtr;
  458. const char *m_eventEndPtr;
  459. const char *m_positionPtr;
  460. OPEN_INTERNAL_ENTITY *m_openInternalEntities;
  461. OPEN_INTERNAL_ENTITY *m_freeInternalEntities;
  462. XML_Bool m_defaultExpandInternalEntities;
  463. int m_tagLevel;
  464. ENTITY *m_declEntity;
  465. const XML_Char *m_doctypeName;
  466. const XML_Char *m_doctypeSysid;
  467. const XML_Char *m_doctypePubid;
  468. const XML_Char *m_declAttributeType;
  469. const XML_Char *m_declNotationName;
  470. const XML_Char *m_declNotationPublicId;
  471. ELEMENT_TYPE *m_declElementType;
  472. ATTRIBUTE_ID *m_declAttributeId;
  473. XML_Bool m_declAttributeIsCdata;
  474. XML_Bool m_declAttributeIsId;
  475. DTD *m_dtd;
  476. const XML_Char *m_curBase;
  477. TAG *m_tagStack;
  478. TAG *m_freeTagList;
  479. BINDING *m_inheritedBindings;
  480. BINDING *m_freeBindingList;
  481. int m_attsSize;
  482. int m_nSpecifiedAtts;
  483. int m_idAttIndex;
  484. ATTRIBUTE *m_atts;
  485. NS_ATT *m_nsAtts;
  486. unsigned long m_nsAttsVersion;
  487. unsigned char m_nsAttsPower;
  488. #ifdef XML_ATTR_INFO
  489. XML_AttrInfo *m_attInfo;
  490. #endif
  491. POSITION m_position;
  492. STRING_POOL m_tempPool;
  493. STRING_POOL m_temp2Pool;
  494. char *m_groupConnector;
  495. unsigned int m_groupSize;
  496. XML_Char m_namespaceSeparator;
  497. XML_Parser m_parentParser;
  498. XML_ParsingStatus m_parsingStatus;
  499. #ifdef XML_DTD
  500. XML_Bool m_isParamEntity;
  501. XML_Bool m_useForeignDTD;
  502. enum XML_ParamEntityParsing m_paramEntityParsing;
  503. #endif
  504. unsigned long m_hash_secret_salt;
  505. };
  506. #define MALLOC(s) (parser->m_mem.malloc_fcn((s)))
  507. #define REALLOC(p,s) (parser->m_mem.realloc_fcn((p),(s)))
  508. #define FREE(p) (parser->m_mem.free_fcn((p)))
  509. #define userData (parser->m_userData)
  510. #define handlerArg (parser->m_handlerArg)
  511. #define startElementHandler (parser->m_startElementHandler)
  512. #define endElementHandler (parser->m_endElementHandler)
  513. #define characterDataHandler (parser->m_characterDataHandler)
  514. #define processingInstructionHandler \
  515. (parser->m_processingInstructionHandler)
  516. #define commentHandler (parser->m_commentHandler)
  517. #define startCdataSectionHandler \
  518. (parser->m_startCdataSectionHandler)
  519. #define endCdataSectionHandler (parser->m_endCdataSectionHandler)
  520. #define defaultHandler (parser->m_defaultHandler)
  521. #define startDoctypeDeclHandler (parser->m_startDoctypeDeclHandler)
  522. #define endDoctypeDeclHandler (parser->m_endDoctypeDeclHandler)
  523. #define unparsedEntityDeclHandler \
  524. (parser->m_unparsedEntityDeclHandler)
  525. #define notationDeclHandler (parser->m_notationDeclHandler)
  526. #define startNamespaceDeclHandler \
  527. (parser->m_startNamespaceDeclHandler)
  528. #define endNamespaceDeclHandler (parser->m_endNamespaceDeclHandler)
  529. #define notStandaloneHandler (parser->m_notStandaloneHandler)
  530. #define externalEntityRefHandler \
  531. (parser->m_externalEntityRefHandler)
  532. #define externalEntityRefHandlerArg \
  533. (parser->m_externalEntityRefHandlerArg)
  534. #define internalEntityRefHandler \
  535. (parser->m_internalEntityRefHandler)
  536. #define skippedEntityHandler (parser->m_skippedEntityHandler)
  537. #define unknownEncodingHandler (parser->m_unknownEncodingHandler)
  538. #define elementDeclHandler (parser->m_elementDeclHandler)
  539. #define attlistDeclHandler (parser->m_attlistDeclHandler)
  540. #define entityDeclHandler (parser->m_entityDeclHandler)
  541. #define xmlDeclHandler (parser->m_xmlDeclHandler)
  542. #define encoding (parser->m_encoding)
  543. #define initEncoding (parser->m_initEncoding)
  544. #define internalEncoding (parser->m_internalEncoding)
  545. #define unknownEncodingMem (parser->m_unknownEncodingMem)
  546. #define unknownEncodingData (parser->m_unknownEncodingData)
  547. #define unknownEncodingHandlerData \
  548. (parser->m_unknownEncodingHandlerData)
  549. #define unknownEncodingRelease (parser->m_unknownEncodingRelease)
  550. #define protocolEncodingName (parser->m_protocolEncodingName)
  551. #define ns (parser->m_ns)
  552. #define ns_triplets (parser->m_ns_triplets)
  553. #define prologState (parser->m_prologState)
  554. #define processor (parser->m_processor)
  555. #define errorCode (parser->m_errorCode)
  556. #define eventPtr (parser->m_eventPtr)
  557. #define eventEndPtr (parser->m_eventEndPtr)
  558. #define positionPtr (parser->m_positionPtr)
  559. #define position (parser->m_position)
  560. #define openInternalEntities (parser->m_openInternalEntities)
  561. #define freeInternalEntities (parser->m_freeInternalEntities)
  562. #define defaultExpandInternalEntities \
  563. (parser->m_defaultExpandInternalEntities)
  564. #define tagLevel (parser->m_tagLevel)
  565. #define buffer (parser->m_buffer)
  566. #define bufferPtr (parser->m_bufferPtr)
  567. #define bufferEnd (parser->m_bufferEnd)
  568. #define parseEndByteIndex (parser->m_parseEndByteIndex)
  569. #define parseEndPtr (parser->m_parseEndPtr)
  570. #define bufferLim (parser->m_bufferLim)
  571. #define dataBuf (parser->m_dataBuf)
  572. #define dataBufEnd (parser->m_dataBufEnd)
  573. #define _dtd (parser->m_dtd)
  574. #define curBase (parser->m_curBase)
  575. #define declEntity (parser->m_declEntity)
  576. #define doctypeName (parser->m_doctypeName)
  577. #define doctypeSysid (parser->m_doctypeSysid)
  578. #define doctypePubid (parser->m_doctypePubid)
  579. #define declAttributeType (parser->m_declAttributeType)
  580. #define declNotationName (parser->m_declNotationName)
  581. #define declNotationPublicId (parser->m_declNotationPublicId)
  582. #define declElementType (parser->m_declElementType)
  583. #define declAttributeId (parser->m_declAttributeId)
  584. #define declAttributeIsCdata (parser->m_declAttributeIsCdata)
  585. #define declAttributeIsId (parser->m_declAttributeIsId)
  586. #define freeTagList (parser->m_freeTagList)
  587. #define freeBindingList (parser->m_freeBindingList)
  588. #define inheritedBindings (parser->m_inheritedBindings)
  589. #define tagStack (parser->m_tagStack)
  590. #define atts (parser->m_atts)
  591. #define attsSize (parser->m_attsSize)
  592. #define nSpecifiedAtts (parser->m_nSpecifiedAtts)
  593. #define idAttIndex (parser->m_idAttIndex)
  594. #define nsAtts (parser->m_nsAtts)
  595. #define nsAttsVersion (parser->m_nsAttsVersion)
  596. #define nsAttsPower (parser->m_nsAttsPower)
  597. #define attInfo (parser->m_attInfo)
  598. #define tempPool (parser->m_tempPool)
  599. #define temp2Pool (parser->m_temp2Pool)
  600. #define groupConnector (parser->m_groupConnector)
  601. #define groupSize (parser->m_groupSize)
  602. #define namespaceSeparator (parser->m_namespaceSeparator)
  603. #define parentParser (parser->m_parentParser)
  604. #define ps_parsing (parser->m_parsingStatus.parsing)
  605. #define ps_finalBuffer (parser->m_parsingStatus.finalBuffer)
  606. #ifdef XML_DTD
  607. #define isParamEntity (parser->m_isParamEntity)
  608. #define useForeignDTD (parser->m_useForeignDTD)
  609. #define paramEntityParsing (parser->m_paramEntityParsing)
  610. #endif /* XML_DTD */
  611. #define hash_secret_salt (parser->m_hash_secret_salt)
  612. XML_Parser XMLCALL
  613. XML_ParserCreate(const XML_Char *encodingName)
  614. {
  615. return XML_ParserCreate_MM(encodingName, NULL, NULL);
  616. }
  617. XML_Parser XMLCALL
  618. XML_ParserCreateNS(const XML_Char *encodingName, XML_Char nsSep)
  619. {
  620. XML_Char tmp[2];
  621. *tmp = nsSep;
  622. return XML_ParserCreate_MM(encodingName, NULL, tmp);
  623. }
  624. static const XML_Char implicitContext[] = {
  625. ASCII_x, ASCII_m, ASCII_l, ASCII_EQUALS, ASCII_h, ASCII_t, ASCII_t, ASCII_p,
  626. ASCII_COLON, ASCII_SLASH, ASCII_SLASH, ASCII_w, ASCII_w, ASCII_w,
  627. ASCII_PERIOD, ASCII_w, ASCII_3, ASCII_PERIOD, ASCII_o, ASCII_r, ASCII_g,
  628. ASCII_SLASH, ASCII_X, ASCII_M, ASCII_L, ASCII_SLASH, ASCII_1, ASCII_9,
  629. ASCII_9, ASCII_8, ASCII_SLASH, ASCII_n, ASCII_a, ASCII_m, ASCII_e,
  630. ASCII_s, ASCII_p, ASCII_a, ASCII_c, ASCII_e, '\0'
  631. };
  632. #if defined(HAVE_GETRANDOM) || defined(HAVE_SYSCALL_GETRANDOM)
  633. # include <errno.h>
  634. # if defined(HAVE_GETRANDOM)
  635. # include <sys/random.h> /* getrandom */
  636. # else
  637. # include <unistd.h> /* syscall */
  638. # include <sys/syscall.h> /* SYS_getrandom */
  639. # endif
  640. /* Obtain entropy on Linux 3.17+ */
  641. static int
  642. writeRandomBytes_getrandom(void * target, size_t count) {
  643. int success = 0; /* full count bytes written? */
  644. size_t bytesWrittenTotal = 0;
  645. const unsigned int getrandomFlags = 0;
  646. do {
  647. void * const currentTarget = (void*)((char*)target + bytesWrittenTotal);
  648. const size_t bytesToWrite = count - bytesWrittenTotal;
  649. const int bytesWrittenMore =
  650. #if defined(HAVE_GETRANDOM)
  651. getrandom(currentTarget, bytesToWrite, getrandomFlags);
  652. #else
  653. syscall(SYS_getrandom, currentTarget, bytesToWrite, getrandomFlags);
  654. #endif
  655. if (bytesWrittenMore > 0) {
  656. bytesWrittenTotal += bytesWrittenMore;
  657. if (bytesWrittenTotal >= count)
  658. success = 1;
  659. }
  660. } while (! success && (errno == EINTR || errno == EAGAIN));
  661. return success;
  662. }
  663. #endif /* defined(HAVE_GETRANDOM) || defined(HAVE_SYSCALL_GETRANDOM) */
  664. #ifdef _WIN32
  665. typedef BOOLEAN (APIENTRY *RTLGENRANDOM_FUNC)(PVOID, ULONG);
  666. /* Obtain entropy on Windows XP / Windows Server 2003 and later.
  667. * Hint on RtlGenRandom and the following article from libsodioum.
  668. *
  669. * Michael Howard: Cryptographically Secure Random number on Windows without using CryptoAPI
  670. * https://blogs.msdn.microsoft.com/michael_howard/2005/01/14/cryptographically-secure-random-number-on-windows-without-using-cryptoapi/
  671. */
  672. static int
  673. writeRandomBytes_RtlGenRandom(void * target, size_t count) {
  674. int success = 0; /* full count bytes written? */
  675. const HMODULE advapi32 = LoadLibrary("ADVAPI32.DLL");
  676. if (advapi32) {
  677. const RTLGENRANDOM_FUNC RtlGenRandom
  678. = (RTLGENRANDOM_FUNC)GetProcAddress(advapi32, "SystemFunction036");
  679. if (RtlGenRandom) {
  680. if (RtlGenRandom((PVOID)target, (ULONG)count) == TRUE) {
  681. success = 1;
  682. }
  683. }
  684. FreeLibrary(advapi32);
  685. }
  686. return success;
  687. }
  688. #endif /* _WIN32 */
  689. static unsigned long
  690. gather_time_entropy(void)
  691. {
  692. #ifdef _WIN32
  693. FILETIME ft;
  694. GetSystemTimeAsFileTime(&ft); /* never fails */
  695. return ft.dwHighDateTime ^ ft.dwLowDateTime;
  696. #else
  697. struct timeval tv;
  698. int gettimeofday_res;
  699. gettimeofday_res = gettimeofday(&tv, NULL);
  700. assert (gettimeofday_res == 0);
  701. /* Microseconds time is <20 bits entropy */
  702. return tv.tv_usec;
  703. #endif
  704. }
  705. #if defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_LIBBSD)
  706. # include <bsd/stdlib.h>
  707. #endif
  708. static unsigned long
  709. ENTROPY_DEBUG(const char * label, unsigned long entropy) {
  710. const char * const EXPAT_ENTROPY_DEBUG = getenv("EXPAT_ENTROPY_DEBUG");
  711. if (EXPAT_ENTROPY_DEBUG && ! strcmp(EXPAT_ENTROPY_DEBUG, "1")) {
  712. fprintf(stderr, "Entropy: %s --> 0x%0*lx (%lu bytes)\n",
  713. label,
  714. (int)sizeof(entropy) * 2, entropy,
  715. (unsigned long)sizeof(entropy));
  716. }
  717. return entropy;
  718. }
  719. static unsigned long
  720. generate_hash_secret_salt(XML_Parser parser)
  721. {
  722. unsigned long entropy;
  723. (void)parser;
  724. #if defined(HAVE_ARC4RANDOM_BUF) || defined(__CloudABI__)
  725. (void)gather_time_entropy;
  726. arc4random_buf(&entropy, sizeof(entropy));
  727. return ENTROPY_DEBUG("arc4random_buf", entropy);
  728. #else
  729. /* Try high quality providers first .. */
  730. #ifdef _WIN32
  731. if (writeRandomBytes_RtlGenRandom((void *)&entropy, sizeof(entropy))) {
  732. return ENTROPY_DEBUG("RtlGenRandom", entropy);
  733. }
  734. #elif defined(HAVE_GETRANDOM) || defined(HAVE_SYSCALL_GETRANDOM)
  735. if (writeRandomBytes_getrandom((void *)&entropy, sizeof(entropy))) {
  736. return ENTROPY_DEBUG("getrandom", entropy);
  737. }
  738. #endif
  739. /* .. and self-made low quality for backup: */
  740. /* Process ID is 0 bits entropy if attacker has local access */
  741. entropy = gather_time_entropy() ^ getpid();
  742. /* Factors are 2^31-1 and 2^61-1 (Mersenne primes M31 and M61) */
  743. if (sizeof(unsigned long) == 4) {
  744. return ENTROPY_DEBUG("fallback(4)", entropy * 2147483647);
  745. } else {
  746. return ENTROPY_DEBUG("fallback(8)",
  747. entropy * (unsigned long)2305843009213693951);
  748. }
  749. #endif
  750. }
  751. static unsigned long
  752. get_hash_secret_salt(XML_Parser parser) {
  753. if (parser->m_parentParser != NULL)
  754. return get_hash_secret_salt(parser->m_parentParser);
  755. return parser->m_hash_secret_salt;
  756. }
  757. static XML_Bool /* only valid for root parser */
  758. startParsing(XML_Parser parser)
  759. {
  760. /* hash functions must be initialized before setContext() is called */
  761. if (hash_secret_salt == 0)
  762. hash_secret_salt = generate_hash_secret_salt(parser);
  763. if (ns) {
  764. /* implicit context only set for root parser, since child
  765. parsers (i.e. external entity parsers) will inherit it
  766. */
  767. return setContext(parser, implicitContext);
  768. }
  769. return XML_TRUE;
  770. }
  771. XML_Parser XMLCALL
  772. XML_ParserCreate_MM(const XML_Char *encodingName,
  773. const XML_Memory_Handling_Suite *memsuite,
  774. const XML_Char *nameSep)
  775. {
  776. return parserCreate(encodingName, memsuite, nameSep, NULL);
  777. }
  778. static XML_Parser
  779. parserCreate(const XML_Char *encodingName,
  780. const XML_Memory_Handling_Suite *memsuite,
  781. const XML_Char *nameSep,
  782. DTD *dtd)
  783. {
  784. XML_Parser parser;
  785. if (memsuite) {
  786. XML_Memory_Handling_Suite *mtemp;
  787. parser = (XML_Parser)
  788. memsuite->malloc_fcn(sizeof(struct XML_ParserStruct));
  789. if (parser != NULL) {
  790. mtemp = (XML_Memory_Handling_Suite *)&(parser->m_mem);
  791. mtemp->malloc_fcn = memsuite->malloc_fcn;
  792. mtemp->realloc_fcn = memsuite->realloc_fcn;
  793. mtemp->free_fcn = memsuite->free_fcn;
  794. }
  795. }
  796. else {
  797. XML_Memory_Handling_Suite *mtemp;
  798. parser = (XML_Parser)malloc(sizeof(struct XML_ParserStruct));
  799. if (parser != NULL) {
  800. mtemp = (XML_Memory_Handling_Suite *)&(parser->m_mem);
  801. mtemp->malloc_fcn = malloc;
  802. mtemp->realloc_fcn = realloc;
  803. mtemp->free_fcn = free;
  804. }
  805. }
  806. if (!parser)
  807. return parser;
  808. buffer = NULL;
  809. bufferLim = NULL;
  810. attsSize = INIT_ATTS_SIZE;
  811. atts = (ATTRIBUTE *)MALLOC(attsSize * sizeof(ATTRIBUTE));
  812. if (atts == NULL) {
  813. FREE(parser);
  814. return NULL;
  815. }
  816. #ifdef XML_ATTR_INFO
  817. attInfo = (XML_AttrInfo*)MALLOC(attsSize * sizeof(XML_AttrInfo));
  818. if (attInfo == NULL) {
  819. FREE(atts);
  820. FREE(parser);
  821. return NULL;
  822. }
  823. #endif
  824. dataBuf = (XML_Char *)MALLOC(INIT_DATA_BUF_SIZE * sizeof(XML_Char));
  825. if (dataBuf == NULL) {
  826. FREE(atts);
  827. #ifdef XML_ATTR_INFO
  828. FREE(attInfo);
  829. #endif
  830. FREE(parser);
  831. return NULL;
  832. }
  833. dataBufEnd = dataBuf + INIT_DATA_BUF_SIZE;
  834. if (dtd)
  835. _dtd = dtd;
  836. else {
  837. _dtd = dtdCreate(&parser->m_mem);
  838. if (_dtd == NULL) {
  839. FREE(dataBuf);
  840. FREE(atts);
  841. #ifdef XML_ATTR_INFO
  842. FREE(attInfo);
  843. #endif
  844. FREE(parser);
  845. return NULL;
  846. }
  847. }
  848. freeBindingList = NULL;
  849. freeTagList = NULL;
  850. freeInternalEntities = NULL;
  851. groupSize = 0;
  852. groupConnector = NULL;
  853. unknownEncodingHandler = NULL;
  854. unknownEncodingHandlerData = NULL;
  855. namespaceSeparator = ASCII_EXCL;
  856. ns = XML_FALSE;
  857. ns_triplets = XML_FALSE;
  858. nsAtts = NULL;
  859. nsAttsVersion = 0;
  860. nsAttsPower = 0;
  861. poolInit(&tempPool, &(parser->m_mem));
  862. poolInit(&temp2Pool, &(parser->m_mem));
  863. parserInit(parser, encodingName);
  864. if (encodingName && !protocolEncodingName) {
  865. XML_ParserFree(parser);
  866. return NULL;
  867. }
  868. if (nameSep) {
  869. ns = XML_TRUE;
  870. internalEncoding = XmlGetInternalEncodingNS();
  871. namespaceSeparator = *nameSep;
  872. }
  873. else {
  874. internalEncoding = XmlGetInternalEncoding();
  875. }
  876. return parser;
  877. }
  878. static void
  879. parserInit(XML_Parser parser, const XML_Char *encodingName)
  880. {
  881. processor = prologInitProcessor;
  882. XmlPrologStateInit(&prologState);
  883. protocolEncodingName = (encodingName != NULL
  884. ? poolCopyString(&tempPool, encodingName)
  885. : NULL);
  886. curBase = NULL;
  887. XmlInitEncoding(&initEncoding, &encoding, 0);
  888. userData = NULL;
  889. handlerArg = NULL;
  890. startElementHandler = NULL;
  891. endElementHandler = NULL;
  892. characterDataHandler = NULL;
  893. processingInstructionHandler = NULL;
  894. commentHandler = NULL;
  895. startCdataSectionHandler = NULL;
  896. endCdataSectionHandler = NULL;
  897. defaultHandler = NULL;
  898. startDoctypeDeclHandler = NULL;
  899. endDoctypeDeclHandler = NULL;
  900. unparsedEntityDeclHandler = NULL;
  901. notationDeclHandler = NULL;
  902. startNamespaceDeclHandler = NULL;
  903. endNamespaceDeclHandler = NULL;
  904. notStandaloneHandler = NULL;
  905. externalEntityRefHandler = NULL;
  906. externalEntityRefHandlerArg = parser;
  907. skippedEntityHandler = NULL;
  908. elementDeclHandler = NULL;
  909. attlistDeclHandler = NULL;
  910. entityDeclHandler = NULL;
  911. xmlDeclHandler = NULL;
  912. bufferPtr = buffer;
  913. bufferEnd = buffer;
  914. parseEndByteIndex = 0;
  915. parseEndPtr = NULL;
  916. declElementType = NULL;
  917. declAttributeId = NULL;
  918. declEntity = NULL;
  919. doctypeName = NULL;
  920. doctypeSysid = NULL;
  921. doctypePubid = NULL;
  922. declAttributeType = NULL;
  923. declNotationName = NULL;
  924. declNotationPublicId = NULL;
  925. declAttributeIsCdata = XML_FALSE;
  926. declAttributeIsId = XML_FALSE;
  927. memset(&position, 0, sizeof(POSITION));
  928. errorCode = XML_ERROR_NONE;
  929. eventPtr = NULL;
  930. eventEndPtr = NULL;
  931. positionPtr = NULL;
  932. openInternalEntities = NULL;
  933. defaultExpandInternalEntities = XML_TRUE;
  934. tagLevel = 0;
  935. tagStack = NULL;
  936. inheritedBindings = NULL;
  937. nSpecifiedAtts = 0;
  938. unknownEncodingMem = NULL;
  939. unknownEncodingRelease = NULL;
  940. unknownEncodingData = NULL;
  941. parentParser = NULL;
  942. ps_parsing = XML_INITIALIZED;
  943. #ifdef XML_DTD
  944. isParamEntity = XML_FALSE;
  945. useForeignDTD = XML_FALSE;
  946. paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
  947. #endif
  948. hash_secret_salt = 0;
  949. }
  950. /* moves list of bindings to freeBindingList */
  951. static void FASTCALL
  952. moveToFreeBindingList(XML_Parser parser, BINDING *bindings)
  953. {
  954. while (bindings) {
  955. BINDING *b = bindings;
  956. bindings = bindings->nextTagBinding;
  957. b->nextTagBinding = freeBindingList;
  958. freeBindingList = b;
  959. }
  960. }
  961. XML_Bool XMLCALL
  962. XML_ParserReset(XML_Parser parser, const XML_Char *encodingName)
  963. {
  964. TAG *tStk;
  965. OPEN_INTERNAL_ENTITY *openEntityList;
  966. if (parser == NULL)
  967. return XML_FALSE;
  968. if (parentParser)
  969. return XML_FALSE;
  970. /* move tagStack to freeTagList */
  971. tStk = tagStack;
  972. while (tStk) {
  973. TAG *tag = tStk;
  974. tStk = tStk->parent;
  975. tag->parent = freeTagList;
  976. moveToFreeBindingList(parser, tag->bindings);
  977. tag->bindings = NULL;
  978. freeTagList = tag;
  979. }
  980. /* move openInternalEntities to freeInternalEntities */
  981. openEntityList = openInternalEntities;
  982. while (openEntityList) {
  983. OPEN_INTERNAL_ENTITY *openEntity = openEntityList;
  984. openEntityList = openEntity->next;
  985. openEntity->next = freeInternalEntities;
  986. freeInternalEntities = openEntity;
  987. }
  988. moveToFreeBindingList(parser, inheritedBindings);
  989. FREE(unknownEncodingMem);
  990. if (unknownEncodingRelease)
  991. unknownEncodingRelease(unknownEncodingData);
  992. poolClear(&tempPool);
  993. poolClear(&temp2Pool);
  994. parserInit(parser, encodingName);
  995. dtdReset(_dtd, &parser->m_mem);
  996. return XML_TRUE;
  997. }
  998. enum XML_Status XMLCALL
  999. XML_SetEncoding(XML_Parser parser, const XML_Char *encodingName)
  1000. {
  1001. if (parser == NULL)
  1002. return XML_STATUS_ERROR;
  1003. /* Block after XML_Parse()/XML_ParseBuffer() has been called.
  1004. XXX There's no way for the caller to determine which of the
  1005. XXX possible error cases caused the XML_STATUS_ERROR return.
  1006. */
  1007. if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
  1008. return XML_STATUS_ERROR;
  1009. if (encodingName == NULL)
  1010. protocolEncodingName = NULL;
  1011. else {
  1012. protocolEncodingName = poolCopyString(&tempPool, encodingName);
  1013. if (!protocolEncodingName)
  1014. return XML_STATUS_ERROR;
  1015. }
  1016. return XML_STATUS_OK;
  1017. }
  1018. XML_Parser XMLCALL
  1019. XML_ExternalEntityParserCreate(XML_Parser oldParser,
  1020. const XML_Char *context,
  1021. const XML_Char *encodingName)
  1022. {
  1023. XML_Parser parser = oldParser;
  1024. DTD *newDtd = NULL;
  1025. DTD *oldDtd;
  1026. XML_StartElementHandler oldStartElementHandler;
  1027. XML_EndElementHandler oldEndElementHandler;
  1028. XML_CharacterDataHandler oldCharacterDataHandler;
  1029. XML_ProcessingInstructionHandler oldProcessingInstructionHandler;
  1030. XML_CommentHandler oldCommentHandler;
  1031. XML_StartCdataSectionHandler oldStartCdataSectionHandler;
  1032. XML_EndCdataSectionHandler oldEndCdataSectionHandler;
  1033. XML_DefaultHandler oldDefaultHandler;
  1034. XML_UnparsedEntityDeclHandler oldUnparsedEntityDeclHandler;
  1035. XML_NotationDeclHandler oldNotationDeclHandler;
  1036. XML_StartNamespaceDeclHandler oldStartNamespaceDeclHandler;
  1037. XML_EndNamespaceDeclHandler oldEndNamespaceDeclHandler;
  1038. XML_NotStandaloneHandler oldNotStandaloneHandler;
  1039. XML_ExternalEntityRefHandler oldExternalEntityRefHandler;
  1040. XML_SkippedEntityHandler oldSkippedEntityHandler;
  1041. XML_UnknownEncodingHandler oldUnknownEncodingHandler;
  1042. XML_ElementDeclHandler oldElementDeclHandler;
  1043. XML_AttlistDeclHandler oldAttlistDeclHandler;
  1044. XML_EntityDeclHandler oldEntityDeclHandler;
  1045. XML_XmlDeclHandler oldXmlDeclHandler;
  1046. ELEMENT_TYPE * oldDeclElementType;
  1047. void *oldUserData;
  1048. void *oldHandlerArg;
  1049. XML_Bool oldDefaultExpandInternalEntities;
  1050. XML_Parser oldExternalEntityRefHandlerArg;
  1051. #ifdef XML_DTD
  1052. enum XML_ParamEntityParsing oldParamEntityParsing;
  1053. int oldInEntityValue;
  1054. #endif
  1055. XML_Bool oldns_triplets;
  1056. /* Note that the new parser shares the same hash secret as the old
  1057. parser, so that dtdCopy and copyEntityTable can lookup values
  1058. from hash tables associated with either parser without us having
  1059. to worry which hash secrets each table has.
  1060. */
  1061. unsigned long oldhash_secret_salt;
  1062. /* Validate the oldParser parameter before we pull everything out of it */
  1063. if (oldParser == NULL)
  1064. return NULL;
  1065. /* Stash the original parser contents on the stack */
  1066. oldDtd = _dtd;
  1067. oldStartElementHandler = startElementHandler;
  1068. oldEndElementHandler = endElementHandler;
  1069. oldCharacterDataHandler = characterDataHandler;
  1070. oldProcessingInstructionHandler = processingInstructionHandler;
  1071. oldCommentHandler = commentHandler;
  1072. oldStartCdataSectionHandler = startCdataSectionHandler;
  1073. oldEndCdataSectionHandler = endCdataSectionHandler;
  1074. oldDefaultHandler = defaultHandler;
  1075. oldUnparsedEntityDeclHandler = unparsedEntityDeclHandler;
  1076. oldNotationDeclHandler = notationDeclHandler;
  1077. oldStartNamespaceDeclHandler = startNamespaceDeclHandler;
  1078. oldEndNamespaceDeclHandler = endNamespaceDeclHandler;
  1079. oldNotStandaloneHandler = notStandaloneHandler;
  1080. oldExternalEntityRefHandler = externalEntityRefHandler;
  1081. oldSkippedEntityHandler = skippedEntityHandler;
  1082. oldUnknownEncodingHandler = unknownEncodingHandler;
  1083. oldElementDeclHandler = elementDeclHandler;
  1084. oldAttlistDeclHandler = attlistDeclHandler;
  1085. oldEntityDeclHandler = entityDeclHandler;
  1086. oldXmlDeclHandler = xmlDeclHandler;
  1087. oldDeclElementType = declElementType;
  1088. oldUserData = userData;
  1089. oldHandlerArg = handlerArg;
  1090. oldDefaultExpandInternalEntities = defaultExpandInternalEntities;
  1091. oldExternalEntityRefHandlerArg = externalEntityRefHandlerArg;
  1092. #ifdef XML_DTD
  1093. oldParamEntityParsing = paramEntityParsing;
  1094. oldInEntityValue = prologState.inEntityValue;
  1095. #endif
  1096. oldns_triplets = ns_triplets;
  1097. /* Note that the new parser shares the same hash secret as the old
  1098. parser, so that dtdCopy and copyEntityTable can lookup values
  1099. from hash tables associated with either parser without us having
  1100. to worry which hash secrets each table has.
  1101. */
  1102. oldhash_secret_salt = hash_secret_salt;
  1103. #ifdef XML_DTD
  1104. if (!context)
  1105. newDtd = oldDtd;
  1106. #endif /* XML_DTD */
  1107. /* Note that the magical uses of the pre-processor to make field
  1108. access look more like C++ require that `parser' be overwritten
  1109. here. This makes this function more painful to follow than it
  1110. would be otherwise.
  1111. */
  1112. if (ns) {
  1113. XML_Char tmp[2];
  1114. *tmp = namespaceSeparator;
  1115. parser = parserCreate(encodingName, &parser->m_mem, tmp, newDtd);
  1116. }
  1117. else {
  1118. parser = parserCreate(encodingName, &parser->m_mem, NULL, newDtd);
  1119. }
  1120. if (!parser)
  1121. return NULL;
  1122. startElementHandler = oldStartElementHandler;
  1123. endElementHandler = oldEndElementHandler;
  1124. characterDataHandler = oldCharacterDataHandler;
  1125. processingInstructionHandler = oldProcessingInstructionHandler;
  1126. commentHandler = oldCommentHandler;
  1127. startCdataSectionHandler = oldStartCdataSectionHandler;
  1128. endCdataSectionHandler = oldEndCdataSectionHandler;
  1129. defaultHandler = oldDefaultHandler;
  1130. unparsedEntityDeclHandler = oldUnparsedEntityDeclHandler;
  1131. notationDeclHandler = oldNotationDeclHandler;
  1132. startNamespaceDeclHandler = oldStartNamespaceDeclHandler;
  1133. endNamespaceDeclHandler = oldEndNamespaceDeclHandler;
  1134. notStandaloneHandler = oldNotStandaloneHandler;
  1135. externalEntityRefHandler = oldExternalEntityRefHandler;
  1136. skippedEntityHandler = oldSkippedEntityHandler;
  1137. unknownEncodingHandler = oldUnknownEncodingHandler;
  1138. elementDeclHandler = oldElementDeclHandler;
  1139. attlistDeclHandler = oldAttlistDeclHandler;
  1140. entityDeclHandler = oldEntityDeclHandler;
  1141. xmlDeclHandler = oldXmlDeclHandler;
  1142. declElementType = oldDeclElementType;
  1143. userData = oldUserData;
  1144. if (oldUserData == oldHandlerArg)
  1145. handlerArg = userData;
  1146. else
  1147. handlerArg = parser;
  1148. if (oldExternalEntityRefHandlerArg != oldParser)
  1149. externalEntityRefHandlerArg = oldExternalEntityRefHandlerArg;
  1150. defaultExpandInternalEntities = oldDefaultExpandInternalEntities;
  1151. ns_triplets = oldns_triplets;
  1152. hash_secret_salt = oldhash_secret_salt;
  1153. parentParser = oldParser;
  1154. #ifdef XML_DTD
  1155. paramEntityParsing = oldParamEntityParsing;
  1156. prologState.inEntityValue = oldInEntityValue;
  1157. if (context) {
  1158. #endif /* XML_DTD */
  1159. if (!dtdCopy(oldParser, _dtd, oldDtd, &parser->m_mem)
  1160. || !setContext(parser, context)) {
  1161. XML_ParserFree(parser);
  1162. return NULL;
  1163. }
  1164. processor = externalEntityInitProcessor;
  1165. #ifdef XML_DTD
  1166. }
  1167. else {
  1168. /* The DTD instance referenced by _dtd is shared between the document's
  1169. root parser and external PE parsers, therefore one does not need to
  1170. call setContext. In addition, one also *must* not call setContext,
  1171. because this would overwrite existing prefix->binding pointers in
  1172. _dtd with ones that get destroyed with the external PE parser.
  1173. This would leave those prefixes with dangling pointers.
  1174. */
  1175. isParamEntity = XML_TRUE;
  1176. XmlPrologStateInitExternalEntity(&prologState);
  1177. processor = externalParEntInitProcessor;
  1178. }
  1179. #endif /* XML_DTD */
  1180. return parser;
  1181. }
  1182. static void FASTCALL
  1183. destroyBindings(BINDING *bindings, XML_Parser parser)
  1184. {
  1185. for (;;) {
  1186. BINDING *b = bindings;
  1187. if (!b)
  1188. break;
  1189. bindings = b->nextTagBinding;
  1190. FREE(b->uri);
  1191. FREE(b);
  1192. }
  1193. }
  1194. void XMLCALL
  1195. XML_ParserFree(XML_Parser parser)
  1196. {
  1197. TAG *tagList;
  1198. OPEN_INTERNAL_ENTITY *entityList;
  1199. if (parser == NULL)
  1200. return;
  1201. /* free tagStack and freeTagList */
  1202. tagList = tagStack;
  1203. for (;;) {
  1204. TAG *p;
  1205. if (tagList == NULL) {
  1206. if (freeTagList == NULL)
  1207. break;
  1208. tagList = freeTagList;
  1209. freeTagList = NULL;
  1210. }
  1211. p = tagList;
  1212. tagList = tagList->parent;
  1213. FREE(p->buf);
  1214. destroyBindings(p->bindings, parser);
  1215. FREE(p);
  1216. }
  1217. /* free openInternalEntities and freeInternalEntities */
  1218. entityList = openInternalEntities;
  1219. for (;;) {
  1220. OPEN_INTERNAL_ENTITY *openEntity;
  1221. if (entityList == NULL) {
  1222. if (freeInternalEntities == NULL)
  1223. break;
  1224. entityList = freeInternalEntities;
  1225. freeInternalEntities = NULL;
  1226. }
  1227. openEntity = entityList;
  1228. entityList = entityList->next;
  1229. FREE(openEntity);
  1230. }
  1231. destroyBindings(freeBindingList, parser);
  1232. destroyBindings(inheritedBindings, parser);
  1233. poolDestroy(&tempPool);
  1234. poolDestroy(&temp2Pool);
  1235. #ifdef XML_DTD
  1236. /* external parameter entity parsers share the DTD structure
  1237. parser->m_dtd with the root parser, so we must not destroy it
  1238. */
  1239. if (!isParamEntity && _dtd)
  1240. #else
  1241. if (_dtd)
  1242. #endif /* XML_DTD */
  1243. dtdDestroy(_dtd, (XML_Bool)!parentParser, &parser->m_mem);
  1244. FREE((void *)atts);
  1245. #ifdef XML_ATTR_INFO
  1246. FREE((void *)attInfo);
  1247. #endif
  1248. FREE(groupConnector);
  1249. FREE(buffer);
  1250. FREE(dataBuf);
  1251. FREE(nsAtts);
  1252. FREE(unknownEncodingMem);
  1253. if (unknownEncodingRelease)
  1254. unknownEncodingRelease(unknownEncodingData);
  1255. FREE(parser);
  1256. }
  1257. void XMLCALL
  1258. XML_UseParserAsHandlerArg(XML_Parser parser)
  1259. {
  1260. if (parser != NULL)
  1261. handlerArg = parser;
  1262. }
  1263. enum XML_Error XMLCALL
  1264. XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD)
  1265. {
  1266. if (parser == NULL)
  1267. return XML_ERROR_INVALID_ARGUMENT;
  1268. #ifdef XML_DTD
  1269. /* block after XML_Parse()/XML_ParseBuffer() has been called */
  1270. if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
  1271. return XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING;
  1272. useForeignDTD = useDTD;
  1273. return XML_ERROR_NONE;
  1274. #else
  1275. return XML_ERROR_FEATURE_REQUIRES_XML_DTD;
  1276. #endif
  1277. }
  1278. void XMLCALL
  1279. XML_SetReturnNSTriplet(XML_Parser parser, int do_nst)
  1280. {
  1281. if (parser == NULL)
  1282. return;
  1283. /* block after XML_Parse()/XML_ParseBuffer() has been called */
  1284. if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
  1285. return;
  1286. ns_triplets = do_nst ? XML_TRUE : XML_FALSE;
  1287. }
  1288. void XMLCALL
  1289. XML_SetUserData(XML_Parser parser, void *p)
  1290. {
  1291. if (parser == NULL)
  1292. return;
  1293. if (handlerArg == userData)
  1294. handlerArg = userData = p;
  1295. else
  1296. userData = p;
  1297. }
  1298. enum XML_Status XMLCALL
  1299. XML_SetBase(XML_Parser parser, const XML_Char *p)
  1300. {
  1301. if (parser == NULL)
  1302. return XML_STATUS_ERROR;
  1303. if (p) {
  1304. p = poolCopyString(&_dtd->pool, p);
  1305. if (!p)
  1306. return XML_STATUS_ERROR;
  1307. curBase = p;
  1308. }
  1309. else
  1310. curBase = NULL;
  1311. return XML_STATUS_OK;
  1312. }
  1313. const XML_Char * XMLCALL
  1314. XML_GetBase(XML_Parser parser)
  1315. {
  1316. if (parser == NULL)
  1317. return NULL;
  1318. return curBase;
  1319. }
  1320. int XMLCALL
  1321. XML_GetSpecifiedAttributeCount(XML_Parser parser)
  1322. {
  1323. if (parser == NULL)
  1324. return -1;
  1325. return nSpecifiedAtts;
  1326. }
  1327. int XMLCALL
  1328. XML_GetIdAttributeIndex(XML_Parser parser)
  1329. {
  1330. if (parser == NULL)
  1331. return -1;
  1332. return idAttIndex;
  1333. }
  1334. #ifdef XML_ATTR_INFO
  1335. const XML_AttrInfo * XMLCALL
  1336. XML_GetAttributeInfo(XML_Parser parser)
  1337. {
  1338. if (parser == NULL)
  1339. return NULL;
  1340. return attInfo;
  1341. }
  1342. #endif
  1343. void XMLCALL
  1344. XML_SetElementHandler(XML_Parser parser,
  1345. XML_StartElementHandler start,
  1346. XML_EndElementHandler end)
  1347. {
  1348. if (parser == NULL)
  1349. return;
  1350. startElementHandler = start;
  1351. endElementHandler = end;
  1352. }
  1353. void XMLCALL
  1354. XML_SetStartElementHandler(XML_Parser parser,
  1355. XML_StartElementHandler start) {
  1356. if (parser != NULL)
  1357. startElementHandler = start;
  1358. }
  1359. void XMLCALL
  1360. XML_SetEndElementHandler(XML_Parser parser,
  1361. XML_EndElementHandler end) {
  1362. if (parser != NULL)
  1363. endElementHandler = end;
  1364. }
  1365. void XMLCALL
  1366. XML_SetCharacterDataHandler(XML_Parser parser,
  1367. XML_CharacterDataHandler handler)
  1368. {
  1369. if (parser != NULL)
  1370. characterDataHandler = handler;
  1371. }
  1372. void XMLCALL
  1373. XML_SetProcessingInstructionHandler(XML_Parser parser,
  1374. XML_ProcessingInstructionHandler handler)
  1375. {
  1376. if (parser != NULL)
  1377. processingInstructionHandler = handler;
  1378. }
  1379. void XMLCALL
  1380. XML_SetCommentHandler(XML_Parser parser,
  1381. XML_CommentHandler handler)
  1382. {
  1383. if (parser != NULL)
  1384. commentHandler = handler;
  1385. }
  1386. void XMLCALL
  1387. XML_SetCdataSectionHandler(XML_Parser parser,
  1388. XML_StartCdataSectionHandler start,
  1389. XML_EndCdataSectionHandler end)
  1390. {
  1391. if (parser == NULL)
  1392. return;
  1393. startCdataSectionHandler = start;
  1394. endCdataSectionHandler = end;
  1395. }
  1396. void XMLCALL
  1397. XML_SetStartCdataSectionHandler(XML_Parser parser,
  1398. XML_StartCdataSectionHandler start) {
  1399. if (parser != NULL)
  1400. startCdataSectionHandler = start;
  1401. }
  1402. void XMLCALL
  1403. XML_SetEndCdataSectionHandler(XML_Parser parser,
  1404. XML_EndCdataSectionHandler end) {
  1405. if (parser != NULL)
  1406. endCdataSectionHandler = end;
  1407. }
  1408. void XMLCALL
  1409. XML_SetDefaultHandler(XML_Parser parser,
  1410. XML_DefaultHandler handler)
  1411. {
  1412. if (parser == NULL)
  1413. return;
  1414. defaultHandler = handler;
  1415. defaultExpandInternalEntities = XML_FALSE;
  1416. }
  1417. void XMLCALL
  1418. XML_SetDefaultHandlerExpand(XML_Parser parser,
  1419. XML_DefaultHandler handler)
  1420. {
  1421. if (parser == NULL)
  1422. return;
  1423. defaultHandler = handler;
  1424. defaultExpandInternalEntities = XML_TRUE;
  1425. }
  1426. void XMLCALL
  1427. XML_SetDoctypeDeclHandler(XML_Parser parser,
  1428. XML_StartDoctypeDeclHandler start,
  1429. XML_EndDoctypeDeclHandler end)
  1430. {
  1431. if (parser == NULL)
  1432. return;
  1433. startDoctypeDeclHandler = start;
  1434. endDoctypeDeclHandler = end;
  1435. }
  1436. void XMLCALL
  1437. XML_SetStartDoctypeDeclHandler(XML_Parser parser,
  1438. XML_StartDoctypeDeclHandler start) {
  1439. if (parser != NULL)
  1440. startDoctypeDeclHandler = start;
  1441. }
  1442. void XMLCALL
  1443. XML_SetEndDoctypeDeclHandler(XML_Parser parser,
  1444. XML_EndDoctypeDeclHandler end) {
  1445. if (parser != NULL)
  1446. endDoctypeDeclHandler = end;
  1447. }
  1448. void XMLCALL
  1449. XML_SetUnparsedEntityDeclHandler(XML_Parser parser,
  1450. XML_UnparsedEntityDeclHandler handler)
  1451. {
  1452. if (parser != NULL)
  1453. unparsedEntityDeclHandler = handler;
  1454. }
  1455. void XMLCALL
  1456. XML_SetNotationDeclHandler(XML_Parser parser,
  1457. XML_NotationDeclHandler handler)
  1458. {
  1459. if (parser != NULL)
  1460. notationDeclHandler = handler;
  1461. }
  1462. void XMLCALL
  1463. XML_SetNamespaceDeclHandler(XML_Parser parser,
  1464. XML_StartNamespaceDeclHandler start,
  1465. XML_EndNamespaceDeclHandler end)
  1466. {
  1467. if (parser == NULL)
  1468. return;
  1469. startNamespaceDeclHandler = start;
  1470. endNamespaceDeclHandler = end;
  1471. }
  1472. void XMLCALL
  1473. XML_SetStartNamespaceDeclHandler(XML_Parser parser,
  1474. XML_StartNamespaceDeclHandler start) {
  1475. if (parser != NULL)
  1476. startNamespaceDeclHandler = start;
  1477. }
  1478. void XMLCALL
  1479. XML_SetEndNamespaceDeclHandler(XML_Parser parser,
  1480. XML_EndNamespaceDeclHandler end) {
  1481. if (parser != NULL)
  1482. endNamespaceDeclHandler = end;
  1483. }
  1484. void XMLCALL
  1485. XML_SetNotStandaloneHandler(XML_Parser parser,
  1486. XML_NotStandaloneHandler handler)
  1487. {
  1488. if (parser != NULL)
  1489. notStandaloneHandler = handler;
  1490. }
  1491. void XMLCALL
  1492. XML_SetExternalEntityRefHandler(XML_Parser parser,
  1493. XML_ExternalEntityRefHandler handler)
  1494. {
  1495. if (parser != NULL)
  1496. externalEntityRefHandler = handler;
  1497. }
  1498. void XMLCALL
  1499. XML_SetExternalEntityRefHandlerArg(XML_Parser parser, void *arg)
  1500. {
  1501. if (parser == NULL)
  1502. return;
  1503. if (arg)
  1504. externalEntityRefHandlerArg = (XML_Parser)arg;
  1505. else
  1506. externalEntityRefHandlerArg = parser;
  1507. }
  1508. void XMLCALL
  1509. XML_SetSkippedEntityHandler(XML_Parser parser,
  1510. XML_SkippedEntityHandler handler)
  1511. {
  1512. if (parser != NULL)
  1513. skippedEntityHandler = handler;
  1514. }
  1515. void XMLCALL
  1516. XML_SetUnknownEncodingHandler(XML_Parser parser,
  1517. XML_UnknownEncodingHandler handler,
  1518. void *data)
  1519. {
  1520. if (parser == NULL)
  1521. return;
  1522. unknownEncodingHandler = handler;
  1523. unknownEncodingHandlerData = data;
  1524. }
  1525. void XMLCALL
  1526. XML_SetElementDeclHandler(XML_Parser parser,
  1527. XML_ElementDeclHandler eldecl)
  1528. {
  1529. if (parser != NULL)
  1530. elementDeclHandler = eldecl;
  1531. }
  1532. void XMLCALL
  1533. XML_SetAttlistDeclHandler(XML_Parser parser,
  1534. XML_AttlistDeclHandler attdecl)
  1535. {
  1536. if (parser != NULL)
  1537. attlistDeclHandler = attdecl;
  1538. }
  1539. void XMLCALL
  1540. XML_SetEntityDeclHandler(XML_Parser parser,
  1541. XML_EntityDeclHandler handler)
  1542. {
  1543. if (parser != NULL)
  1544. entityDeclHandler = handler;
  1545. }
  1546. void XMLCALL
  1547. XML_SetXmlDeclHandler(XML_Parser parser,
  1548. XML_XmlDeclHandler handler) {
  1549. if (parser != NULL)
  1550. xmlDeclHandler = handler;
  1551. }
  1552. int XMLCALL
  1553. XML_SetParamEntityParsing(XML_Parser parser,
  1554. enum XML_ParamEntityParsing peParsing)
  1555. {
  1556. if (parser == NULL)
  1557. return 0;
  1558. /* block after XML_Parse()/XML_ParseBuffer() has been called */
  1559. if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
  1560. return 0;
  1561. #ifdef XML_DTD
  1562. paramEntityParsing = peParsing;
  1563. return 1;
  1564. #else
  1565. return peParsing == XML_PARAM_ENTITY_PARSING_NEVER;
  1566. #endif
  1567. }
  1568. int XMLCALL
  1569. XML_SetHashSalt(XML_Parser parser,
  1570. unsigned long hash_salt)
  1571. {
  1572. if (parser == NULL)
  1573. return 0;
  1574. if (parser->m_parentParser)
  1575. return XML_SetHashSalt(parser->m_parentParser, hash_salt);
  1576. /* block after XML_Parse()/XML_ParseBuffer() has been called */
  1577. if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
  1578. return 0;
  1579. hash_secret_salt = hash_salt;
  1580. return 1;
  1581. }
  1582. enum XML_Status XMLCALL
  1583. XML_Parse(XML_Parser parser, const char *s, int len, int isFinal)
  1584. {
  1585. if ((parser == NULL) || (len < 0) || ((s == NULL) && (len != 0))) {
  1586. errorCode = XML_ERROR_INVALID_ARGUMENT;
  1587. return XML_STATUS_ERROR;
  1588. }
  1589. switch (ps_parsing) {
  1590. case XML_SUSPENDED:
  1591. errorCode = XML_ERROR_SUSPENDED;
  1592. return XML_STATUS_ERROR;
  1593. case XML_FINISHED:
  1594. errorCode = XML_ERROR_FINISHED;
  1595. return XML_STATUS_ERROR;
  1596. case XML_INITIALIZED:
  1597. if (parentParser == NULL && !startParsing(parser)) {
  1598. errorCode = XML_ERROR_NO_MEMORY;
  1599. return XML_STATUS_ERROR;
  1600. }
  1601. default:
  1602. ps_parsing = XML_PARSING;
  1603. }
  1604. if (len == 0) {
  1605. ps_finalBuffer = (XML_Bool)isFinal;
  1606. if (!isFinal)
  1607. return XML_STATUS_OK;
  1608. positionPtr = bufferPtr;
  1609. parseEndPtr = bufferEnd;
  1610. /* If data are left over from last buffer, and we now know that these
  1611. data are the final chunk of input, then we have to check them again
  1612. to detect errors based on that fact.
  1613. */
  1614. errorCode = processor(parser, bufferPtr, parseEndPtr, &bufferPtr);
  1615. if (errorCode == XML_ERROR_NONE) {
  1616. switch (ps_parsing) {
  1617. case XML_SUSPENDED:
  1618. XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
  1619. positionPtr = bufferPtr;
  1620. return XML_STATUS_SUSPENDED;
  1621. case XML_INITIALIZED:
  1622. case XML_PARSING:
  1623. ps_parsing = XML_FINISHED;
  1624. /* fall through */
  1625. default:
  1626. return XML_STATUS_OK;
  1627. }
  1628. }
  1629. eventEndPtr = eventPtr;
  1630. processor = errorProcessor;
  1631. return XML_STATUS_ERROR;
  1632. }
  1633. #ifndef XML_CONTEXT_BYTES
  1634. else if (bufferPtr == bufferEnd) {
  1635. const char *end;
  1636. int nLeftOver;
  1637. enum XML_Status result;
  1638. /* Detect overflow (a+b > MAX <==> b > MAX-a) */
  1639. if (len > ((XML_Size)-1) / 2 - parseEndByteIndex) {
  1640. errorCode = XML_ERROR_NO_MEMORY;
  1641. eventPtr = eventEndPtr = NULL;
  1642. processor = errorProcessor;
  1643. return XML_STATUS_ERROR;
  1644. }
  1645. parseEndByteIndex += len;
  1646. positionPtr = s;
  1647. ps_finalBuffer = (XML_Bool)isFinal;
  1648. errorCode = processor(parser, s, parseEndPtr = s + len, &end);
  1649. if (errorCode != XML_ERROR_NONE) {
  1650. eventEndPtr = eventPtr;
  1651. processor = errorProcessor;
  1652. return XML_STATUS_ERROR;
  1653. }
  1654. else {
  1655. switch (ps_parsing) {
  1656. case XML_SUSPENDED:
  1657. result = XML_STATUS_SUSPENDED;
  1658. break;
  1659. case XML_INITIALIZED:
  1660. case XML_PARSING:
  1661. if (isFinal) {
  1662. ps_parsing = XML_FINISHED;
  1663. return XML_STATUS_OK;
  1664. }
  1665. /* fall through */
  1666. default:
  1667. result = XML_STATUS_OK;
  1668. }
  1669. }
  1670. XmlUpdatePosition(encoding, positionPtr, end, &position);
  1671. nLeftOver = s + len - end;
  1672. if (nLeftOver) {
  1673. if (buffer == NULL || nLeftOver > bufferLim - buffer) {
  1674. /* avoid _signed_ integer overflow */
  1675. char *temp = NULL;
  1676. const int bytesToAllocate = (int)((unsigned)len * 2U);
  1677. if (bytesToAllocate > 0) {
  1678. temp = (buffer == NULL
  1679. ? (char *)MALLOC(bytesToAllocate)
  1680. : (char *)REALLOC(buffer, bytesToAllocate));
  1681. }
  1682. if (temp == NULL) {
  1683. errorCode = XML_ERROR_NO_MEMORY;
  1684. eventPtr = eventEndPtr = NULL;
  1685. processor = errorProcessor;
  1686. return XML_STATUS_ERROR;
  1687. }
  1688. buffer = temp;
  1689. bufferLim = buffer + bytesToAllocate;
  1690. }
  1691. memcpy(buffer, end, nLeftOver);
  1692. }
  1693. bufferPtr = buffer;
  1694. bufferEnd = buffer + nLeftOver;
  1695. positionPtr = bufferPtr;
  1696. parseEndPtr = bufferEnd;
  1697. eventPtr = bufferPtr;
  1698. eventEndPtr = bufferPtr;
  1699. return result;
  1700. }
  1701. #endif /* not defined XML_CONTEXT_BYTES */
  1702. else {
  1703. void *buff = XML_GetBuffer(parser, len);
  1704. if (buff == NULL)
  1705. return XML_STATUS_ERROR;
  1706. else {
  1707. memcpy(buff, s, len);
  1708. return XML_ParseBuffer(parser, len, isFinal);
  1709. }
  1710. }
  1711. }
  1712. enum XML_Status XMLCALL
  1713. XML_ParseBuffer(XML_Parser parser, int len, int isFinal)
  1714. {
  1715. const char *start;
  1716. enum XML_Status result = XML_STATUS_OK;
  1717. if (parser == NULL)
  1718. return XML_STATUS_ERROR;
  1719. switch (ps_parsing) {
  1720. case XML_SUSPENDED:
  1721. errorCode = XML_ERROR_SUSPENDED;
  1722. return XML_STATUS_ERROR;
  1723. case XML_FINISHED:
  1724. errorCode = XML_ERROR_FINISHED;
  1725. return XML_STATUS_ERROR;
  1726. case XML_INITIALIZED:
  1727. if (parentParser == NULL && !startParsing(parser)) {
  1728. errorCode = XML_ERROR_NO_MEMORY;
  1729. return XML_STATUS_ERROR;
  1730. }
  1731. default:
  1732. ps_parsing = XML_PARSING;
  1733. }
  1734. start = bufferPtr;
  1735. positionPtr = start;
  1736. bufferEnd += len;
  1737. parseEndPtr = bufferEnd;
  1738. parseEndByteIndex += len;
  1739. ps_finalBuffer = (XML_Bool)isFinal;
  1740. errorCode = processor(parser, start, parseEndPtr, &bufferPtr);
  1741. if (errorCode != XML_ERROR_NONE) {
  1742. eventEndPtr = eventPtr;
  1743. processor = errorProcessor;
  1744. return XML_STATUS_ERROR;
  1745. }
  1746. else {
  1747. switch (ps_parsing) {
  1748. case XML_SUSPENDED:
  1749. result = XML_STATUS_SUSPENDED;
  1750. break;
  1751. case XML_INITIALIZED:
  1752. case XML_PARSING:
  1753. if (isFinal) {
  1754. ps_parsing = XML_FINISHED;
  1755. return result;
  1756. }
  1757. default: ; /* should not happen */
  1758. }
  1759. }
  1760. XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
  1761. positionPtr = bufferPtr;
  1762. return result;
  1763. }
  1764. void * XMLCALL
  1765. XML_GetBuffer(XML_Parser parser, int len)
  1766. {
  1767. if (parser == NULL)
  1768. return NULL;
  1769. if (len < 0) {
  1770. errorCode = XML_ERROR_NO_MEMORY;
  1771. return NULL;
  1772. }
  1773. switch (ps_parsing) {
  1774. case XML_SUSPENDED:
  1775. errorCode = XML_ERROR_SUSPENDED;
  1776. return NULL;
  1777. case XML_FINISHED:
  1778. errorCode = XML_ERROR_FINISHED;
  1779. return NULL;
  1780. default: ;
  1781. }
  1782. if (len > bufferLim - bufferEnd) {
  1783. #ifdef XML_CONTEXT_BYTES
  1784. int keep;
  1785. #endif /* defined XML_CONTEXT_BYTES */
  1786. /* Do not invoke signed arithmetic overflow: */
  1787. int neededSize = (int) ((unsigned)len + (unsigned)(bufferEnd - bufferPtr));
  1788. if (neededSize < 0) {
  1789. errorCode = XML_ERROR_NO_MEMORY;
  1790. return NULL;
  1791. }
  1792. #ifdef XML_CONTEXT_BYTES
  1793. keep = (int)(bufferPtr - buffer);
  1794. if (keep > XML_CONTEXT_BYTES)
  1795. keep = XML_CONTEXT_BYTES;
  1796. neededSize += keep;
  1797. #endif /* defined XML_CONTEXT_BYTES */
  1798. if (neededSize <= bufferLim - buffer) {
  1799. #ifdef XML_CONTEXT_BYTES
  1800. if (keep < bufferPtr - buffer) {
  1801. int offset = (int)(bufferPtr - buffer) - keep;
  1802. memmove(buffer, &buffer[offset], bufferEnd - bufferPtr + keep);
  1803. bufferEnd -= offset;
  1804. bufferPtr -= offset;
  1805. }
  1806. #else
  1807. memmove(buffer, bufferPtr, bufferEnd - bufferPtr);
  1808. bufferEnd = buffer + (bufferEnd - bufferPtr);
  1809. bufferPtr = buffer;
  1810. #endif /* not defined XML_CONTEXT_BYTES */
  1811. }
  1812. else {
  1813. char *newBuf;
  1814. int bufferSize = (int)(bufferLim - bufferPtr);
  1815. if (bufferSize == 0)
  1816. bufferSize = INIT_BUFFER_SIZE;
  1817. do {
  1818. /* Do not invoke signed arithmetic overflow: */
  1819. bufferSize = (int) (2U * (unsigned) bufferSize);
  1820. } while (bufferSize < neededSize && bufferSize > 0);
  1821. if (bufferSize <= 0) {
  1822. errorCode = XML_ERROR_NO_MEMORY;
  1823. return NULL;
  1824. }
  1825. newBuf = (char *)MALLOC(bufferSize);
  1826. if (newBuf == 0) {
  1827. errorCode = XML_ERROR_NO_MEMORY;
  1828. return NULL;
  1829. }
  1830. bufferLim = newBuf + bufferSize;
  1831. #ifdef XML_CONTEXT_BYTES
  1832. if (bufferPtr) {
  1833. int keep = (int)(bufferPtr - buffer);
  1834. if (keep > XML_CONTEXT_BYTES)
  1835. keep = XML_CONTEXT_BYTES;
  1836. memcpy(newBuf, &bufferPtr[-keep], bufferEnd - bufferPtr + keep);
  1837. FREE(buffer);
  1838. buffer = newBuf;
  1839. bufferEnd = buffer + (bufferEnd - bufferPtr) + keep;
  1840. bufferPtr = buffer + keep;
  1841. }
  1842. else {
  1843. bufferEnd = newBuf + (bufferEnd - bufferPtr);
  1844. bufferPtr = buffer = newBuf;
  1845. }
  1846. #else
  1847. if (bufferPtr) {
  1848. memcpy(newBuf, bufferPtr, bufferEnd - bufferPtr);
  1849. FREE(buffer);
  1850. }
  1851. bufferEnd = newBuf + (bufferEnd - bufferPtr);
  1852. bufferPtr = buffer = newBuf;
  1853. #endif /* not defined XML_CONTEXT_BYTES */
  1854. }
  1855. eventPtr = eventEndPtr = NULL;
  1856. positionPtr = NULL;
  1857. }
  1858. return bufferEnd;
  1859. }
  1860. enum XML_Status XMLCALL
  1861. XML_StopParser(XML_Parser parser, XML_Bool resumable)
  1862. {
  1863. if (parser == NULL)
  1864. return XML_STATUS_ERROR;
  1865. switch (ps_parsing) {
  1866. case XML_SUSPENDED:
  1867. if (resumable) {
  1868. errorCode = XML_ERROR_SUSPENDED;
  1869. return XML_STATUS_ERROR;
  1870. }
  1871. ps_parsing = XML_FINISHED;
  1872. break;
  1873. case XML_FINISHED:
  1874. errorCode = XML_ERROR_FINISHED;
  1875. return XML_STATUS_ERROR;
  1876. default:
  1877. if (resumable) {
  1878. #ifdef XML_DTD
  1879. if (isParamEntity) {
  1880. errorCode = XML_ERROR_SUSPEND_PE;
  1881. return XML_STATUS_ERROR;
  1882. }
  1883. #endif
  1884. ps_parsing = XML_SUSPENDED;
  1885. }
  1886. else
  1887. ps_parsing = XML_FINISHED;
  1888. }
  1889. return XML_STATUS_OK;
  1890. }
  1891. enum XML_Status XMLCALL
  1892. XML_ResumeParser(XML_Parser parser)
  1893. {
  1894. enum XML_Status result = XML_STATUS_OK;
  1895. if (parser == NULL)
  1896. return XML_STATUS_ERROR;
  1897. if (ps_parsing != XML_SUSPENDED) {
  1898. errorCode = XML_ERROR_NOT_SUSPENDED;
  1899. return XML_STATUS_ERROR;
  1900. }
  1901. ps_parsing = XML_PARSING;
  1902. errorCode = processor(parser, bufferPtr, parseEndPtr, &bufferPtr);
  1903. if (errorCode != XML_ERROR_NONE) {
  1904. eventEndPtr = eventPtr;
  1905. processor = errorProcessor;
  1906. return XML_STATUS_ERROR;
  1907. }
  1908. else {
  1909. switch (ps_parsing) {
  1910. case XML_SUSPENDED:
  1911. result = XML_STATUS_SUSPENDED;
  1912. break;
  1913. case XML_INITIALIZED:
  1914. case XML_PARSING:
  1915. if (ps_finalBuffer) {
  1916. ps_parsing = XML_FINISHED;
  1917. return result;
  1918. }
  1919. default: ;
  1920. }
  1921. }
  1922. XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
  1923. positionPtr = bufferPtr;
  1924. return result;
  1925. }
  1926. void XMLCALL
  1927. XML_GetParsingStatus(XML_Parser parser, XML_ParsingStatus *status)
  1928. {
  1929. if (parser == NULL)
  1930. return;
  1931. assert(status != NULL);
  1932. *status = parser->m_parsingStatus;
  1933. }
  1934. enum XML_Error XMLCALL
  1935. XML_GetErrorCode(XML_Parser parser)
  1936. {
  1937. if (parser == NULL)
  1938. return XML_ERROR_INVALID_ARGUMENT;
  1939. return errorCode;
  1940. }
  1941. XML_Index XMLCALL
  1942. XML_GetCurrentByteIndex(XML_Parser parser)
  1943. {
  1944. if (parser == NULL)
  1945. return -1;
  1946. if (eventPtr)
  1947. return (XML_Index)(parseEndByteIndex - (parseEndPtr - eventPtr));
  1948. return -1;
  1949. }
  1950. int XMLCALL
  1951. XML_GetCurrentByteCount(XML_Parser parser)
  1952. {
  1953. if (parser == NULL)
  1954. return 0;
  1955. if (eventEndPtr && eventPtr)
  1956. return (int)(eventEndPtr - eventPtr);
  1957. return 0;
  1958. }
  1959. const char * XMLCALL
  1960. XML_GetInputContext(XML_Parser parser, int *offset, int *size)
  1961. {
  1962. #ifdef XML_CONTEXT_BYTES
  1963. if (parser == NULL)
  1964. return NULL;
  1965. if (eventPtr && buffer) {
  1966. if (offset != NULL)
  1967. *offset = (int)(eventPtr - buffer);
  1968. if (size != NULL)
  1969. *size = (int)(bufferEnd - buffer);
  1970. return buffer;
  1971. }
  1972. #else
  1973. (void)parser;
  1974. (void)offset;
  1975. (void)size;
  1976. #endif /* defined XML_CONTEXT_BYTES */
  1977. return (char *) 0;
  1978. }
  1979. XML_Size XMLCALL
  1980. XML_GetCurrentLineNumber(XML_Parser parser)
  1981. {
  1982. if (parser == NULL)
  1983. return 0;
  1984. if (eventPtr && eventPtr >= positionPtr) {
  1985. XmlUpdatePosition(encoding, positionPtr, eventPtr, &position);
  1986. positionPtr = eventPtr;
  1987. }
  1988. return position.lineNumber + 1;
  1989. }
  1990. XML_Size XMLCALL
  1991. XML_GetCurrentColumnNumber(XML_Parser parser)
  1992. {
  1993. if (parser == NULL)
  1994. return 0;
  1995. if (eventPtr && eventPtr >= positionPtr) {
  1996. XmlUpdatePosition(encoding, positionPtr, eventPtr, &position);
  1997. positionPtr = eventPtr;
  1998. }
  1999. return position.columnNumber;
  2000. }
  2001. void XMLCALL
  2002. XML_FreeContentModel(XML_Parser parser, XML_Content *model)
  2003. {
  2004. if (parser != NULL)
  2005. FREE(model);
  2006. }
  2007. void * XMLCALL
  2008. XML_MemMalloc(XML_Parser parser, size_t size)
  2009. {
  2010. if (parser == NULL)
  2011. return NULL;
  2012. return MALLOC(size);
  2013. }
  2014. void * XMLCALL
  2015. XML_MemRealloc(XML_Parser parser, void *ptr, size_t size)
  2016. {
  2017. if (parser == NULL)
  2018. return NULL;
  2019. return REALLOC(ptr, size);
  2020. }
  2021. void XMLCALL
  2022. XML_MemFree(XML_Parser parser, void *ptr)
  2023. {
  2024. if (parser != NULL)
  2025. FREE(ptr);
  2026. }
  2027. void XMLCALL
  2028. XML_DefaultCurrent(XML_Parser parser)
  2029. {
  2030. if (parser == NULL)
  2031. return;
  2032. if (defaultHandler) {
  2033. if (openInternalEntities)
  2034. reportDefault(parser,
  2035. internalEncoding,
  2036. openInternalEntities->internalEventPtr,
  2037. openInternalEntities->internalEventEndPtr);
  2038. else
  2039. reportDefault(parser, encoding, eventPtr, eventEndPtr);
  2040. }
  2041. }
  2042. const XML_LChar * XMLCALL
  2043. XML_ErrorString(enum XML_Error code)
  2044. {
  2045. static const XML_LChar* const message[] = {
  2046. 0,
  2047. XML_L("out of memory"),
  2048. XML_L("syntax error"),
  2049. XML_L("no element found"),
  2050. XML_L("not well-formed (invalid token)"),
  2051. XML_L("unclosed token"),
  2052. XML_L("partial character"),
  2053. XML_L("mismatched tag"),
  2054. XML_L("duplicate attribute"),
  2055. XML_L("junk after document element"),
  2056. XML_L("illegal parameter entity reference"),
  2057. XML_L("undefined entity"),
  2058. XML_L("recursive entity reference"),
  2059. XML_L("asynchronous entity"),
  2060. XML_L("reference to invalid character number"),
  2061. XML_L("reference to binary entity"),
  2062. XML_L("reference to external entity in attribute"),
  2063. XML_L("XML or text declaration not at start of entity"),
  2064. XML_L("unknown encoding"),
  2065. XML_L("encoding specified in XML declaration is incorrect"),
  2066. XML_L("unclosed CDATA section"),
  2067. XML_L("error in processing external entity reference"),
  2068. XML_L("document is not standalone"),
  2069. XML_L("unexpected parser state - please send a bug report"),
  2070. XML_L("entity declared in parameter entity"),
  2071. XML_L("requested feature requires XML_DTD support in Expat"),
  2072. XML_L("cannot change setting once parsing has begun"),
  2073. XML_L("unbound prefix"),
  2074. XML_L("must not undeclare prefix"),
  2075. XML_L("incomplete markup in parameter entity"),
  2076. XML_L("XML declaration not well-formed"),
  2077. XML_L("text declaration not well-formed"),
  2078. XML_L("illegal character(s) in public id"),
  2079. XML_L("parser suspended"),
  2080. XML_L("parser not suspended"),
  2081. XML_L("parsing aborted"),
  2082. XML_L("parsing finished"),
  2083. XML_L("cannot suspend in external parameter entity"),
  2084. XML_L("reserved prefix (xml) must not be undeclared or bound to another namespace name"),
  2085. XML_L("reserved prefix (xmlns) must not be declared or undeclared"),
  2086. XML_L("prefix must not be bound to one of the reserved namespace names")
  2087. };
  2088. if (code > 0 && code < sizeof(message)/sizeof(message[0]))
  2089. return message[code];
  2090. return NULL;
  2091. }
  2092. const XML_LChar * XMLCALL
  2093. XML_ExpatVersion(void) {
  2094. /* V1 is used to string-ize the version number. However, it would
  2095. string-ize the actual version macro *names* unless we get them
  2096. substituted before being passed to V1. CPP is defined to expand
  2097. a macro, then rescan for more expansions. Thus, we use V2 to expand
  2098. the version macros, then CPP will expand the resulting V1() macro
  2099. with the correct numerals. */
  2100. /* ### I'm assuming cpp is portable in this respect... */
  2101. #define V1(a,b,c) XML_L(#a)XML_L(".")XML_L(#b)XML_L(".")XML_L(#c)
  2102. #define V2(a,b,c) XML_L("expat_")V1(a,b,c)
  2103. return V2(XML_MAJOR_VERSION, XML_MINOR_VERSION, XML_MICRO_VERSION);
  2104. #undef V1
  2105. #undef V2
  2106. }
  2107. XML_Expat_Version XMLCALL
  2108. XML_ExpatVersionInfo(void)
  2109. {
  2110. XML_Expat_Version version;
  2111. version.major = XML_MAJOR_VERSION;
  2112. version.minor = XML_MINOR_VERSION;
  2113. version.micro = XML_MICRO_VERSION;
  2114. return version;
  2115. }
  2116. const XML_Feature * XMLCALL
  2117. XML_GetFeatureList(void)
  2118. {
  2119. static const XML_Feature features[] = {
  2120. {XML_FEATURE_SIZEOF_XML_CHAR, XML_L("sizeof(XML_Char)"),
  2121. sizeof(XML_Char)},
  2122. {XML_FEATURE_SIZEOF_XML_LCHAR, XML_L("sizeof(XML_LChar)"),
  2123. sizeof(XML_LChar)},
  2124. #ifdef XML_UNICODE
  2125. {XML_FEATURE_UNICODE, XML_L("XML_UNICODE"), 0},
  2126. #endif
  2127. #ifdef XML_UNICODE_WCHAR_T
  2128. {XML_FEATURE_UNICODE_WCHAR_T, XML_L("XML_UNICODE_WCHAR_T"), 0},
  2129. #endif
  2130. #ifdef XML_DTD
  2131. {XML_FEATURE_DTD, XML_L("XML_DTD"), 0},
  2132. #endif
  2133. #ifdef XML_CONTEXT_BYTES
  2134. {XML_FEATURE_CONTEXT_BYTES, XML_L("XML_CONTEXT_BYTES"),
  2135. XML_CONTEXT_BYTES},
  2136. #endif
  2137. #ifdef XML_MIN_SIZE
  2138. {XML_FEATURE_MIN_SIZE, XML_L("XML_MIN_SIZE"), 0},
  2139. #endif
  2140. #ifdef XML_NS
  2141. {XML_FEATURE_NS, XML_L("XML_NS"), 0},
  2142. #endif
  2143. #ifdef XML_LARGE_SIZE
  2144. {XML_FEATURE_LARGE_SIZE, XML_L("XML_LARGE_SIZE"), 0},
  2145. #endif
  2146. #ifdef XML_ATTR_INFO
  2147. {XML_FEATURE_ATTR_INFO, XML_L("XML_ATTR_INFO"), 0},
  2148. #endif
  2149. {XML_FEATURE_END, NULL, 0}
  2150. };
  2151. return features;
  2152. }
  2153. /* Initially tag->rawName always points into the parse buffer;
  2154. for those TAG instances opened while the current parse buffer was
  2155. processed, and not yet closed, we need to store tag->rawName in a more
  2156. permanent location, since the parse buffer is about to be discarded.
  2157. */
  2158. static XML_Bool
  2159. storeRawNames(XML_Parser parser)
  2160. {
  2161. TAG *tag = tagStack;
  2162. while (tag) {
  2163. int bufSize;
  2164. int nameLen = sizeof(XML_Char) * (tag->name.strLen + 1);
  2165. char *rawNameBuf = tag->buf + nameLen;
  2166. /* Stop if already stored. Since tagStack is a stack, we can stop
  2167. at the first entry that has already been copied; everything
  2168. below it in the stack is already been accounted for in a
  2169. previous call to this function.
  2170. */
  2171. if (tag->rawName == rawNameBuf)
  2172. break;
  2173. /* For re-use purposes we need to ensure that the
  2174. size of tag->buf is a multiple of sizeof(XML_Char).
  2175. */
  2176. bufSize = nameLen + ROUND_UP(tag->rawNameLength, sizeof(XML_Char));
  2177. if (bufSize > tag->bufEnd - tag->buf) {
  2178. char *temp = (char *)REALLOC(tag->buf, bufSize);
  2179. if (temp == NULL)
  2180. return XML_FALSE;
  2181. /* if tag->name.str points to tag->buf (only when namespace
  2182. processing is off) then we have to update it
  2183. */
  2184. if (tag->name.str == (XML_Char *)tag->buf)
  2185. tag->name.str = (XML_Char *)temp;
  2186. /* if tag->name.localPart is set (when namespace processing is on)
  2187. then update it as well, since it will always point into tag->buf
  2188. */
  2189. if (tag->name.localPart)
  2190. tag->name.localPart = (XML_Char *)temp + (tag->name.localPart -
  2191. (XML_Char *)tag->buf);
  2192. tag->buf = temp;
  2193. tag->bufEnd = temp + bufSize;
  2194. rawNameBuf = temp + nameLen;
  2195. }
  2196. memcpy(rawNameBuf, tag->rawName, tag->rawNameLength);
  2197. tag->rawName = rawNameBuf;
  2198. tag = tag->parent;
  2199. }
  2200. return XML_TRUE;
  2201. }
  2202. static enum XML_Error PTRCALL
  2203. contentProcessor(XML_Parser parser,
  2204. const char *start,
  2205. const char *end,
  2206. const char **endPtr)
  2207. {
  2208. enum XML_Error result = doContent(parser, 0, encoding, start, end,
  2209. endPtr, (XML_Bool)!ps_finalBuffer);
  2210. if (result == XML_ERROR_NONE) {
  2211. if (!storeRawNames(parser))
  2212. return XML_ERROR_NO_MEMORY;
  2213. }
  2214. return result;
  2215. }
  2216. static enum XML_Error PTRCALL
  2217. externalEntityInitProcessor(XML_Parser parser,
  2218. const char *start,
  2219. const char *end,
  2220. const char **endPtr)
  2221. {
  2222. enum XML_Error result = initializeEncoding(parser);
  2223. if (result != XML_ERROR_NONE)
  2224. return result;
  2225. processor = externalEntityInitProcessor2;
  2226. return externalEntityInitProcessor2(parser, start, end, endPtr);
  2227. }
  2228. static enum XML_Error PTRCALL
  2229. externalEntityInitProcessor2(XML_Parser parser,
  2230. const char *start,
  2231. const char *end,
  2232. const char **endPtr)
  2233. {
  2234. const char *next = start; /* XmlContentTok doesn't always set the last arg */
  2235. int tok = XmlContentTok(encoding, start, end, &next);
  2236. switch (tok) {
  2237. case XML_TOK_BOM:
  2238. /* If we are at the end of the buffer, this would cause the next stage,
  2239. i.e. externalEntityInitProcessor3, to pass control directly to
  2240. doContent (by detecting XML_TOK_NONE) without processing any xml text
  2241. declaration - causing the error XML_ERROR_MISPLACED_XML_PI in doContent.
  2242. */
  2243. if (next == end && !ps_finalBuffer) {
  2244. *endPtr = next;
  2245. return XML_ERROR_NONE;
  2246. }
  2247. start = next;
  2248. break;
  2249. case XML_TOK_PARTIAL:
  2250. if (!ps_finalBuffer) {
  2251. *endPtr = start;
  2252. return XML_ERROR_NONE;
  2253. }
  2254. eventPtr = start;
  2255. return XML_ERROR_UNCLOSED_TOKEN;
  2256. case XML_TOK_PARTIAL_CHAR:
  2257. if (!ps_finalBuffer) {
  2258. *endPtr = start;
  2259. return XML_ERROR_NONE;
  2260. }
  2261. eventPtr = start;
  2262. return XML_ERROR_PARTIAL_CHAR;
  2263. }
  2264. processor = externalEntityInitProcessor3;
  2265. return externalEntityInitProcessor3(parser, start, end, endPtr);
  2266. }
  2267. static enum XML_Error PTRCALL
  2268. externalEntityInitProcessor3(XML_Parser parser,
  2269. const char *start,
  2270. const char *end,
  2271. const char **endPtr)
  2272. {
  2273. int tok;
  2274. const char *next = start; /* XmlContentTok doesn't always set the last arg */
  2275. eventPtr = start;
  2276. tok = XmlContentTok(encoding, start, end, &next);
  2277. eventEndPtr = next;
  2278. switch (tok) {
  2279. case XML_TOK_XML_DECL:
  2280. {
  2281. enum XML_Error result;
  2282. result = processXmlDecl(parser, 1, start, next);
  2283. if (result != XML_ERROR_NONE)
  2284. return result;
  2285. switch (ps_parsing) {
  2286. case XML_SUSPENDED:
  2287. *endPtr = next;
  2288. return XML_ERROR_NONE;
  2289. case XML_FINISHED:
  2290. return XML_ERROR_ABORTED;
  2291. default:
  2292. start = next;
  2293. }
  2294. }
  2295. break;
  2296. case XML_TOK_PARTIAL:
  2297. if (!ps_finalBuffer) {
  2298. *endPtr = start;
  2299. return XML_ERROR_NONE;
  2300. }
  2301. return XML_ERROR_UNCLOSED_TOKEN;
  2302. case XML_TOK_PARTIAL_CHAR:
  2303. if (!ps_finalBuffer) {
  2304. *endPtr = start;
  2305. return XML_ERROR_NONE;
  2306. }
  2307. return XML_ERROR_PARTIAL_CHAR;
  2308. }
  2309. processor = externalEntityContentProcessor;
  2310. tagLevel = 1;
  2311. return externalEntityContentProcessor(parser, start, end, endPtr);
  2312. }
  2313. static enum XML_Error PTRCALL
  2314. externalEntityContentProcessor(XML_Parser parser,
  2315. const char *start,
  2316. const char *end,
  2317. const char **endPtr)
  2318. {
  2319. enum XML_Error result = doContent(parser, 1, encoding, start, end,
  2320. endPtr, (XML_Bool)!ps_finalBuffer);
  2321. if (result == XML_ERROR_NONE) {
  2322. if (!storeRawNames(parser))
  2323. return XML_ERROR_NO_MEMORY;
  2324. }
  2325. return result;
  2326. }
  2327. static enum XML_Error
  2328. doContent(XML_Parser parser,
  2329. int startTagLevel,
  2330. const ENCODING *enc,
  2331. const char *s,
  2332. const char *end,
  2333. const char **nextPtr,
  2334. XML_Bool haveMore)
  2335. {
  2336. /* save one level of indirection */
  2337. DTD * const dtd = _dtd;
  2338. const char **eventPP;
  2339. const char **eventEndPP;
  2340. if (enc == encoding) {
  2341. eventPP = &eventPtr;
  2342. eventEndPP = &eventEndPtr;
  2343. }
  2344. else {
  2345. eventPP = &(openInternalEntities->internalEventPtr);
  2346. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  2347. }
  2348. *eventPP = s;
  2349. for (;;) {
  2350. const char *next = s; /* XmlContentTok doesn't always set the last arg */
  2351. int tok = XmlContentTok(enc, s, end, &next);
  2352. *eventEndPP = next;
  2353. switch (tok) {
  2354. case XML_TOK_TRAILING_CR:
  2355. if (haveMore) {
  2356. *nextPtr = s;
  2357. return XML_ERROR_NONE;
  2358. }
  2359. *eventEndPP = end;
  2360. if (characterDataHandler) {
  2361. XML_Char c = 0xA;
  2362. characterDataHandler(handlerArg, &c, 1);
  2363. }
  2364. else if (defaultHandler)
  2365. reportDefault(parser, enc, s, end);
  2366. /* We are at the end of the final buffer, should we check for
  2367. XML_SUSPENDED, XML_FINISHED?
  2368. */
  2369. if (startTagLevel == 0)
  2370. return XML_ERROR_NO_ELEMENTS;
  2371. if (tagLevel != startTagLevel)
  2372. return XML_ERROR_ASYNC_ENTITY;
  2373. *nextPtr = end;
  2374. return XML_ERROR_NONE;
  2375. case XML_TOK_NONE:
  2376. if (haveMore) {
  2377. *nextPtr = s;
  2378. return XML_ERROR_NONE;
  2379. }
  2380. if (startTagLevel > 0) {
  2381. if (tagLevel != startTagLevel)
  2382. return XML_ERROR_ASYNC_ENTITY;
  2383. *nextPtr = s;
  2384. return XML_ERROR_NONE;
  2385. }
  2386. return XML_ERROR_NO_ELEMENTS;
  2387. case XML_TOK_INVALID:
  2388. *eventPP = next;
  2389. return XML_ERROR_INVALID_TOKEN;
  2390. case XML_TOK_PARTIAL:
  2391. if (haveMore) {
  2392. *nextPtr = s;
  2393. return XML_ERROR_NONE;
  2394. }
  2395. return XML_ERROR_UNCLOSED_TOKEN;
  2396. case XML_TOK_PARTIAL_CHAR:
  2397. if (haveMore) {
  2398. *nextPtr = s;
  2399. return XML_ERROR_NONE;
  2400. }
  2401. return XML_ERROR_PARTIAL_CHAR;
  2402. case XML_TOK_ENTITY_REF:
  2403. {
  2404. const XML_Char *name;
  2405. ENTITY *entity;
  2406. XML_Char ch = (XML_Char) XmlPredefinedEntityName(enc,
  2407. s + enc->minBytesPerChar,
  2408. next - enc->minBytesPerChar);
  2409. if (ch) {
  2410. if (characterDataHandler)
  2411. characterDataHandler(handlerArg, &ch, 1);
  2412. else if (defaultHandler)
  2413. reportDefault(parser, enc, s, next);
  2414. break;
  2415. }
  2416. name = poolStoreString(&dtd->pool, enc,
  2417. s + enc->minBytesPerChar,
  2418. next - enc->minBytesPerChar);
  2419. if (!name)
  2420. return XML_ERROR_NO_MEMORY;
  2421. entity = (ENTITY *)lookup(parser, &dtd->generalEntities, name, 0);
  2422. poolDiscard(&dtd->pool);
  2423. /* First, determine if a check for an existing declaration is needed;
  2424. if yes, check that the entity exists, and that it is internal,
  2425. otherwise call the skipped entity or default handler.
  2426. */
  2427. if (!dtd->hasParamEntityRefs || dtd->standalone) {
  2428. if (!entity)
  2429. return XML_ERROR_UNDEFINED_ENTITY;
  2430. else if (!entity->is_internal)
  2431. return XML_ERROR_ENTITY_DECLARED_IN_PE;
  2432. }
  2433. else if (!entity) {
  2434. if (skippedEntityHandler)
  2435. skippedEntityHandler(handlerArg, name, 0);
  2436. else if (defaultHandler)
  2437. reportDefault(parser, enc, s, next);
  2438. break;
  2439. }
  2440. if (entity->open)
  2441. return XML_ERROR_RECURSIVE_ENTITY_REF;
  2442. if (entity->notation)
  2443. return XML_ERROR_BINARY_ENTITY_REF;
  2444. if (entity->textPtr) {
  2445. enum XML_Error result;
  2446. if (!defaultExpandInternalEntities) {
  2447. if (skippedEntityHandler)
  2448. skippedEntityHandler(handlerArg, entity->name, 0);
  2449. else if (defaultHandler)
  2450. reportDefault(parser, enc, s, next);
  2451. break;
  2452. }
  2453. result = processInternalEntity(parser, entity, XML_FALSE);
  2454. if (result != XML_ERROR_NONE)
  2455. return result;
  2456. }
  2457. else if (externalEntityRefHandler) {
  2458. const XML_Char *context;
  2459. entity->open = XML_TRUE;
  2460. context = getContext(parser);
  2461. entity->open = XML_FALSE;
  2462. if (!context)
  2463. return XML_ERROR_NO_MEMORY;
  2464. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  2465. context,
  2466. entity->base,
  2467. entity->systemId,
  2468. entity->publicId))
  2469. return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  2470. poolDiscard(&tempPool);
  2471. }
  2472. else if (defaultHandler)
  2473. reportDefault(parser, enc, s, next);
  2474. break;
  2475. }
  2476. case XML_TOK_START_TAG_NO_ATTS:
  2477. /* fall through */
  2478. case XML_TOK_START_TAG_WITH_ATTS:
  2479. {
  2480. TAG *tag;
  2481. enum XML_Error result;
  2482. XML_Char *toPtr;
  2483. if (freeTagList) {
  2484. tag = freeTagList;
  2485. freeTagList = freeTagList->parent;
  2486. }
  2487. else {
  2488. tag = (TAG *)MALLOC(sizeof(TAG));
  2489. if (!tag)
  2490. return XML_ERROR_NO_MEMORY;
  2491. tag->buf = (char *)MALLOC(INIT_TAG_BUF_SIZE);
  2492. if (!tag->buf) {
  2493. FREE(tag);
  2494. return XML_ERROR_NO_MEMORY;
  2495. }
  2496. tag->bufEnd = tag->buf + INIT_TAG_BUF_SIZE;
  2497. }
  2498. tag->bindings = NULL;
  2499. tag->parent = tagStack;
  2500. tagStack = tag;
  2501. tag->name.localPart = NULL;
  2502. tag->name.prefix = NULL;
  2503. tag->rawName = s + enc->minBytesPerChar;
  2504. tag->rawNameLength = XmlNameLength(enc, tag->rawName);
  2505. ++tagLevel;
  2506. {
  2507. const char *rawNameEnd = tag->rawName + tag->rawNameLength;
  2508. const char *fromPtr = tag->rawName;
  2509. toPtr = (XML_Char *)tag->buf;
  2510. for (;;) {
  2511. int bufSize;
  2512. int convLen;
  2513. const enum XML_Convert_Result convert_res = XmlConvert(enc,
  2514. &fromPtr, rawNameEnd,
  2515. (ICHAR **)&toPtr, (ICHAR *)tag->bufEnd - 1);
  2516. convLen = (int)(toPtr - (XML_Char *)tag->buf);
  2517. if ((fromPtr >= rawNameEnd) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE)) {
  2518. tag->name.strLen = convLen;
  2519. break;
  2520. }
  2521. bufSize = (int)(tag->bufEnd - tag->buf) << 1;
  2522. {
  2523. char *temp = (char *)REALLOC(tag->buf, bufSize);
  2524. if (temp == NULL)
  2525. return XML_ERROR_NO_MEMORY;
  2526. tag->buf = temp;
  2527. tag->bufEnd = temp + bufSize;
  2528. toPtr = (XML_Char *)temp + convLen;
  2529. }
  2530. }
  2531. }
  2532. tag->name.str = (XML_Char *)tag->buf;
  2533. *toPtr = XML_T('\0');
  2534. result = storeAtts(parser, enc, s, &(tag->name), &(tag->bindings));
  2535. if (result)
  2536. return result;
  2537. if (startElementHandler)
  2538. startElementHandler(handlerArg, tag->name.str,
  2539. (const XML_Char **)atts);
  2540. else if (defaultHandler)
  2541. reportDefault(parser, enc, s, next);
  2542. poolClear(&tempPool);
  2543. break;
  2544. }
  2545. case XML_TOK_EMPTY_ELEMENT_NO_ATTS:
  2546. /* fall through */
  2547. case XML_TOK_EMPTY_ELEMENT_WITH_ATTS:
  2548. {
  2549. const char *rawName = s + enc->minBytesPerChar;
  2550. enum XML_Error result;
  2551. BINDING *bindings = NULL;
  2552. XML_Bool noElmHandlers = XML_TRUE;
  2553. TAG_NAME name;
  2554. name.str = poolStoreString(&tempPool, enc, rawName,
  2555. rawName + XmlNameLength(enc, rawName));
  2556. if (!name.str)
  2557. return XML_ERROR_NO_MEMORY;
  2558. poolFinish(&tempPool);
  2559. result = storeAtts(parser, enc, s, &name, &bindings);
  2560. if (result != XML_ERROR_NONE) {
  2561. freeBindings(parser, bindings);
  2562. return result;
  2563. }
  2564. poolFinish(&tempPool);
  2565. if (startElementHandler) {
  2566. startElementHandler(handlerArg, name.str, (const XML_Char **)atts);
  2567. noElmHandlers = XML_FALSE;
  2568. }
  2569. if (endElementHandler) {
  2570. if (startElementHandler)
  2571. *eventPP = *eventEndPP;
  2572. endElementHandler(handlerArg, name.str);
  2573. noElmHandlers = XML_FALSE;
  2574. }
  2575. if (noElmHandlers && defaultHandler)
  2576. reportDefault(parser, enc, s, next);
  2577. poolClear(&tempPool);
  2578. freeBindings(parser, bindings);
  2579. }
  2580. if (tagLevel == 0)
  2581. return epilogProcessor(parser, next, end, nextPtr);
  2582. break;
  2583. case XML_TOK_END_TAG:
  2584. if (tagLevel == startTagLevel)
  2585. return XML_ERROR_ASYNC_ENTITY;
  2586. else {
  2587. int len;
  2588. const char *rawName;
  2589. TAG *tag = tagStack;
  2590. tagStack = tag->parent;
  2591. tag->parent = freeTagList;
  2592. freeTagList = tag;
  2593. rawName = s + enc->minBytesPerChar*2;
  2594. len = XmlNameLength(enc, rawName);
  2595. if (len != tag->rawNameLength
  2596. || memcmp(tag->rawName, rawName, len) != 0) {
  2597. *eventPP = rawName;
  2598. return XML_ERROR_TAG_MISMATCH;
  2599. }
  2600. --tagLevel;
  2601. if (endElementHandler) {
  2602. const XML_Char *localPart;
  2603. const XML_Char *prefix;
  2604. XML_Char *uri;
  2605. localPart = tag->name.localPart;
  2606. if (ns && localPart) {
  2607. /* localPart and prefix may have been overwritten in
  2608. tag->name.str, since this points to the binding->uri
  2609. buffer which gets re-used; so we have to add them again
  2610. */
  2611. uri = (XML_Char *)tag->name.str + tag->name.uriLen;
  2612. /* don't need to check for space - already done in storeAtts() */
  2613. while (*localPart) *uri++ = *localPart++;
  2614. prefix = (XML_Char *)tag->name.prefix;
  2615. if (ns_triplets && prefix) {
  2616. *uri++ = namespaceSeparator;
  2617. while (*prefix) *uri++ = *prefix++;
  2618. }
  2619. *uri = XML_T('\0');
  2620. }
  2621. endElementHandler(handlerArg, tag->name.str);
  2622. }
  2623. else if (defaultHandler)
  2624. reportDefault(parser, enc, s, next);
  2625. while (tag->bindings) {
  2626. BINDING *b = tag->bindings;
  2627. if (endNamespaceDeclHandler)
  2628. endNamespaceDeclHandler(handlerArg, b->prefix->name);
  2629. tag->bindings = tag->bindings->nextTagBinding;
  2630. b->nextTagBinding = freeBindingList;
  2631. freeBindingList = b;
  2632. b->prefix->binding = b->prevPrefixBinding;
  2633. }
  2634. if (tagLevel == 0)
  2635. return epilogProcessor(parser, next, end, nextPtr);
  2636. }
  2637. break;
  2638. case XML_TOK_CHAR_REF:
  2639. {
  2640. int n = XmlCharRefNumber(enc, s);
  2641. if (n < 0)
  2642. return XML_ERROR_BAD_CHAR_REF;
  2643. if (characterDataHandler) {
  2644. XML_Char buf[XML_ENCODE_MAX];
  2645. characterDataHandler(handlerArg, buf, XmlEncode(n, (ICHAR *)buf));
  2646. }
  2647. else if (defaultHandler)
  2648. reportDefault(parser, enc, s, next);
  2649. }
  2650. break;
  2651. case XML_TOK_XML_DECL:
  2652. return XML_ERROR_MISPLACED_XML_PI;
  2653. case XML_TOK_DATA_NEWLINE:
  2654. if (characterDataHandler) {
  2655. XML_Char c = 0xA;
  2656. characterDataHandler(handlerArg, &c, 1);
  2657. }
  2658. else if (defaultHandler)
  2659. reportDefault(parser, enc, s, next);
  2660. break;
  2661. case XML_TOK_CDATA_SECT_OPEN:
  2662. {
  2663. enum XML_Error result;
  2664. if (startCdataSectionHandler)
  2665. startCdataSectionHandler(handlerArg);
  2666. #if 0
  2667. /* Suppose you doing a transformation on a document that involves
  2668. changing only the character data. You set up a defaultHandler
  2669. and a characterDataHandler. The defaultHandler simply copies
  2670. characters through. The characterDataHandler does the
  2671. transformation and writes the characters out escaping them as
  2672. necessary. This case will fail to work if we leave out the
  2673. following two lines (because & and < inside CDATA sections will
  2674. be incorrectly escaped).
  2675. However, now we have a start/endCdataSectionHandler, so it seems
  2676. easier to let the user deal with this.
  2677. */
  2678. else if (characterDataHandler)
  2679. characterDataHandler(handlerArg, dataBuf, 0);
  2680. #endif
  2681. else if (defaultHandler)
  2682. reportDefault(parser, enc, s, next);
  2683. result = doCdataSection(parser, enc, &next, end, nextPtr, haveMore);
  2684. if (result != XML_ERROR_NONE)
  2685. return result;
  2686. else if (!next) {
  2687. processor = cdataSectionProcessor;
  2688. return result;
  2689. }
  2690. }
  2691. break;
  2692. case XML_TOK_TRAILING_RSQB:
  2693. if (haveMore) {
  2694. *nextPtr = s;
  2695. return XML_ERROR_NONE;
  2696. }
  2697. if (characterDataHandler) {
  2698. if (MUST_CONVERT(enc, s)) {
  2699. ICHAR *dataPtr = (ICHAR *)dataBuf;
  2700. XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
  2701. characterDataHandler(handlerArg, dataBuf,
  2702. (int)(dataPtr - (ICHAR *)dataBuf));
  2703. }
  2704. else
  2705. characterDataHandler(handlerArg,
  2706. (XML_Char *)s,
  2707. (int)((XML_Char *)end - (XML_Char *)s));
  2708. }
  2709. else if (defaultHandler)
  2710. reportDefault(parser, enc, s, end);
  2711. /* We are at the end of the final buffer, should we check for
  2712. XML_SUSPENDED, XML_FINISHED?
  2713. */
  2714. if (startTagLevel == 0) {
  2715. *eventPP = end;
  2716. return XML_ERROR_NO_ELEMENTS;
  2717. }
  2718. if (tagLevel != startTagLevel) {
  2719. *eventPP = end;
  2720. return XML_ERROR_ASYNC_ENTITY;
  2721. }
  2722. *nextPtr = end;
  2723. return XML_ERROR_NONE;
  2724. case XML_TOK_DATA_CHARS:
  2725. {
  2726. XML_CharacterDataHandler charDataHandler = characterDataHandler;
  2727. if (charDataHandler) {
  2728. if (MUST_CONVERT(enc, s)) {
  2729. for (;;) {
  2730. ICHAR *dataPtr = (ICHAR *)dataBuf;
  2731. const enum XML_Convert_Result convert_res = XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
  2732. *eventEndPP = s;
  2733. charDataHandler(handlerArg, dataBuf,
  2734. (int)(dataPtr - (ICHAR *)dataBuf));
  2735. if ((convert_res == XML_CONVERT_COMPLETED) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE))
  2736. break;
  2737. *eventPP = s;
  2738. }
  2739. }
  2740. else
  2741. charDataHandler(handlerArg,
  2742. (XML_Char *)s,
  2743. (int)((XML_Char *)next - (XML_Char *)s));
  2744. }
  2745. else if (defaultHandler)
  2746. reportDefault(parser, enc, s, next);
  2747. }
  2748. break;
  2749. case XML_TOK_PI:
  2750. if (!reportProcessingInstruction(parser, enc, s, next))
  2751. return XML_ERROR_NO_MEMORY;
  2752. break;
  2753. case XML_TOK_COMMENT:
  2754. if (!reportComment(parser, enc, s, next))
  2755. return XML_ERROR_NO_MEMORY;
  2756. break;
  2757. default:
  2758. if (defaultHandler)
  2759. reportDefault(parser, enc, s, next);
  2760. break;
  2761. }
  2762. *eventPP = s = next;
  2763. switch (ps_parsing) {
  2764. case XML_SUSPENDED:
  2765. *nextPtr = next;
  2766. return XML_ERROR_NONE;
  2767. case XML_FINISHED:
  2768. return XML_ERROR_ABORTED;
  2769. default: ;
  2770. }
  2771. }
  2772. /* not reached */
  2773. }
  2774. /* This function does not call free() on the allocated memory, merely
  2775. * moving it to the parser's freeBindingList where it can be freed or
  2776. * reused as appropriate.
  2777. */
  2778. static void
  2779. freeBindings(XML_Parser parser, BINDING *bindings)
  2780. {
  2781. while (bindings) {
  2782. BINDING *b = bindings;
  2783. /* startNamespaceDeclHandler will have been called for this
  2784. * binding in addBindings(), so call the end handler now.
  2785. */
  2786. if (endNamespaceDeclHandler)
  2787. endNamespaceDeclHandler(handlerArg, b->prefix->name);
  2788. bindings = bindings->nextTagBinding;
  2789. b->nextTagBinding = freeBindingList;
  2790. freeBindingList = b;
  2791. b->prefix->binding = b->prevPrefixBinding;
  2792. }
  2793. }
  2794. /* Precondition: all arguments must be non-NULL;
  2795. Purpose:
  2796. - normalize attributes
  2797. - check attributes for well-formedness
  2798. - generate namespace aware attribute names (URI, prefix)
  2799. - build list of attributes for startElementHandler
  2800. - default attributes
  2801. - process namespace declarations (check and report them)
  2802. - generate namespace aware element name (URI, prefix)
  2803. */
  2804. static enum XML_Error
  2805. storeAtts(XML_Parser parser, const ENCODING *enc,
  2806. const char *attStr, TAG_NAME *tagNamePtr,
  2807. BINDING **bindingsPtr)
  2808. {
  2809. DTD * const dtd = _dtd; /* save one level of indirection */
  2810. ELEMENT_TYPE *elementType;
  2811. int nDefaultAtts;
  2812. const XML_Char **appAtts; /* the attribute list for the application */
  2813. int attIndex = 0;
  2814. int prefixLen;
  2815. int i;
  2816. int n;
  2817. XML_Char *uri;
  2818. int nPrefixes = 0;
  2819. BINDING *binding;
  2820. const XML_Char *localPart;
  2821. /* lookup the element type name */
  2822. elementType = (ELEMENT_TYPE *)lookup(parser, &dtd->elementTypes, tagNamePtr->str,0);
  2823. if (!elementType) {
  2824. const XML_Char *name = poolCopyString(&dtd->pool, tagNamePtr->str);
  2825. if (!name)
  2826. return XML_ERROR_NO_MEMORY;
  2827. elementType = (ELEMENT_TYPE *)lookup(parser, &dtd->elementTypes, name,
  2828. sizeof(ELEMENT_TYPE));
  2829. if (!elementType)
  2830. return XML_ERROR_NO_MEMORY;
  2831. if (ns && !setElementTypePrefix(parser, elementType))
  2832. return XML_ERROR_NO_MEMORY;
  2833. }
  2834. nDefaultAtts = elementType->nDefaultAtts;
  2835. /* get the attributes from the tokenizer */
  2836. n = XmlGetAttributes(enc, attStr, attsSize, atts);
  2837. if (n + nDefaultAtts > attsSize) {
  2838. int oldAttsSize = attsSize;
  2839. ATTRIBUTE *temp;
  2840. #ifdef XML_ATTR_INFO
  2841. XML_AttrInfo *temp2;
  2842. #endif
  2843. attsSize = n + nDefaultAtts + INIT_ATTS_SIZE;
  2844. temp = (ATTRIBUTE *)REALLOC((void *)atts, attsSize * sizeof(ATTRIBUTE));
  2845. if (temp == NULL)
  2846. return XML_ERROR_NO_MEMORY;
  2847. atts = temp;
  2848. #ifdef XML_ATTR_INFO
  2849. temp2 = (XML_AttrInfo *)REALLOC((void *)attInfo, attsSize * sizeof(XML_AttrInfo));
  2850. if (temp2 == NULL)
  2851. return XML_ERROR_NO_MEMORY;
  2852. attInfo = temp2;
  2853. #endif
  2854. if (n > oldAttsSize)
  2855. XmlGetAttributes(enc, attStr, n, atts);
  2856. }
  2857. appAtts = (const XML_Char **)atts;
  2858. for (i = 0; i < n; i++) {
  2859. ATTRIBUTE *currAtt = &atts[i];
  2860. #ifdef XML_ATTR_INFO
  2861. XML_AttrInfo *currAttInfo = &attInfo[i];
  2862. #endif
  2863. /* add the name and value to the attribute list */
  2864. ATTRIBUTE_ID *attId = getAttributeId(parser, enc, currAtt->name,
  2865. currAtt->name
  2866. + XmlNameLength(enc, currAtt->name));
  2867. if (!attId)
  2868. return XML_ERROR_NO_MEMORY;
  2869. #ifdef XML_ATTR_INFO
  2870. currAttInfo->nameStart = parseEndByteIndex - (parseEndPtr - currAtt->name);
  2871. currAttInfo->nameEnd = currAttInfo->nameStart +
  2872. XmlNameLength(enc, currAtt->name);
  2873. currAttInfo->valueStart = parseEndByteIndex -
  2874. (parseEndPtr - currAtt->valuePtr);
  2875. currAttInfo->valueEnd = parseEndByteIndex - (parseEndPtr - currAtt->valueEnd);
  2876. #endif
  2877. /* Detect duplicate attributes by their QNames. This does not work when
  2878. namespace processing is turned on and different prefixes for the same
  2879. namespace are used. For this case we have a check further down.
  2880. */
  2881. if ((attId->name)[-1]) {
  2882. if (enc == encoding)
  2883. eventPtr = atts[i].name;
  2884. return XML_ERROR_DUPLICATE_ATTRIBUTE;
  2885. }
  2886. (attId->name)[-1] = 1;
  2887. appAtts[attIndex++] = attId->name;
  2888. if (!atts[i].normalized) {
  2889. enum XML_Error result;
  2890. XML_Bool isCdata = XML_TRUE;
  2891. /* figure out whether declared as other than CDATA */
  2892. if (attId->maybeTokenized) {
  2893. int j;
  2894. for (j = 0; j < nDefaultAtts; j++) {
  2895. if (attId == elementType->defaultAtts[j].id) {
  2896. isCdata = elementType->defaultAtts[j].isCdata;
  2897. break;
  2898. }
  2899. }
  2900. }
  2901. /* normalize the attribute value */
  2902. result = storeAttributeValue(parser, enc, isCdata,
  2903. atts[i].valuePtr, atts[i].valueEnd,
  2904. &tempPool);
  2905. if (result)
  2906. return result;
  2907. appAtts[attIndex] = poolStart(&tempPool);
  2908. poolFinish(&tempPool);
  2909. }
  2910. else {
  2911. /* the value did not need normalizing */
  2912. appAtts[attIndex] = poolStoreString(&tempPool, enc, atts[i].valuePtr,
  2913. atts[i].valueEnd);
  2914. if (appAtts[attIndex] == 0)
  2915. return XML_ERROR_NO_MEMORY;
  2916. poolFinish(&tempPool);
  2917. }
  2918. /* handle prefixed attribute names */
  2919. if (attId->prefix) {
  2920. if (attId->xmlns) {
  2921. /* deal with namespace declarations here */
  2922. enum XML_Error result = addBinding(parser, attId->prefix, attId,
  2923. appAtts[attIndex], bindingsPtr);
  2924. if (result)
  2925. return result;
  2926. --attIndex;
  2927. }
  2928. else {
  2929. /* deal with other prefixed names later */
  2930. attIndex++;
  2931. nPrefixes++;
  2932. (attId->name)[-1] = 2;
  2933. }
  2934. }
  2935. else
  2936. attIndex++;
  2937. }
  2938. /* set-up for XML_GetSpecifiedAttributeCount and XML_GetIdAttributeIndex */
  2939. nSpecifiedAtts = attIndex;
  2940. if (elementType->idAtt && (elementType->idAtt->name)[-1]) {
  2941. for (i = 0; i < attIndex; i += 2)
  2942. if (appAtts[i] == elementType->idAtt->name) {
  2943. idAttIndex = i;
  2944. break;
  2945. }
  2946. }
  2947. else
  2948. idAttIndex = -1;
  2949. /* do attribute defaulting */
  2950. for (i = 0; i < nDefaultAtts; i++) {
  2951. const DEFAULT_ATTRIBUTE *da = elementType->defaultAtts + i;
  2952. if (!(da->id->name)[-1] && da->value) {
  2953. if (da->id->prefix) {
  2954. if (da->id->xmlns) {
  2955. enum XML_Error result = addBinding(parser, da->id->prefix, da->id,
  2956. da->value, bindingsPtr);
  2957. if (result)
  2958. return result;
  2959. }
  2960. else {
  2961. (da->id->name)[-1] = 2;
  2962. nPrefixes++;
  2963. appAtts[attIndex++] = da->id->name;
  2964. appAtts[attIndex++] = da->value;
  2965. }
  2966. }
  2967. else {
  2968. (da->id->name)[-1] = 1;
  2969. appAtts[attIndex++] = da->id->name;
  2970. appAtts[attIndex++] = da->value;
  2971. }
  2972. }
  2973. }
  2974. appAtts[attIndex] = 0;
  2975. /* expand prefixed attribute names, check for duplicates,
  2976. and clear flags that say whether attributes were specified */
  2977. i = 0;
  2978. if (nPrefixes) {
  2979. int j; /* hash table index */
  2980. unsigned long version = nsAttsVersion;
  2981. int nsAttsSize = (int)1 << nsAttsPower;
  2982. /* size of hash table must be at least 2 * (# of prefixed attributes) */
  2983. if ((nPrefixes << 1) >> nsAttsPower) { /* true for nsAttsPower = 0 */
  2984. NS_ATT *temp;
  2985. /* hash table size must also be a power of 2 and >= 8 */
  2986. while (nPrefixes >> nsAttsPower++);
  2987. if (nsAttsPower < 3)
  2988. nsAttsPower = 3;
  2989. nsAttsSize = (int)1 << nsAttsPower;
  2990. temp = (NS_ATT *)REALLOC(nsAtts, nsAttsSize * sizeof(NS_ATT));
  2991. if (!temp)
  2992. return XML_ERROR_NO_MEMORY;
  2993. nsAtts = temp;
  2994. version = 0; /* force re-initialization of nsAtts hash table */
  2995. }
  2996. /* using a version flag saves us from initializing nsAtts every time */
  2997. if (!version) { /* initialize version flags when version wraps around */
  2998. version = INIT_ATTS_VERSION;
  2999. for (j = nsAttsSize; j != 0; )
  3000. nsAtts[--j].version = version;
  3001. }
  3002. nsAttsVersion = --version;
  3003. /* expand prefixed names and check for duplicates */
  3004. for (; i < attIndex; i += 2) {
  3005. const XML_Char *s = appAtts[i];
  3006. if (s[-1] == 2) { /* prefixed */
  3007. ATTRIBUTE_ID *id;
  3008. const BINDING *b;
  3009. unsigned long uriHash;
  3010. struct siphash sip_state;
  3011. struct sipkey sip_key;
  3012. copy_salt_to_sipkey(parser, &sip_key);
  3013. sip24_init(&sip_state, &sip_key);
  3014. ((XML_Char *)s)[-1] = 0; /* clear flag */
  3015. id = (ATTRIBUTE_ID *)lookup(parser, &dtd->attributeIds, s, 0);
  3016. if (!id || !id->prefix)
  3017. return XML_ERROR_NO_MEMORY;
  3018. b = id->prefix->binding;
  3019. if (!b)
  3020. return XML_ERROR_UNBOUND_PREFIX;
  3021. for (j = 0; j < b->uriLen; j++) {
  3022. const XML_Char c = b->uri[j];
  3023. if (!poolAppendChar(&tempPool, c))
  3024. return XML_ERROR_NO_MEMORY;
  3025. }
  3026. sip24_update(&sip_state, b->uri, b->uriLen * sizeof(XML_Char));
  3027. while (*s++ != XML_T(ASCII_COLON))
  3028. ;
  3029. sip24_update(&sip_state, s, keylen(s) * sizeof(XML_Char));
  3030. do { /* copies null terminator */
  3031. if (!poolAppendChar(&tempPool, *s))
  3032. return XML_ERROR_NO_MEMORY;
  3033. } while (*s++);
  3034. uriHash = (unsigned long)sip24_final(&sip_state);
  3035. { /* Check hash table for duplicate of expanded name (uriName).
  3036. Derived from code in lookup(parser, HASH_TABLE *table, ...).
  3037. */
  3038. unsigned char step = 0;
  3039. unsigned long mask = nsAttsSize - 1;
  3040. j = uriHash & mask; /* index into hash table */
  3041. while (nsAtts[j].version == version) {
  3042. /* for speed we compare stored hash values first */
  3043. if (uriHash == nsAtts[j].hash) {
  3044. const XML_Char *s1 = poolStart(&tempPool);
  3045. const XML_Char *s2 = nsAtts[j].uriName;
  3046. /* s1 is null terminated, but not s2 */
  3047. for (; *s1 == *s2 && *s1 != 0; s1++, s2++);
  3048. if (*s1 == 0)
  3049. return XML_ERROR_DUPLICATE_ATTRIBUTE;
  3050. }
  3051. if (!step)
  3052. step = PROBE_STEP(uriHash, mask, nsAttsPower);
  3053. j < step ? (j += nsAttsSize - step) : (j -= step);
  3054. }
  3055. }
  3056. if (ns_triplets) { /* append namespace separator and prefix */
  3057. tempPool.ptr[-1] = namespaceSeparator;
  3058. s = b->prefix->name;
  3059. do {
  3060. if (!poolAppendChar(&tempPool, *s))
  3061. return XML_ERROR_NO_MEMORY;
  3062. } while (*s++);
  3063. }
  3064. /* store expanded name in attribute list */
  3065. s = poolStart(&tempPool);
  3066. poolFinish(&tempPool);
  3067. appAtts[i] = s;
  3068. /* fill empty slot with new version, uriName and hash value */
  3069. nsAtts[j].version = version;
  3070. nsAtts[j].hash = uriHash;
  3071. nsAtts[j].uriName = s;
  3072. if (!--nPrefixes) {
  3073. i += 2;
  3074. break;
  3075. }
  3076. }
  3077. else /* not prefixed */
  3078. ((XML_Char *)s)[-1] = 0; /* clear flag */
  3079. }
  3080. }
  3081. /* clear flags for the remaining attributes */
  3082. for (; i < attIndex; i += 2)
  3083. ((XML_Char *)(appAtts[i]))[-1] = 0;
  3084. for (binding = *bindingsPtr; binding; binding = binding->nextTagBinding)
  3085. binding->attId->name[-1] = 0;
  3086. if (!ns)
  3087. return XML_ERROR_NONE;
  3088. /* expand the element type name */
  3089. if (elementType->prefix) {
  3090. binding = elementType->prefix->binding;
  3091. if (!binding)
  3092. return XML_ERROR_UNBOUND_PREFIX;
  3093. localPart = tagNamePtr->str;
  3094. while (*localPart++ != XML_T(ASCII_COLON))
  3095. ;
  3096. }
  3097. else if (dtd->defaultPrefix.binding) {
  3098. binding = dtd->defaultPrefix.binding;
  3099. localPart = tagNamePtr->str;
  3100. }
  3101. else
  3102. return XML_ERROR_NONE;
  3103. prefixLen = 0;
  3104. if (ns_triplets && binding->prefix->name) {
  3105. for (; binding->prefix->name[prefixLen++];)
  3106. ; /* prefixLen includes null terminator */
  3107. }
  3108. tagNamePtr->localPart = localPart;
  3109. tagNamePtr->uriLen = binding->uriLen;
  3110. tagNamePtr->prefix = binding->prefix->name;
  3111. tagNamePtr->prefixLen = prefixLen;
  3112. for (i = 0; localPart[i++];)
  3113. ; /* i includes null terminator */
  3114. n = i + binding->uriLen + prefixLen;
  3115. if (n > binding->uriAlloc) {
  3116. TAG *p;
  3117. uri = (XML_Char *)MALLOC((n + EXPAND_SPARE) * sizeof(XML_Char));
  3118. if (!uri)
  3119. return XML_ERROR_NO_MEMORY;
  3120. binding->uriAlloc = n + EXPAND_SPARE;
  3121. memcpy(uri, binding->uri, binding->uriLen * sizeof(XML_Char));
  3122. for (p = tagStack; p; p = p->parent)
  3123. if (p->name.str == binding->uri)
  3124. p->name.str = uri;
  3125. FREE(binding->uri);
  3126. binding->uri = uri;
  3127. }
  3128. /* if namespaceSeparator != '\0' then uri includes it already */
  3129. uri = binding->uri + binding->uriLen;
  3130. memcpy(uri, localPart, i * sizeof(XML_Char));
  3131. /* we always have a namespace separator between localPart and prefix */
  3132. if (prefixLen) {
  3133. uri += i - 1;
  3134. *uri = namespaceSeparator; /* replace null terminator */
  3135. memcpy(uri + 1, binding->prefix->name, prefixLen * sizeof(XML_Char));
  3136. }
  3137. tagNamePtr->str = binding->uri;
  3138. return XML_ERROR_NONE;
  3139. }
  3140. /* addBinding() overwrites the value of prefix->binding without checking.
  3141. Therefore one must keep track of the old value outside of addBinding().
  3142. */
  3143. static enum XML_Error
  3144. addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
  3145. const XML_Char *uri, BINDING **bindingsPtr)
  3146. {
  3147. static const XML_Char xmlNamespace[] = {
  3148. ASCII_h, ASCII_t, ASCII_t, ASCII_p, ASCII_COLON, ASCII_SLASH, ASCII_SLASH,
  3149. ASCII_w, ASCII_w, ASCII_w, ASCII_PERIOD, ASCII_w, ASCII_3, ASCII_PERIOD,
  3150. ASCII_o, ASCII_r, ASCII_g, ASCII_SLASH, ASCII_X, ASCII_M, ASCII_L,
  3151. ASCII_SLASH, ASCII_1, ASCII_9, ASCII_9, ASCII_8, ASCII_SLASH,
  3152. ASCII_n, ASCII_a, ASCII_m, ASCII_e, ASCII_s, ASCII_p, ASCII_a, ASCII_c,
  3153. ASCII_e, '\0'
  3154. };
  3155. static const int xmlLen =
  3156. (int)sizeof(xmlNamespace)/sizeof(XML_Char) - 1;
  3157. static const XML_Char xmlnsNamespace[] = {
  3158. ASCII_h, ASCII_t, ASCII_t, ASCII_p, ASCII_COLON, ASCII_SLASH, ASCII_SLASH,
  3159. ASCII_w, ASCII_w, ASCII_w, ASCII_PERIOD, ASCII_w, ASCII_3, ASCII_PERIOD,
  3160. ASCII_o, ASCII_r, ASCII_g, ASCII_SLASH, ASCII_2, ASCII_0, ASCII_0,
  3161. ASCII_0, ASCII_SLASH, ASCII_x, ASCII_m, ASCII_l, ASCII_n, ASCII_s,
  3162. ASCII_SLASH, '\0'
  3163. };
  3164. static const int xmlnsLen =
  3165. (int)sizeof(xmlnsNamespace)/sizeof(XML_Char) - 1;
  3166. XML_Bool mustBeXML = XML_FALSE;
  3167. XML_Bool isXML = XML_TRUE;
  3168. XML_Bool isXMLNS = XML_TRUE;
  3169. BINDING *b;
  3170. int len;
  3171. /* empty URI is only valid for default namespace per XML NS 1.0 (not 1.1) */
  3172. if (*uri == XML_T('\0') && prefix->name)
  3173. return XML_ERROR_UNDECLARING_PREFIX;
  3174. if (prefix->name
  3175. && prefix->name[0] == XML_T(ASCII_x)
  3176. && prefix->name[1] == XML_T(ASCII_m)
  3177. && prefix->name[2] == XML_T(ASCII_l)) {
  3178. /* Not allowed to bind xmlns */
  3179. if (prefix->name[3] == XML_T(ASCII_n)
  3180. && prefix->name[4] == XML_T(ASCII_s)
  3181. && prefix->name[5] == XML_T('\0'))
  3182. return XML_ERROR_RESERVED_PREFIX_XMLNS;
  3183. if (prefix->name[3] == XML_T('\0'))
  3184. mustBeXML = XML_TRUE;
  3185. }
  3186. for (len = 0; uri[len]; len++) {
  3187. if (isXML && (len > xmlLen || uri[len] != xmlNamespace[len]))
  3188. isXML = XML_FALSE;
  3189. if (!mustBeXML && isXMLNS
  3190. && (len > xmlnsLen || uri[len] != xmlnsNamespace[len]))
  3191. isXMLNS = XML_FALSE;
  3192. }
  3193. isXML = isXML && len == xmlLen;
  3194. isXMLNS = isXMLNS && len == xmlnsLen;
  3195. if (mustBeXML != isXML)
  3196. return mustBeXML ? XML_ERROR_RESERVED_PREFIX_XML
  3197. : XML_ERROR_RESERVED_NAMESPACE_URI;
  3198. if (isXMLNS)
  3199. return XML_ERROR_RESERVED_NAMESPACE_URI;
  3200. if (namespaceSeparator)
  3201. len++;
  3202. if (freeBindingList) {
  3203. b = freeBindingList;
  3204. if (len > b->uriAlloc) {
  3205. XML_Char *temp = (XML_Char *)REALLOC(b->uri,
  3206. sizeof(XML_Char) * (len + EXPAND_SPARE));
  3207. if (temp == NULL)
  3208. return XML_ERROR_NO_MEMORY;
  3209. b->uri = temp;
  3210. b->uriAlloc = len + EXPAND_SPARE;
  3211. }
  3212. freeBindingList = b->nextTagBinding;
  3213. }
  3214. else {
  3215. b = (BINDING *)MALLOC(sizeof(BINDING));
  3216. if (!b)
  3217. return XML_ERROR_NO_MEMORY;
  3218. b->uri = (XML_Char *)MALLOC(sizeof(XML_Char) * (len + EXPAND_SPARE));
  3219. if (!b->uri) {
  3220. FREE(b);
  3221. return XML_ERROR_NO_MEMORY;
  3222. }
  3223. b->uriAlloc = len + EXPAND_SPARE;
  3224. }
  3225. b->uriLen = len;
  3226. memcpy(b->uri, uri, len * sizeof(XML_Char));
  3227. if (namespaceSeparator)
  3228. b->uri[len - 1] = namespaceSeparator;
  3229. b->prefix = prefix;
  3230. b->attId = attId;
  3231. b->prevPrefixBinding = prefix->binding;
  3232. /* NULL binding when default namespace undeclared */
  3233. if (*uri == XML_T('\0') && prefix == &_dtd->defaultPrefix)
  3234. prefix->binding = NULL;
  3235. else
  3236. prefix->binding = b;
  3237. b->nextTagBinding = *bindingsPtr;
  3238. *bindingsPtr = b;
  3239. /* if attId == NULL then we are not starting a namespace scope */
  3240. if (attId && startNamespaceDeclHandler)
  3241. startNamespaceDeclHandler(handlerArg, prefix->name,
  3242. prefix->binding ? uri : 0);
  3243. return XML_ERROR_NONE;
  3244. }
  3245. /* The idea here is to avoid using stack for each CDATA section when
  3246. the whole file is parsed with one call.
  3247. */
  3248. static enum XML_Error PTRCALL
  3249. cdataSectionProcessor(XML_Parser parser,
  3250. const char *start,
  3251. const char *end,
  3252. const char **endPtr)
  3253. {
  3254. enum XML_Error result = doCdataSection(parser, encoding, &start, end,
  3255. endPtr, (XML_Bool)!ps_finalBuffer);
  3256. if (result != XML_ERROR_NONE)
  3257. return result;
  3258. if (start) {
  3259. if (parentParser) { /* we are parsing an external entity */
  3260. processor = externalEntityContentProcessor;
  3261. return externalEntityContentProcessor(parser, start, end, endPtr);
  3262. }
  3263. else {
  3264. processor = contentProcessor;
  3265. return contentProcessor(parser, start, end, endPtr);
  3266. }
  3267. }
  3268. return result;
  3269. }
  3270. /* startPtr gets set to non-null if the section is closed, and to null if
  3271. the section is not yet closed.
  3272. */
  3273. static enum XML_Error
  3274. doCdataSection(XML_Parser parser,
  3275. const ENCODING *enc,
  3276. const char **startPtr,
  3277. const char *end,
  3278. const char **nextPtr,
  3279. XML_Bool haveMore)
  3280. {
  3281. const char *s = *startPtr;
  3282. const char **eventPP;
  3283. const char **eventEndPP;
  3284. if (enc == encoding) {
  3285. eventPP = &eventPtr;
  3286. *eventPP = s;
  3287. eventEndPP = &eventEndPtr;
  3288. }
  3289. else {
  3290. eventPP = &(openInternalEntities->internalEventPtr);
  3291. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  3292. }
  3293. *eventPP = s;
  3294. *startPtr = NULL;
  3295. for (;;) {
  3296. const char *next;
  3297. int tok = XmlCdataSectionTok(enc, s, end, &next);
  3298. *eventEndPP = next;
  3299. switch (tok) {
  3300. case XML_TOK_CDATA_SECT_CLOSE:
  3301. if (endCdataSectionHandler)
  3302. endCdataSectionHandler(handlerArg);
  3303. #if 0
  3304. /* see comment under XML_TOK_CDATA_SECT_OPEN */
  3305. else if (characterDataHandler)
  3306. characterDataHandler(handlerArg, dataBuf, 0);
  3307. #endif
  3308. else if (defaultHandler)
  3309. reportDefault(parser, enc, s, next);
  3310. *startPtr = next;
  3311. *nextPtr = next;
  3312. if (ps_parsing == XML_FINISHED)
  3313. return XML_ERROR_ABORTED;
  3314. else
  3315. return XML_ERROR_NONE;
  3316. case XML_TOK_DATA_NEWLINE:
  3317. if (characterDataHandler) {
  3318. XML_Char c = 0xA;
  3319. characterDataHandler(handlerArg, &c, 1);
  3320. }
  3321. else if (defaultHandler)
  3322. reportDefault(parser, enc, s, next);
  3323. break;
  3324. case XML_TOK_DATA_CHARS:
  3325. {
  3326. XML_CharacterDataHandler charDataHandler = characterDataHandler;
  3327. if (charDataHandler) {
  3328. if (MUST_CONVERT(enc, s)) {
  3329. for (;;) {
  3330. ICHAR *dataPtr = (ICHAR *)dataBuf;
  3331. const enum XML_Convert_Result convert_res = XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
  3332. *eventEndPP = next;
  3333. charDataHandler(handlerArg, dataBuf,
  3334. (int)(dataPtr - (ICHAR *)dataBuf));
  3335. if ((convert_res == XML_CONVERT_COMPLETED) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE))
  3336. break;
  3337. *eventPP = s;
  3338. }
  3339. }
  3340. else
  3341. charDataHandler(handlerArg,
  3342. (XML_Char *)s,
  3343. (int)((XML_Char *)next - (XML_Char *)s));
  3344. }
  3345. else if (defaultHandler)
  3346. reportDefault(parser, enc, s, next);
  3347. }
  3348. break;
  3349. case XML_TOK_INVALID:
  3350. *eventPP = next;
  3351. return XML_ERROR_INVALID_TOKEN;
  3352. case XML_TOK_PARTIAL_CHAR:
  3353. if (haveMore) {
  3354. *nextPtr = s;
  3355. return XML_ERROR_NONE;
  3356. }
  3357. return XML_ERROR_PARTIAL_CHAR;
  3358. case XML_TOK_PARTIAL:
  3359. case XML_TOK_NONE:
  3360. if (haveMore) {
  3361. *nextPtr = s;
  3362. return XML_ERROR_NONE;
  3363. }
  3364. return XML_ERROR_UNCLOSED_CDATA_SECTION;
  3365. default:
  3366. *eventPP = next;
  3367. return XML_ERROR_UNEXPECTED_STATE;
  3368. }
  3369. *eventPP = s = next;
  3370. switch (ps_parsing) {
  3371. case XML_SUSPENDED:
  3372. *nextPtr = next;
  3373. return XML_ERROR_NONE;
  3374. case XML_FINISHED:
  3375. return XML_ERROR_ABORTED;
  3376. default: ;
  3377. }
  3378. }
  3379. /* not reached */
  3380. }
  3381. #ifdef XML_DTD
  3382. /* The idea here is to avoid using stack for each IGNORE section when
  3383. the whole file is parsed with one call.
  3384. */
  3385. static enum XML_Error PTRCALL
  3386. ignoreSectionProcessor(XML_Parser parser,
  3387. const char *start,
  3388. const char *end,
  3389. const char **endPtr)
  3390. {
  3391. enum XML_Error result = doIgnoreSection(parser, encoding, &start, end,
  3392. endPtr, (XML_Bool)!ps_finalBuffer);
  3393. if (result != XML_ERROR_NONE)
  3394. return result;
  3395. if (start) {
  3396. processor = prologProcessor;
  3397. return prologProcessor(parser, start, end, endPtr);
  3398. }
  3399. return result;
  3400. }
  3401. /* startPtr gets set to non-null is the section is closed, and to null
  3402. if the section is not yet closed.
  3403. */
  3404. static enum XML_Error
  3405. doIgnoreSection(XML_Parser parser,
  3406. const ENCODING *enc,
  3407. const char **startPtr,
  3408. const char *end,
  3409. const char **nextPtr,
  3410. XML_Bool haveMore)
  3411. {
  3412. const char *next;
  3413. int tok;
  3414. const char *s = *startPtr;
  3415. const char **eventPP;
  3416. const char **eventEndPP;
  3417. if (enc == encoding) {
  3418. eventPP = &eventPtr;
  3419. *eventPP = s;
  3420. eventEndPP = &eventEndPtr;
  3421. }
  3422. else {
  3423. eventPP = &(openInternalEntities->internalEventPtr);
  3424. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  3425. }
  3426. *eventPP = s;
  3427. *startPtr = NULL;
  3428. tok = XmlIgnoreSectionTok(enc, s, end, &next);
  3429. *eventEndPP = next;
  3430. switch (tok) {
  3431. case XML_TOK_IGNORE_SECT:
  3432. if (defaultHandler)
  3433. reportDefault(parser, enc, s, next);
  3434. *startPtr = next;
  3435. *nextPtr = next;
  3436. if (ps_parsing == XML_FINISHED)
  3437. return XML_ERROR_ABORTED;
  3438. else
  3439. return XML_ERROR_NONE;
  3440. case XML_TOK_INVALID:
  3441. *eventPP = next;
  3442. return XML_ERROR_INVALID_TOKEN;
  3443. case XML_TOK_PARTIAL_CHAR:
  3444. if (haveMore) {
  3445. *nextPtr = s;
  3446. return XML_ERROR_NONE;
  3447. }
  3448. return XML_ERROR_PARTIAL_CHAR;
  3449. case XML_TOK_PARTIAL:
  3450. case XML_TOK_NONE:
  3451. if (haveMore) {
  3452. *nextPtr = s;
  3453. return XML_ERROR_NONE;
  3454. }
  3455. return XML_ERROR_SYNTAX; /* XML_ERROR_UNCLOSED_IGNORE_SECTION */
  3456. default:
  3457. *eventPP = next;
  3458. return XML_ERROR_UNEXPECTED_STATE;
  3459. }
  3460. /* not reached */
  3461. }
  3462. #endif /* XML_DTD */
  3463. static enum XML_Error
  3464. initializeEncoding(XML_Parser parser)
  3465. {
  3466. const char *s;
  3467. #ifdef XML_UNICODE
  3468. char encodingBuf[128];
  3469. if (!protocolEncodingName)
  3470. s = NULL;
  3471. else {
  3472. int i;
  3473. for (i = 0; protocolEncodingName[i]; i++) {
  3474. if (i == sizeof(encodingBuf) - 1
  3475. || (protocolEncodingName[i] & ~0x7f) != 0) {
  3476. encodingBuf[0] = '\0';
  3477. break;
  3478. }
  3479. encodingBuf[i] = (char)protocolEncodingName[i];
  3480. }
  3481. encodingBuf[i] = '\0';
  3482. s = encodingBuf;
  3483. }
  3484. #else
  3485. s = protocolEncodingName;
  3486. #endif
  3487. if ((ns ? XmlInitEncodingNS : XmlInitEncoding)(&initEncoding, &encoding, s))
  3488. return XML_ERROR_NONE;
  3489. return handleUnknownEncoding(parser, protocolEncodingName);
  3490. }
  3491. static enum XML_Error
  3492. processXmlDecl(XML_Parser parser, int isGeneralTextEntity,
  3493. const char *s, const char *next)
  3494. {
  3495. const char *encodingName = NULL;
  3496. const XML_Char *storedEncName = NULL;
  3497. const ENCODING *newEncoding = NULL;
  3498. const char *version = NULL;
  3499. const char *versionend;
  3500. const XML_Char *storedversion = NULL;
  3501. int standalone = -1;
  3502. if (!(ns
  3503. ? XmlParseXmlDeclNS
  3504. : XmlParseXmlDecl)(isGeneralTextEntity,
  3505. encoding,
  3506. s,
  3507. next,
  3508. &eventPtr,
  3509. &version,
  3510. &versionend,
  3511. &encodingName,
  3512. &newEncoding,
  3513. &standalone)) {
  3514. if (isGeneralTextEntity)
  3515. return XML_ERROR_TEXT_DECL;
  3516. else
  3517. return XML_ERROR_XML_DECL;
  3518. }
  3519. if (!isGeneralTextEntity && standalone == 1) {
  3520. _dtd->standalone = XML_TRUE;
  3521. #ifdef XML_DTD
  3522. if (paramEntityParsing == XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)
  3523. paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
  3524. #endif /* XML_DTD */
  3525. }
  3526. if (xmlDeclHandler) {
  3527. if (encodingName != NULL) {
  3528. storedEncName = poolStoreString(&temp2Pool,
  3529. encoding,
  3530. encodingName,
  3531. encodingName
  3532. + XmlNameLength(encoding, encodingName));
  3533. if (!storedEncName)
  3534. return XML_ERROR_NO_MEMORY;
  3535. poolFinish(&temp2Pool);
  3536. }
  3537. if (version) {
  3538. storedversion = poolStoreString(&temp2Pool,
  3539. encoding,
  3540. version,
  3541. versionend - encoding->minBytesPerChar);
  3542. if (!storedversion)
  3543. return XML_ERROR_NO_MEMORY;
  3544. }
  3545. xmlDeclHandler(handlerArg, storedversion, storedEncName, standalone);
  3546. }
  3547. else if (defaultHandler)
  3548. reportDefault(parser, encoding, s, next);
  3549. if (protocolEncodingName == NULL) {
  3550. if (newEncoding) {
  3551. if (newEncoding->minBytesPerChar != encoding->minBytesPerChar) {
  3552. eventPtr = encodingName;
  3553. return XML_ERROR_INCORRECT_ENCODING;
  3554. }
  3555. encoding = newEncoding;
  3556. }
  3557. else if (encodingName) {
  3558. enum XML_Error result;
  3559. if (!storedEncName) {
  3560. storedEncName = poolStoreString(
  3561. &temp2Pool, encoding, encodingName,
  3562. encodingName + XmlNameLength(encoding, encodingName));
  3563. if (!storedEncName)
  3564. return XML_ERROR_NO_MEMORY;
  3565. }
  3566. result = handleUnknownEncoding(parser, storedEncName);
  3567. poolClear(&temp2Pool);
  3568. if (result == XML_ERROR_UNKNOWN_ENCODING)
  3569. eventPtr = encodingName;
  3570. return result;
  3571. }
  3572. }
  3573. if (storedEncName || storedversion)
  3574. poolClear(&temp2Pool);
  3575. return XML_ERROR_NONE;
  3576. }
  3577. static enum XML_Error
  3578. handleUnknownEncoding(XML_Parser parser, const XML_Char *encodingName)
  3579. {
  3580. if (unknownEncodingHandler) {
  3581. XML_Encoding info;
  3582. int i;
  3583. for (i = 0; i < 256; i++)
  3584. info.map[i] = -1;
  3585. info.convert = NULL;
  3586. info.data = NULL;
  3587. info.release = NULL;
  3588. if (unknownEncodingHandler(unknownEncodingHandlerData, encodingName,
  3589. &info)) {
  3590. ENCODING *enc;
  3591. unknownEncodingMem = MALLOC(XmlSizeOfUnknownEncoding());
  3592. if (!unknownEncodingMem) {
  3593. if (info.release)
  3594. info.release(info.data);
  3595. return XML_ERROR_NO_MEMORY;
  3596. }
  3597. enc = (ns
  3598. ? XmlInitUnknownEncodingNS
  3599. : XmlInitUnknownEncoding)(unknownEncodingMem,
  3600. info.map,
  3601. info.convert,
  3602. info.data);
  3603. if (enc) {
  3604. unknownEncodingData = info.data;
  3605. unknownEncodingRelease = info.release;
  3606. encoding = enc;
  3607. return XML_ERROR_NONE;
  3608. }
  3609. }
  3610. if (info.release != NULL)
  3611. info.release(info.data);
  3612. }
  3613. return XML_ERROR_UNKNOWN_ENCODING;
  3614. }
  3615. static enum XML_Error PTRCALL
  3616. prologInitProcessor(XML_Parser parser,
  3617. const char *s,
  3618. const char *end,
  3619. const char **nextPtr)
  3620. {
  3621. enum XML_Error result = initializeEncoding(parser);
  3622. if (result != XML_ERROR_NONE)
  3623. return result;
  3624. processor = prologProcessor;
  3625. return prologProcessor(parser, s, end, nextPtr);
  3626. }
  3627. #ifdef XML_DTD
  3628. static enum XML_Error PTRCALL
  3629. externalParEntInitProcessor(XML_Parser parser,
  3630. const char *s,
  3631. const char *end,
  3632. const char **nextPtr)
  3633. {
  3634. enum XML_Error result = initializeEncoding(parser);
  3635. if (result != XML_ERROR_NONE)
  3636. return result;
  3637. /* we know now that XML_Parse(Buffer) has been called,
  3638. so we consider the external parameter entity read */
  3639. _dtd->paramEntityRead = XML_TRUE;
  3640. if (prologState.inEntityValue) {
  3641. processor = entityValueInitProcessor;
  3642. return entityValueInitProcessor(parser, s, end, nextPtr);
  3643. }
  3644. else {
  3645. processor = externalParEntProcessor;
  3646. return externalParEntProcessor(parser, s, end, nextPtr);
  3647. }
  3648. }
  3649. static enum XML_Error PTRCALL
  3650. entityValueInitProcessor(XML_Parser parser,
  3651. const char *s,
  3652. const char *end,
  3653. const char **nextPtr)
  3654. {
  3655. int tok;
  3656. const char *start = s;
  3657. const char *next = start;
  3658. eventPtr = start;
  3659. for (;;) {
  3660. tok = XmlPrologTok(encoding, start, end, &next);
  3661. eventEndPtr = next;
  3662. if (tok <= 0) {
  3663. if (!ps_finalBuffer && tok != XML_TOK_INVALID) {
  3664. *nextPtr = s;
  3665. return XML_ERROR_NONE;
  3666. }
  3667. switch (tok) {
  3668. case XML_TOK_INVALID:
  3669. return XML_ERROR_INVALID_TOKEN;
  3670. case XML_TOK_PARTIAL:
  3671. return XML_ERROR_UNCLOSED_TOKEN;
  3672. case XML_TOK_PARTIAL_CHAR:
  3673. return XML_ERROR_PARTIAL_CHAR;
  3674. case XML_TOK_NONE: /* start == end */
  3675. default:
  3676. break;
  3677. }
  3678. /* found end of entity value - can store it now */
  3679. return storeEntityValue(parser, encoding, s, end);
  3680. }
  3681. else if (tok == XML_TOK_XML_DECL) {
  3682. enum XML_Error result;
  3683. result = processXmlDecl(parser, 0, start, next);
  3684. if (result != XML_ERROR_NONE)
  3685. return result;
  3686. switch (ps_parsing) {
  3687. case XML_SUSPENDED:
  3688. *nextPtr = next;
  3689. return XML_ERROR_NONE;
  3690. case XML_FINISHED:
  3691. return XML_ERROR_ABORTED;
  3692. default:
  3693. *nextPtr = next;
  3694. }
  3695. /* stop scanning for text declaration - we found one */
  3696. processor = entityValueProcessor;
  3697. return entityValueProcessor(parser, next, end, nextPtr);
  3698. }
  3699. /* If we are at the end of the buffer, this would cause XmlPrologTok to
  3700. return XML_TOK_NONE on the next call, which would then cause the
  3701. function to exit with *nextPtr set to s - that is what we want for other
  3702. tokens, but not for the BOM - we would rather like to skip it;
  3703. then, when this routine is entered the next time, XmlPrologTok will
  3704. return XML_TOK_INVALID, since the BOM is still in the buffer
  3705. */
  3706. else if (tok == XML_TOK_BOM && next == end && !ps_finalBuffer) {
  3707. *nextPtr = next;
  3708. return XML_ERROR_NONE;
  3709. }
  3710. /* If we get this token, we have the start of what might be a
  3711. normal tag, but not a declaration (i.e. it doesn't begin with
  3712. "<!"). In a DTD context, that isn't legal.
  3713. */
  3714. else if (tok == XML_TOK_INSTANCE_START) {
  3715. *nextPtr = next;
  3716. return XML_ERROR_SYNTAX;
  3717. }
  3718. start = next;
  3719. eventPtr = start;
  3720. }
  3721. }
  3722. static enum XML_Error PTRCALL
  3723. externalParEntProcessor(XML_Parser parser,
  3724. const char *s,
  3725. const char *end,
  3726. const char **nextPtr)
  3727. {
  3728. const char *next = s;
  3729. int tok;
  3730. tok = XmlPrologTok(encoding, s, end, &next);
  3731. if (tok <= 0) {
  3732. if (!ps_finalBuffer && tok != XML_TOK_INVALID) {
  3733. *nextPtr = s;
  3734. return XML_ERROR_NONE;
  3735. }
  3736. switch (tok) {
  3737. case XML_TOK_INVALID:
  3738. return XML_ERROR_INVALID_TOKEN;
  3739. case XML_TOK_PARTIAL:
  3740. return XML_ERROR_UNCLOSED_TOKEN;
  3741. case XML_TOK_PARTIAL_CHAR:
  3742. return XML_ERROR_PARTIAL_CHAR;
  3743. case XML_TOK_NONE: /* start == end */
  3744. default:
  3745. break;
  3746. }
  3747. }
  3748. /* This would cause the next stage, i.e. doProlog to be passed XML_TOK_BOM.
  3749. However, when parsing an external subset, doProlog will not accept a BOM
  3750. as valid, and report a syntax error, so we have to skip the BOM
  3751. */
  3752. else if (tok == XML_TOK_BOM) {
  3753. s = next;
  3754. tok = XmlPrologTok(encoding, s, end, &next);
  3755. }
  3756. processor = prologProcessor;
  3757. return doProlog(parser, encoding, s, end, tok, next,
  3758. nextPtr, (XML_Bool)!ps_finalBuffer);
  3759. }
  3760. static enum XML_Error PTRCALL
  3761. entityValueProcessor(XML_Parser parser,
  3762. const char *s,
  3763. const char *end,
  3764. const char **nextPtr)
  3765. {
  3766. const char *start = s;
  3767. const char *next = s;
  3768. const ENCODING *enc = encoding;
  3769. int tok;
  3770. for (;;) {
  3771. tok = XmlPrologTok(enc, start, end, &next);
  3772. if (tok <= 0) {
  3773. if (!ps_finalBuffer && tok != XML_TOK_INVALID) {
  3774. *nextPtr = s;
  3775. return XML_ERROR_NONE;
  3776. }
  3777. switch (tok) {
  3778. case XML_TOK_INVALID:
  3779. return XML_ERROR_INVALID_TOKEN;
  3780. case XML_TOK_PARTIAL:
  3781. return XML_ERROR_UNCLOSED_TOKEN;
  3782. case XML_TOK_PARTIAL_CHAR:
  3783. return XML_ERROR_PARTIAL_CHAR;
  3784. case XML_TOK_NONE: /* start == end */
  3785. default:
  3786. break;
  3787. }
  3788. /* found end of entity value - can store it now */
  3789. return storeEntityValue(parser, enc, s, end);
  3790. }
  3791. start = next;
  3792. }
  3793. }
  3794. #endif /* XML_DTD */
  3795. static enum XML_Error PTRCALL
  3796. prologProcessor(XML_Parser parser,
  3797. const char *s,
  3798. const char *end,
  3799. const char **nextPtr)
  3800. {
  3801. const char *next = s;
  3802. int tok = XmlPrologTok(encoding, s, end, &next);
  3803. return doProlog(parser, encoding, s, end, tok, next,
  3804. nextPtr, (XML_Bool)!ps_finalBuffer);
  3805. }
  3806. static enum XML_Error
  3807. doProlog(XML_Parser parser,
  3808. const ENCODING *enc,
  3809. const char *s,
  3810. const char *end,
  3811. int tok,
  3812. const char *next,
  3813. const char **nextPtr,
  3814. XML_Bool haveMore)
  3815. {
  3816. #ifdef XML_DTD
  3817. static const XML_Char externalSubsetName[] = { ASCII_HASH , '\0' };
  3818. #endif /* XML_DTD */
  3819. static const XML_Char atypeCDATA[] =
  3820. { ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0' };
  3821. static const XML_Char atypeID[] = { ASCII_I, ASCII_D, '\0' };
  3822. static const XML_Char atypeIDREF[] =
  3823. { ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, '\0' };
  3824. static const XML_Char atypeIDREFS[] =
  3825. { ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, ASCII_S, '\0' };
  3826. static const XML_Char atypeENTITY[] =
  3827. { ASCII_E, ASCII_N, ASCII_T, ASCII_I, ASCII_T, ASCII_Y, '\0' };
  3828. static const XML_Char atypeENTITIES[] = { ASCII_E, ASCII_N,
  3829. ASCII_T, ASCII_I, ASCII_T, ASCII_I, ASCII_E, ASCII_S, '\0' };
  3830. static const XML_Char atypeNMTOKEN[] = {
  3831. ASCII_N, ASCII_M, ASCII_T, ASCII_O, ASCII_K, ASCII_E, ASCII_N, '\0' };
  3832. static const XML_Char atypeNMTOKENS[] = { ASCII_N, ASCII_M, ASCII_T,
  3833. ASCII_O, ASCII_K, ASCII_E, ASCII_N, ASCII_S, '\0' };
  3834. static const XML_Char notationPrefix[] = { ASCII_N, ASCII_O, ASCII_T,
  3835. ASCII_A, ASCII_T, ASCII_I, ASCII_O, ASCII_N, ASCII_LPAREN, '\0' };
  3836. static const XML_Char enumValueSep[] = { ASCII_PIPE, '\0' };
  3837. static const XML_Char enumValueStart[] = { ASCII_LPAREN, '\0' };
  3838. /* save one level of indirection */
  3839. DTD * const dtd = _dtd;
  3840. const char **eventPP;
  3841. const char **eventEndPP;
  3842. enum XML_Content_Quant quant;
  3843. if (enc == encoding) {
  3844. eventPP = &eventPtr;
  3845. eventEndPP = &eventEndPtr;
  3846. }
  3847. else {
  3848. eventPP = &(openInternalEntities->internalEventPtr);
  3849. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  3850. }
  3851. for (;;) {
  3852. int role;
  3853. XML_Bool handleDefault = XML_TRUE;
  3854. *eventPP = s;
  3855. *eventEndPP = next;
  3856. if (tok <= 0) {
  3857. if (haveMore && tok != XML_TOK_INVALID) {
  3858. *nextPtr = s;
  3859. return XML_ERROR_NONE;
  3860. }
  3861. switch (tok) {
  3862. case XML_TOK_INVALID:
  3863. *eventPP = next;
  3864. return XML_ERROR_INVALID_TOKEN;
  3865. case XML_TOK_PARTIAL:
  3866. return XML_ERROR_UNCLOSED_TOKEN;
  3867. case XML_TOK_PARTIAL_CHAR:
  3868. return XML_ERROR_PARTIAL_CHAR;
  3869. case -XML_TOK_PROLOG_S:
  3870. tok = -tok;
  3871. break;
  3872. case XML_TOK_NONE:
  3873. #ifdef XML_DTD
  3874. /* for internal PE NOT referenced between declarations */
  3875. if (enc != encoding && !openInternalEntities->betweenDecl) {
  3876. *nextPtr = s;
  3877. return XML_ERROR_NONE;
  3878. }
  3879. /* WFC: PE Between Declarations - must check that PE contains
  3880. complete markup, not only for external PEs, but also for
  3881. internal PEs if the reference occurs between declarations.
  3882. */
  3883. if (isParamEntity || enc != encoding) {
  3884. if (XmlTokenRole(&prologState, XML_TOK_NONE, end, end, enc)
  3885. == XML_ROLE_ERROR)
  3886. return XML_ERROR_INCOMPLETE_PE;
  3887. *nextPtr = s;
  3888. return XML_ERROR_NONE;
  3889. }
  3890. #endif /* XML_DTD */
  3891. return XML_ERROR_NO_ELEMENTS;
  3892. default:
  3893. tok = -tok;
  3894. next = end;
  3895. break;
  3896. }
  3897. }
  3898. role = XmlTokenRole(&prologState, tok, s, next, enc);
  3899. switch (role) {
  3900. case XML_ROLE_XML_DECL:
  3901. {
  3902. enum XML_Error result = processXmlDecl(parser, 0, s, next);
  3903. if (result != XML_ERROR_NONE)
  3904. return result;
  3905. enc = encoding;
  3906. handleDefault = XML_FALSE;
  3907. }
  3908. break;
  3909. case XML_ROLE_DOCTYPE_NAME:
  3910. if (startDoctypeDeclHandler) {
  3911. doctypeName = poolStoreString(&tempPool, enc, s, next);
  3912. if (!doctypeName)
  3913. return XML_ERROR_NO_MEMORY;
  3914. poolFinish(&tempPool);
  3915. doctypePubid = NULL;
  3916. handleDefault = XML_FALSE;
  3917. }
  3918. doctypeSysid = NULL; /* always initialize to NULL */
  3919. break;
  3920. case XML_ROLE_DOCTYPE_INTERNAL_SUBSET:
  3921. if (startDoctypeDeclHandler) {
  3922. startDoctypeDeclHandler(handlerArg, doctypeName, doctypeSysid,
  3923. doctypePubid, 1);
  3924. doctypeName = NULL;
  3925. poolClear(&tempPool);
  3926. handleDefault = XML_FALSE;
  3927. }
  3928. break;
  3929. #ifdef XML_DTD
  3930. case XML_ROLE_TEXT_DECL:
  3931. {
  3932. enum XML_Error result = processXmlDecl(parser, 1, s, next);
  3933. if (result != XML_ERROR_NONE)
  3934. return result;
  3935. enc = encoding;
  3936. handleDefault = XML_FALSE;
  3937. }
  3938. break;
  3939. #endif /* XML_DTD */
  3940. case XML_ROLE_DOCTYPE_PUBLIC_ID:
  3941. #ifdef XML_DTD
  3942. useForeignDTD = XML_FALSE;
  3943. declEntity = (ENTITY *)lookup(parser,
  3944. &dtd->paramEntities,
  3945. externalSubsetName,
  3946. sizeof(ENTITY));
  3947. if (!declEntity)
  3948. return XML_ERROR_NO_MEMORY;
  3949. #endif /* XML_DTD */
  3950. dtd->hasParamEntityRefs = XML_TRUE;
  3951. if (startDoctypeDeclHandler) {
  3952. XML_Char *pubId;
  3953. if (!XmlIsPublicId(enc, s, next, eventPP))
  3954. return XML_ERROR_PUBLICID;
  3955. pubId = poolStoreString(&tempPool, enc,
  3956. s + enc->minBytesPerChar,
  3957. next - enc->minBytesPerChar);
  3958. if (!pubId)
  3959. return XML_ERROR_NO_MEMORY;
  3960. normalizePublicId(pubId);
  3961. poolFinish(&tempPool);
  3962. doctypePubid = pubId;
  3963. handleDefault = XML_FALSE;
  3964. goto alreadyChecked;
  3965. }
  3966. /* fall through */
  3967. case XML_ROLE_ENTITY_PUBLIC_ID:
  3968. if (!XmlIsPublicId(enc, s, next, eventPP))
  3969. return XML_ERROR_PUBLICID;
  3970. alreadyChecked:
  3971. if (dtd->keepProcessing && declEntity) {
  3972. XML_Char *tem = poolStoreString(&dtd->pool,
  3973. enc,
  3974. s + enc->minBytesPerChar,
  3975. next - enc->minBytesPerChar);
  3976. if (!tem)
  3977. return XML_ERROR_NO_MEMORY;
  3978. normalizePublicId(tem);
  3979. declEntity->publicId = tem;
  3980. poolFinish(&dtd->pool);
  3981. if (entityDeclHandler)
  3982. handleDefault = XML_FALSE;
  3983. }
  3984. break;
  3985. case XML_ROLE_DOCTYPE_CLOSE:
  3986. if (doctypeName) {
  3987. startDoctypeDeclHandler(handlerArg, doctypeName,
  3988. doctypeSysid, doctypePubid, 0);
  3989. poolClear(&tempPool);
  3990. handleDefault = XML_FALSE;
  3991. }
  3992. /* doctypeSysid will be non-NULL in the case of a previous
  3993. XML_ROLE_DOCTYPE_SYSTEM_ID, even if startDoctypeDeclHandler
  3994. was not set, indicating an external subset
  3995. */
  3996. #ifdef XML_DTD
  3997. if (doctypeSysid || useForeignDTD) {
  3998. XML_Bool hadParamEntityRefs = dtd->hasParamEntityRefs;
  3999. dtd->hasParamEntityRefs = XML_TRUE;
  4000. if (paramEntityParsing && externalEntityRefHandler) {
  4001. ENTITY *entity = (ENTITY *)lookup(parser,
  4002. &dtd->paramEntities,
  4003. externalSubsetName,
  4004. sizeof(ENTITY));
  4005. if (!entity)
  4006. return XML_ERROR_NO_MEMORY;
  4007. if (useForeignDTD)
  4008. entity->base = curBase;
  4009. dtd->paramEntityRead = XML_FALSE;
  4010. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  4011. 0,
  4012. entity->base,
  4013. entity->systemId,
  4014. entity->publicId))
  4015. return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  4016. if (dtd->paramEntityRead) {
  4017. if (!dtd->standalone &&
  4018. notStandaloneHandler &&
  4019. !notStandaloneHandler(handlerArg))
  4020. return XML_ERROR_NOT_STANDALONE;
  4021. }
  4022. /* if we didn't read the foreign DTD then this means that there
  4023. is no external subset and we must reset dtd->hasParamEntityRefs
  4024. */
  4025. else if (!doctypeSysid)
  4026. dtd->hasParamEntityRefs = hadParamEntityRefs;
  4027. /* end of DTD - no need to update dtd->keepProcessing */
  4028. }
  4029. useForeignDTD = XML_FALSE;
  4030. }
  4031. #endif /* XML_DTD */
  4032. if (endDoctypeDeclHandler) {
  4033. endDoctypeDeclHandler(handlerArg);
  4034. handleDefault = XML_FALSE;
  4035. }
  4036. break;
  4037. case XML_ROLE_INSTANCE_START:
  4038. #ifdef XML_DTD
  4039. /* if there is no DOCTYPE declaration then now is the
  4040. last chance to read the foreign DTD
  4041. */
  4042. if (useForeignDTD) {
  4043. XML_Bool hadParamEntityRefs = dtd->hasParamEntityRefs;
  4044. dtd->hasParamEntityRefs = XML_TRUE;
  4045. if (paramEntityParsing && externalEntityRefHandler) {
  4046. ENTITY *entity = (ENTITY *)lookup(parser, &dtd->paramEntities,
  4047. externalSubsetName,
  4048. sizeof(ENTITY));
  4049. if (!entity)
  4050. return XML_ERROR_NO_MEMORY;
  4051. entity->base = curBase;
  4052. dtd->paramEntityRead = XML_FALSE;
  4053. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  4054. 0,
  4055. entity->base,
  4056. entity->systemId,
  4057. entity->publicId))
  4058. return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  4059. if (dtd->paramEntityRead) {
  4060. if (!dtd->standalone &&
  4061. notStandaloneHandler &&
  4062. !notStandaloneHandler(handlerArg))
  4063. return XML_ERROR_NOT_STANDALONE;
  4064. }
  4065. /* if we didn't read the foreign DTD then this means that there
  4066. is no external subset and we must reset dtd->hasParamEntityRefs
  4067. */
  4068. else
  4069. dtd->hasParamEntityRefs = hadParamEntityRefs;
  4070. /* end of DTD - no need to update dtd->keepProcessing */
  4071. }
  4072. }
  4073. #endif /* XML_DTD */
  4074. processor = contentProcessor;
  4075. return contentProcessor(parser, s, end, nextPtr);
  4076. case XML_ROLE_ATTLIST_ELEMENT_NAME:
  4077. declElementType = getElementType(parser, enc, s, next);
  4078. if (!declElementType)
  4079. return XML_ERROR_NO_MEMORY;
  4080. goto checkAttListDeclHandler;
  4081. case XML_ROLE_ATTRIBUTE_NAME:
  4082. declAttributeId = getAttributeId(parser, enc, s, next);
  4083. if (!declAttributeId)
  4084. return XML_ERROR_NO_MEMORY;
  4085. declAttributeIsCdata = XML_FALSE;
  4086. declAttributeType = NULL;
  4087. declAttributeIsId = XML_FALSE;
  4088. goto checkAttListDeclHandler;
  4089. case XML_ROLE_ATTRIBUTE_TYPE_CDATA:
  4090. declAttributeIsCdata = XML_TRUE;
  4091. declAttributeType = atypeCDATA;
  4092. goto checkAttListDeclHandler;
  4093. case XML_ROLE_ATTRIBUTE_TYPE_ID:
  4094. declAttributeIsId = XML_TRUE;
  4095. declAttributeType = atypeID;
  4096. goto checkAttListDeclHandler;
  4097. case XML_ROLE_ATTRIBUTE_TYPE_IDREF:
  4098. declAttributeType = atypeIDREF;
  4099. goto checkAttListDeclHandler;
  4100. case XML_ROLE_ATTRIBUTE_TYPE_IDREFS:
  4101. declAttributeType = atypeIDREFS;
  4102. goto checkAttListDeclHandler;
  4103. case XML_ROLE_ATTRIBUTE_TYPE_ENTITY:
  4104. declAttributeType = atypeENTITY;
  4105. goto checkAttListDeclHandler;
  4106. case XML_ROLE_ATTRIBUTE_TYPE_ENTITIES:
  4107. declAttributeType = atypeENTITIES;
  4108. goto checkAttListDeclHandler;
  4109. case XML_ROLE_ATTRIBUTE_TYPE_NMTOKEN:
  4110. declAttributeType = atypeNMTOKEN;
  4111. goto checkAttListDeclHandler;
  4112. case XML_ROLE_ATTRIBUTE_TYPE_NMTOKENS:
  4113. declAttributeType = atypeNMTOKENS;
  4114. checkAttListDeclHandler:
  4115. if (dtd->keepProcessing && attlistDeclHandler)
  4116. handleDefault = XML_FALSE;
  4117. break;
  4118. case XML_ROLE_ATTRIBUTE_ENUM_VALUE:
  4119. case XML_ROLE_ATTRIBUTE_NOTATION_VALUE:
  4120. if (dtd->keepProcessing && attlistDeclHandler) {
  4121. const XML_Char *prefix;
  4122. if (declAttributeType) {
  4123. prefix = enumValueSep;
  4124. }
  4125. else {
  4126. prefix = (role == XML_ROLE_ATTRIBUTE_NOTATION_VALUE
  4127. ? notationPrefix
  4128. : enumValueStart);
  4129. }
  4130. if (!poolAppendString(&tempPool, prefix))
  4131. return XML_ERROR_NO_MEMORY;
  4132. if (!poolAppend(&tempPool, enc, s, next))
  4133. return XML_ERROR_NO_MEMORY;
  4134. declAttributeType = tempPool.start;
  4135. handleDefault = XML_FALSE;
  4136. }
  4137. break;
  4138. case XML_ROLE_IMPLIED_ATTRIBUTE_VALUE:
  4139. case XML_ROLE_REQUIRED_ATTRIBUTE_VALUE:
  4140. if (dtd->keepProcessing) {
  4141. if (!defineAttribute(declElementType, declAttributeId,
  4142. declAttributeIsCdata, declAttributeIsId,
  4143. 0, parser))
  4144. return XML_ERROR_NO_MEMORY;
  4145. if (attlistDeclHandler && declAttributeType) {
  4146. if (*declAttributeType == XML_T(ASCII_LPAREN)
  4147. || (*declAttributeType == XML_T(ASCII_N)
  4148. && declAttributeType[1] == XML_T(ASCII_O))) {
  4149. /* Enumerated or Notation type */
  4150. if (!poolAppendChar(&tempPool, XML_T(ASCII_RPAREN))
  4151. || !poolAppendChar(&tempPool, XML_T('\0')))
  4152. return XML_ERROR_NO_MEMORY;
  4153. declAttributeType = tempPool.start;
  4154. poolFinish(&tempPool);
  4155. }
  4156. *eventEndPP = s;
  4157. attlistDeclHandler(handlerArg, declElementType->name,
  4158. declAttributeId->name, declAttributeType,
  4159. 0, role == XML_ROLE_REQUIRED_ATTRIBUTE_VALUE);
  4160. poolClear(&tempPool);
  4161. handleDefault = XML_FALSE;
  4162. }
  4163. }
  4164. break;
  4165. case XML_ROLE_DEFAULT_ATTRIBUTE_VALUE:
  4166. case XML_ROLE_FIXED_ATTRIBUTE_VALUE:
  4167. if (dtd->keepProcessing) {
  4168. const XML_Char *attVal;
  4169. enum XML_Error result =
  4170. storeAttributeValue(parser, enc, declAttributeIsCdata,
  4171. s + enc->minBytesPerChar,
  4172. next - enc->minBytesPerChar,
  4173. &dtd->pool);
  4174. if (result)
  4175. return result;
  4176. attVal = poolStart(&dtd->pool);
  4177. poolFinish(&dtd->pool);
  4178. /* ID attributes aren't allowed to have a default */
  4179. if (!defineAttribute(declElementType, declAttributeId,
  4180. declAttributeIsCdata, XML_FALSE, attVal, parser))
  4181. return XML_ERROR_NO_MEMORY;
  4182. if (attlistDeclHandler && declAttributeType) {
  4183. if (*declAttributeType == XML_T(ASCII_LPAREN)
  4184. || (*declAttributeType == XML_T(ASCII_N)
  4185. && declAttributeType[1] == XML_T(ASCII_O))) {
  4186. /* Enumerated or Notation type */
  4187. if (!poolAppendChar(&tempPool, XML_T(ASCII_RPAREN))
  4188. || !poolAppendChar(&tempPool, XML_T('\0')))
  4189. return XML_ERROR_NO_MEMORY;
  4190. declAttributeType = tempPool.start;
  4191. poolFinish(&tempPool);
  4192. }
  4193. *eventEndPP = s;
  4194. attlistDeclHandler(handlerArg, declElementType->name,
  4195. declAttributeId->name, declAttributeType,
  4196. attVal,
  4197. role == XML_ROLE_FIXED_ATTRIBUTE_VALUE);
  4198. poolClear(&tempPool);
  4199. handleDefault = XML_FALSE;
  4200. }
  4201. }
  4202. break;
  4203. case XML_ROLE_ENTITY_VALUE:
  4204. if (dtd->keepProcessing) {
  4205. enum XML_Error result = storeEntityValue(parser, enc,
  4206. s + enc->minBytesPerChar,
  4207. next - enc->minBytesPerChar);
  4208. if (declEntity) {
  4209. declEntity->textPtr = poolStart(&dtd->entityValuePool);
  4210. declEntity->textLen = (int)(poolLength(&dtd->entityValuePool));
  4211. poolFinish(&dtd->entityValuePool);
  4212. if (entityDeclHandler) {
  4213. *eventEndPP = s;
  4214. entityDeclHandler(handlerArg,
  4215. declEntity->name,
  4216. declEntity->is_param,
  4217. declEntity->textPtr,
  4218. declEntity->textLen,
  4219. curBase, 0, 0, 0);
  4220. handleDefault = XML_FALSE;
  4221. }
  4222. }
  4223. else
  4224. poolDiscard(&dtd->entityValuePool);
  4225. if (result != XML_ERROR_NONE)
  4226. return result;
  4227. }
  4228. break;
  4229. case XML_ROLE_DOCTYPE_SYSTEM_ID:
  4230. #ifdef XML_DTD
  4231. useForeignDTD = XML_FALSE;
  4232. #endif /* XML_DTD */
  4233. dtd->hasParamEntityRefs = XML_TRUE;
  4234. if (startDoctypeDeclHandler) {
  4235. doctypeSysid = poolStoreString(&tempPool, enc,
  4236. s + enc->minBytesPerChar,
  4237. next - enc->minBytesPerChar);
  4238. if (doctypeSysid == NULL)
  4239. return XML_ERROR_NO_MEMORY;
  4240. poolFinish(&tempPool);
  4241. handleDefault = XML_FALSE;
  4242. }
  4243. #ifdef XML_DTD
  4244. else
  4245. /* use externalSubsetName to make doctypeSysid non-NULL
  4246. for the case where no startDoctypeDeclHandler is set */
  4247. doctypeSysid = externalSubsetName;
  4248. #endif /* XML_DTD */
  4249. if (!dtd->standalone
  4250. #ifdef XML_DTD
  4251. && !paramEntityParsing
  4252. #endif /* XML_DTD */
  4253. && notStandaloneHandler
  4254. && !notStandaloneHandler(handlerArg))
  4255. return XML_ERROR_NOT_STANDALONE;
  4256. #ifndef XML_DTD
  4257. break;
  4258. #else /* XML_DTD */
  4259. if (!declEntity) {
  4260. declEntity = (ENTITY *)lookup(parser,
  4261. &dtd->paramEntities,
  4262. externalSubsetName,
  4263. sizeof(ENTITY));
  4264. if (!declEntity)
  4265. return XML_ERROR_NO_MEMORY;
  4266. declEntity->publicId = NULL;
  4267. }
  4268. /* fall through */
  4269. #endif /* XML_DTD */
  4270. case XML_ROLE_ENTITY_SYSTEM_ID:
  4271. if (dtd->keepProcessing && declEntity) {
  4272. declEntity->systemId = poolStoreString(&dtd->pool, enc,
  4273. s + enc->minBytesPerChar,
  4274. next - enc->minBytesPerChar);
  4275. if (!declEntity->systemId)
  4276. return XML_ERROR_NO_MEMORY;
  4277. declEntity->base = curBase;
  4278. poolFinish(&dtd->pool);
  4279. if (entityDeclHandler)
  4280. handleDefault = XML_FALSE;
  4281. }
  4282. break;
  4283. case XML_ROLE_ENTITY_COMPLETE:
  4284. if (dtd->keepProcessing && declEntity && entityDeclHandler) {
  4285. *eventEndPP = s;
  4286. entityDeclHandler(handlerArg,
  4287. declEntity->name,
  4288. declEntity->is_param,
  4289. 0,0,
  4290. declEntity->base,
  4291. declEntity->systemId,
  4292. declEntity->publicId,
  4293. 0);
  4294. handleDefault = XML_FALSE;
  4295. }
  4296. break;
  4297. case XML_ROLE_ENTITY_NOTATION_NAME:
  4298. if (dtd->keepProcessing && declEntity) {
  4299. declEntity->notation = poolStoreString(&dtd->pool, enc, s, next);
  4300. if (!declEntity->notation)
  4301. return XML_ERROR_NO_MEMORY;
  4302. poolFinish(&dtd->pool);
  4303. if (unparsedEntityDeclHandler) {
  4304. *eventEndPP = s;
  4305. unparsedEntityDeclHandler(handlerArg,
  4306. declEntity->name,
  4307. declEntity->base,
  4308. declEntity->systemId,
  4309. declEntity->publicId,
  4310. declEntity->notation);
  4311. handleDefault = XML_FALSE;
  4312. }
  4313. else if (entityDeclHandler) {
  4314. *eventEndPP = s;
  4315. entityDeclHandler(handlerArg,
  4316. declEntity->name,
  4317. 0,0,0,
  4318. declEntity->base,
  4319. declEntity->systemId,
  4320. declEntity->publicId,
  4321. declEntity->notation);
  4322. handleDefault = XML_FALSE;
  4323. }
  4324. }
  4325. break;
  4326. case XML_ROLE_GENERAL_ENTITY_NAME:
  4327. {
  4328. if (XmlPredefinedEntityName(enc, s, next)) {
  4329. declEntity = NULL;
  4330. break;
  4331. }
  4332. if (dtd->keepProcessing) {
  4333. const XML_Char *name = poolStoreString(&dtd->pool, enc, s, next);
  4334. if (!name)
  4335. return XML_ERROR_NO_MEMORY;
  4336. declEntity = (ENTITY *)lookup(parser, &dtd->generalEntities, name,
  4337. sizeof(ENTITY));
  4338. if (!declEntity)
  4339. return XML_ERROR_NO_MEMORY;
  4340. if (declEntity->name != name) {
  4341. poolDiscard(&dtd->pool);
  4342. declEntity = NULL;
  4343. }
  4344. else {
  4345. poolFinish(&dtd->pool);
  4346. declEntity->publicId = NULL;
  4347. declEntity->is_param = XML_FALSE;
  4348. /* if we have a parent parser or are reading an internal parameter
  4349. entity, then the entity declaration is not considered "internal"
  4350. */
  4351. declEntity->is_internal = !(parentParser || openInternalEntities);
  4352. if (entityDeclHandler)
  4353. handleDefault = XML_FALSE;
  4354. }
  4355. }
  4356. else {
  4357. poolDiscard(&dtd->pool);
  4358. declEntity = NULL;
  4359. }
  4360. }
  4361. break;
  4362. case XML_ROLE_PARAM_ENTITY_NAME:
  4363. #ifdef XML_DTD
  4364. if (dtd->keepProcessing) {
  4365. const XML_Char *name = poolStoreString(&dtd->pool, enc, s, next);
  4366. if (!name)
  4367. return XML_ERROR_NO_MEMORY;
  4368. declEntity = (ENTITY *)lookup(parser, &dtd->paramEntities,
  4369. name, sizeof(ENTITY));
  4370. if (!declEntity)
  4371. return XML_ERROR_NO_MEMORY;
  4372. if (declEntity->name != name) {
  4373. poolDiscard(&dtd->pool);
  4374. declEntity = NULL;
  4375. }
  4376. else {
  4377. poolFinish(&dtd->pool);
  4378. declEntity->publicId = NULL;
  4379. declEntity->is_param = XML_TRUE;
  4380. /* if we have a parent parser or are reading an internal parameter
  4381. entity, then the entity declaration is not considered "internal"
  4382. */
  4383. declEntity->is_internal = !(parentParser || openInternalEntities);
  4384. if (entityDeclHandler)
  4385. handleDefault = XML_FALSE;
  4386. }
  4387. }
  4388. else {
  4389. poolDiscard(&dtd->pool);
  4390. declEntity = NULL;
  4391. }
  4392. #else /* not XML_DTD */
  4393. declEntity = NULL;
  4394. #endif /* XML_DTD */
  4395. break;
  4396. case XML_ROLE_NOTATION_NAME:
  4397. declNotationPublicId = NULL;
  4398. declNotationName = NULL;
  4399. if (notationDeclHandler) {
  4400. declNotationName = poolStoreString(&tempPool, enc, s, next);
  4401. if (!declNotationName)
  4402. return XML_ERROR_NO_MEMORY;
  4403. poolFinish(&tempPool);
  4404. handleDefault = XML_FALSE;
  4405. }
  4406. break;
  4407. case XML_ROLE_NOTATION_PUBLIC_ID:
  4408. if (!XmlIsPublicId(enc, s, next, eventPP))
  4409. return XML_ERROR_PUBLICID;
  4410. if (declNotationName) { /* means notationDeclHandler != NULL */
  4411. XML_Char *tem = poolStoreString(&tempPool,
  4412. enc,
  4413. s + enc->minBytesPerChar,
  4414. next - enc->minBytesPerChar);
  4415. if (!tem)
  4416. return XML_ERROR_NO_MEMORY;
  4417. normalizePublicId(tem);
  4418. declNotationPublicId = tem;
  4419. poolFinish(&tempPool);
  4420. handleDefault = XML_FALSE;
  4421. }
  4422. break;
  4423. case XML_ROLE_NOTATION_SYSTEM_ID:
  4424. if (declNotationName && notationDeclHandler) {
  4425. const XML_Char *systemId
  4426. = poolStoreString(&tempPool, enc,
  4427. s + enc->minBytesPerChar,
  4428. next - enc->minBytesPerChar);
  4429. if (!systemId)
  4430. return XML_ERROR_NO_MEMORY;
  4431. *eventEndPP = s;
  4432. notationDeclHandler(handlerArg,
  4433. declNotationName,
  4434. curBase,
  4435. systemId,
  4436. declNotationPublicId);
  4437. handleDefault = XML_FALSE;
  4438. }
  4439. poolClear(&tempPool);
  4440. break;
  4441. case XML_ROLE_NOTATION_NO_SYSTEM_ID:
  4442. if (declNotationPublicId && notationDeclHandler) {
  4443. *eventEndPP = s;
  4444. notationDeclHandler(handlerArg,
  4445. declNotationName,
  4446. curBase,
  4447. 0,
  4448. declNotationPublicId);
  4449. handleDefault = XML_FALSE;
  4450. }
  4451. poolClear(&tempPool);
  4452. break;
  4453. case XML_ROLE_ERROR:
  4454. switch (tok) {
  4455. case XML_TOK_PARAM_ENTITY_REF:
  4456. /* PE references in internal subset are
  4457. not allowed within declarations. */
  4458. return XML_ERROR_PARAM_ENTITY_REF;
  4459. case XML_TOK_XML_DECL:
  4460. return XML_ERROR_MISPLACED_XML_PI;
  4461. default:
  4462. return XML_ERROR_SYNTAX;
  4463. }
  4464. #ifdef XML_DTD
  4465. case XML_ROLE_IGNORE_SECT:
  4466. {
  4467. enum XML_Error result;
  4468. if (defaultHandler)
  4469. reportDefault(parser, enc, s, next);
  4470. handleDefault = XML_FALSE;
  4471. result = doIgnoreSection(parser, enc, &next, end, nextPtr, haveMore);
  4472. if (result != XML_ERROR_NONE)
  4473. return result;
  4474. else if (!next) {
  4475. processor = ignoreSectionProcessor;
  4476. return result;
  4477. }
  4478. }
  4479. break;
  4480. #endif /* XML_DTD */
  4481. case XML_ROLE_GROUP_OPEN:
  4482. if (prologState.level >= groupSize) {
  4483. if (groupSize) {
  4484. char *temp = (char *)REALLOC(groupConnector, groupSize *= 2);
  4485. if (temp == NULL)
  4486. return XML_ERROR_NO_MEMORY;
  4487. groupConnector = temp;
  4488. if (dtd->scaffIndex) {
  4489. int *temp = (int *)REALLOC(dtd->scaffIndex,
  4490. groupSize * sizeof(int));
  4491. if (temp == NULL)
  4492. return XML_ERROR_NO_MEMORY;
  4493. dtd->scaffIndex = temp;
  4494. }
  4495. }
  4496. else {
  4497. groupConnector = (char *)MALLOC(groupSize = 32);
  4498. if (!groupConnector)
  4499. return XML_ERROR_NO_MEMORY;
  4500. }
  4501. }
  4502. groupConnector[prologState.level] = 0;
  4503. if (dtd->in_eldecl) {
  4504. int myindex = nextScaffoldPart(parser);
  4505. if (myindex < 0)
  4506. return XML_ERROR_NO_MEMORY;
  4507. dtd->scaffIndex[dtd->scaffLevel] = myindex;
  4508. dtd->scaffLevel++;
  4509. dtd->scaffold[myindex].type = XML_CTYPE_SEQ;
  4510. if (elementDeclHandler)
  4511. handleDefault = XML_FALSE;
  4512. }
  4513. break;
  4514. case XML_ROLE_GROUP_SEQUENCE:
  4515. if (groupConnector[prologState.level] == ASCII_PIPE)
  4516. return XML_ERROR_SYNTAX;
  4517. groupConnector[prologState.level] = ASCII_COMMA;
  4518. if (dtd->in_eldecl && elementDeclHandler)
  4519. handleDefault = XML_FALSE;
  4520. break;
  4521. case XML_ROLE_GROUP_CHOICE:
  4522. if (groupConnector[prologState.level] == ASCII_COMMA)
  4523. return XML_ERROR_SYNTAX;
  4524. if (dtd->in_eldecl
  4525. && !groupConnector[prologState.level]
  4526. && (dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel - 1]].type
  4527. != XML_CTYPE_MIXED)
  4528. ) {
  4529. dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel - 1]].type
  4530. = XML_CTYPE_CHOICE;
  4531. if (elementDeclHandler)
  4532. handleDefault = XML_FALSE;
  4533. }
  4534. groupConnector[prologState.level] = ASCII_PIPE;
  4535. break;
  4536. case XML_ROLE_PARAM_ENTITY_REF:
  4537. #ifdef XML_DTD
  4538. case XML_ROLE_INNER_PARAM_ENTITY_REF:
  4539. dtd->hasParamEntityRefs = XML_TRUE;
  4540. if (!paramEntityParsing)
  4541. dtd->keepProcessing = dtd->standalone;
  4542. else {
  4543. const XML_Char *name;
  4544. ENTITY *entity;
  4545. name = poolStoreString(&dtd->pool, enc,
  4546. s + enc->minBytesPerChar,
  4547. next - enc->minBytesPerChar);
  4548. if (!name)
  4549. return XML_ERROR_NO_MEMORY;
  4550. entity = (ENTITY *)lookup(parser, &dtd->paramEntities, name, 0);
  4551. poolDiscard(&dtd->pool);
  4552. /* first, determine if a check for an existing declaration is needed;
  4553. if yes, check that the entity exists, and that it is internal,
  4554. otherwise call the skipped entity handler
  4555. */
  4556. if (prologState.documentEntity &&
  4557. (dtd->standalone
  4558. ? !openInternalEntities
  4559. : !dtd->hasParamEntityRefs)) {
  4560. if (!entity)
  4561. return XML_ERROR_UNDEFINED_ENTITY;
  4562. else if (!entity->is_internal)
  4563. return XML_ERROR_ENTITY_DECLARED_IN_PE;
  4564. }
  4565. else if (!entity) {
  4566. dtd->keepProcessing = dtd->standalone;
  4567. /* cannot report skipped entities in declarations */
  4568. if ((role == XML_ROLE_PARAM_ENTITY_REF) && skippedEntityHandler) {
  4569. skippedEntityHandler(handlerArg, name, 1);
  4570. handleDefault = XML_FALSE;
  4571. }
  4572. break;
  4573. }
  4574. if (entity->open)
  4575. return XML_ERROR_RECURSIVE_ENTITY_REF;
  4576. if (entity->textPtr) {
  4577. enum XML_Error result;
  4578. XML_Bool betweenDecl =
  4579. (role == XML_ROLE_PARAM_ENTITY_REF ? XML_TRUE : XML_FALSE);
  4580. result = processInternalEntity(parser, entity, betweenDecl);
  4581. if (result != XML_ERROR_NONE)
  4582. return result;
  4583. handleDefault = XML_FALSE;
  4584. break;
  4585. }
  4586. if (externalEntityRefHandler) {
  4587. dtd->paramEntityRead = XML_FALSE;
  4588. entity->open = XML_TRUE;
  4589. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  4590. 0,
  4591. entity->base,
  4592. entity->systemId,
  4593. entity->publicId)) {
  4594. entity->open = XML_FALSE;
  4595. return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  4596. }
  4597. entity->open = XML_FALSE;
  4598. handleDefault = XML_FALSE;
  4599. if (!dtd->paramEntityRead) {
  4600. dtd->keepProcessing = dtd->standalone;
  4601. break;
  4602. }
  4603. }
  4604. else {
  4605. dtd->keepProcessing = dtd->standalone;
  4606. break;
  4607. }
  4608. }
  4609. #endif /* XML_DTD */
  4610. if (!dtd->standalone &&
  4611. notStandaloneHandler &&
  4612. !notStandaloneHandler(handlerArg))
  4613. return XML_ERROR_NOT_STANDALONE;
  4614. break;
  4615. /* Element declaration stuff */
  4616. case XML_ROLE_ELEMENT_NAME:
  4617. if (elementDeclHandler) {
  4618. declElementType = getElementType(parser, enc, s, next);
  4619. if (!declElementType)
  4620. return XML_ERROR_NO_MEMORY;
  4621. dtd->scaffLevel = 0;
  4622. dtd->scaffCount = 0;
  4623. dtd->in_eldecl = XML_TRUE;
  4624. handleDefault = XML_FALSE;
  4625. }
  4626. break;
  4627. case XML_ROLE_CONTENT_ANY:
  4628. case XML_ROLE_CONTENT_EMPTY:
  4629. if (dtd->in_eldecl) {
  4630. if (elementDeclHandler) {
  4631. XML_Content * content = (XML_Content *) MALLOC(sizeof(XML_Content));
  4632. if (!content)
  4633. return XML_ERROR_NO_MEMORY;
  4634. content->quant = XML_CQUANT_NONE;
  4635. content->name = NULL;
  4636. content->numchildren = 0;
  4637. content->children = NULL;
  4638. content->type = ((role == XML_ROLE_CONTENT_ANY) ?
  4639. XML_CTYPE_ANY :
  4640. XML_CTYPE_EMPTY);
  4641. *eventEndPP = s;
  4642. elementDeclHandler(handlerArg, declElementType->name, content);
  4643. handleDefault = XML_FALSE;
  4644. }
  4645. dtd->in_eldecl = XML_FALSE;
  4646. }
  4647. break;
  4648. case XML_ROLE_CONTENT_PCDATA:
  4649. if (dtd->in_eldecl) {
  4650. dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel - 1]].type
  4651. = XML_CTYPE_MIXED;
  4652. if (elementDeclHandler)
  4653. handleDefault = XML_FALSE;
  4654. }
  4655. break;
  4656. case XML_ROLE_CONTENT_ELEMENT:
  4657. quant = XML_CQUANT_NONE;
  4658. goto elementContent;
  4659. case XML_ROLE_CONTENT_ELEMENT_OPT:
  4660. quant = XML_CQUANT_OPT;
  4661. goto elementContent;
  4662. case XML_ROLE_CONTENT_ELEMENT_REP:
  4663. quant = XML_CQUANT_REP;
  4664. goto elementContent;
  4665. case XML_ROLE_CONTENT_ELEMENT_PLUS:
  4666. quant = XML_CQUANT_PLUS;
  4667. elementContent:
  4668. if (dtd->in_eldecl) {
  4669. ELEMENT_TYPE *el;
  4670. const XML_Char *name;
  4671. int nameLen;
  4672. const char *nxt = (quant == XML_CQUANT_NONE
  4673. ? next
  4674. : next - enc->minBytesPerChar);
  4675. int myindex = nextScaffoldPart(parser);
  4676. if (myindex < 0)
  4677. return XML_ERROR_NO_MEMORY;
  4678. dtd->scaffold[myindex].type = XML_CTYPE_NAME;
  4679. dtd->scaffold[myindex].quant = quant;
  4680. el = getElementType(parser, enc, s, nxt);
  4681. if (!el)
  4682. return XML_ERROR_NO_MEMORY;
  4683. name = el->name;
  4684. dtd->scaffold[myindex].name = name;
  4685. nameLen = 0;
  4686. for (; name[nameLen++]; );
  4687. dtd->contentStringLen += nameLen;
  4688. if (elementDeclHandler)
  4689. handleDefault = XML_FALSE;
  4690. }
  4691. break;
  4692. case XML_ROLE_GROUP_CLOSE:
  4693. quant = XML_CQUANT_NONE;
  4694. goto closeGroup;
  4695. case XML_ROLE_GROUP_CLOSE_OPT:
  4696. quant = XML_CQUANT_OPT;
  4697. goto closeGroup;
  4698. case XML_ROLE_GROUP_CLOSE_REP:
  4699. quant = XML_CQUANT_REP;
  4700. goto closeGroup;
  4701. case XML_ROLE_GROUP_CLOSE_PLUS:
  4702. quant = XML_CQUANT_PLUS;
  4703. closeGroup:
  4704. if (dtd->in_eldecl) {
  4705. if (elementDeclHandler)
  4706. handleDefault = XML_FALSE;
  4707. dtd->scaffLevel--;
  4708. dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel]].quant = quant;
  4709. if (dtd->scaffLevel == 0) {
  4710. if (!handleDefault) {
  4711. XML_Content *model = build_model(parser);
  4712. if (!model)
  4713. return XML_ERROR_NO_MEMORY;
  4714. *eventEndPP = s;
  4715. elementDeclHandler(handlerArg, declElementType->name, model);
  4716. }
  4717. dtd->in_eldecl = XML_FALSE;
  4718. dtd->contentStringLen = 0;
  4719. }
  4720. }
  4721. break;
  4722. /* End element declaration stuff */
  4723. case XML_ROLE_PI:
  4724. if (!reportProcessingInstruction(parser, enc, s, next))
  4725. return XML_ERROR_NO_MEMORY;
  4726. handleDefault = XML_FALSE;
  4727. break;
  4728. case XML_ROLE_COMMENT:
  4729. if (!reportComment(parser, enc, s, next))
  4730. return XML_ERROR_NO_MEMORY;
  4731. handleDefault = XML_FALSE;
  4732. break;
  4733. case XML_ROLE_NONE:
  4734. switch (tok) {
  4735. case XML_TOK_BOM:
  4736. handleDefault = XML_FALSE;
  4737. break;
  4738. }
  4739. break;
  4740. case XML_ROLE_DOCTYPE_NONE:
  4741. if (startDoctypeDeclHandler)
  4742. handleDefault = XML_FALSE;
  4743. break;
  4744. case XML_ROLE_ENTITY_NONE:
  4745. if (dtd->keepProcessing && entityDeclHandler)
  4746. handleDefault = XML_FALSE;
  4747. break;
  4748. case XML_ROLE_NOTATION_NONE:
  4749. if (notationDeclHandler)
  4750. handleDefault = XML_FALSE;
  4751. break;
  4752. case XML_ROLE_ATTLIST_NONE:
  4753. if (dtd->keepProcessing && attlistDeclHandler)
  4754. handleDefault = XML_FALSE;
  4755. break;
  4756. case XML_ROLE_ELEMENT_NONE:
  4757. if (elementDeclHandler)
  4758. handleDefault = XML_FALSE;
  4759. break;
  4760. } /* end of big switch */
  4761. if (handleDefault && defaultHandler)
  4762. reportDefault(parser, enc, s, next);
  4763. switch (ps_parsing) {
  4764. case XML_SUSPENDED:
  4765. *nextPtr = next;
  4766. return XML_ERROR_NONE;
  4767. case XML_FINISHED:
  4768. return XML_ERROR_ABORTED;
  4769. default:
  4770. s = next;
  4771. tok = XmlPrologTok(enc, s, end, &next);
  4772. }
  4773. }
  4774. /* not reached */
  4775. }
  4776. static enum XML_Error PTRCALL
  4777. epilogProcessor(XML_Parser parser,
  4778. const char *s,
  4779. const char *end,
  4780. const char **nextPtr)
  4781. {
  4782. processor = epilogProcessor;
  4783. eventPtr = s;
  4784. for (;;) {
  4785. const char *next = NULL;
  4786. int tok = XmlPrologTok(encoding, s, end, &next);
  4787. eventEndPtr = next;
  4788. switch (tok) {
  4789. /* report partial linebreak - it might be the last token */
  4790. case -XML_TOK_PROLOG_S:
  4791. if (defaultHandler) {
  4792. reportDefault(parser, encoding, s, next);
  4793. if (ps_parsing == XML_FINISHED)
  4794. return XML_ERROR_ABORTED;
  4795. }
  4796. *nextPtr = next;
  4797. return XML_ERROR_NONE;
  4798. case XML_TOK_NONE:
  4799. *nextPtr = s;
  4800. return XML_ERROR_NONE;
  4801. case XML_TOK_PROLOG_S:
  4802. if (defaultHandler)
  4803. reportDefault(parser, encoding, s, next);
  4804. break;
  4805. case XML_TOK_PI:
  4806. if (!reportProcessingInstruction(parser, encoding, s, next))
  4807. return XML_ERROR_NO_MEMORY;
  4808. break;
  4809. case XML_TOK_COMMENT:
  4810. if (!reportComment(parser, encoding, s, next))
  4811. return XML_ERROR_NO_MEMORY;
  4812. break;
  4813. case XML_TOK_INVALID:
  4814. eventPtr = next;
  4815. return XML_ERROR_INVALID_TOKEN;
  4816. case XML_TOK_PARTIAL:
  4817. if (!ps_finalBuffer) {
  4818. *nextPtr = s;
  4819. return XML_ERROR_NONE;
  4820. }
  4821. return XML_ERROR_UNCLOSED_TOKEN;
  4822. case XML_TOK_PARTIAL_CHAR:
  4823. if (!ps_finalBuffer) {
  4824. *nextPtr = s;
  4825. return XML_ERROR_NONE;
  4826. }
  4827. return XML_ERROR_PARTIAL_CHAR;
  4828. default:
  4829. return XML_ERROR_JUNK_AFTER_DOC_ELEMENT;
  4830. }
  4831. eventPtr = s = next;
  4832. switch (ps_parsing) {
  4833. case XML_SUSPENDED:
  4834. *nextPtr = next;
  4835. return XML_ERROR_NONE;
  4836. case XML_FINISHED:
  4837. return XML_ERROR_ABORTED;
  4838. default: ;
  4839. }
  4840. }
  4841. }
  4842. static enum XML_Error
  4843. processInternalEntity(XML_Parser parser, ENTITY *entity,
  4844. XML_Bool betweenDecl)
  4845. {
  4846. const char *textStart, *textEnd;
  4847. const char *next;
  4848. enum XML_Error result;
  4849. OPEN_INTERNAL_ENTITY *openEntity;
  4850. if (freeInternalEntities) {
  4851. openEntity = freeInternalEntities;
  4852. freeInternalEntities = openEntity->next;
  4853. }
  4854. else {
  4855. openEntity = (OPEN_INTERNAL_ENTITY *)MALLOC(sizeof(OPEN_INTERNAL_ENTITY));
  4856. if (!openEntity)
  4857. return XML_ERROR_NO_MEMORY;
  4858. }
  4859. entity->open = XML_TRUE;
  4860. entity->processed = 0;
  4861. openEntity->next = openInternalEntities;
  4862. openInternalEntities = openEntity;
  4863. openEntity->entity = entity;
  4864. openEntity->startTagLevel = tagLevel;
  4865. openEntity->betweenDecl = betweenDecl;
  4866. openEntity->internalEventPtr = NULL;
  4867. openEntity->internalEventEndPtr = NULL;
  4868. textStart = (char *)entity->textPtr;
  4869. textEnd = (char *)(entity->textPtr + entity->textLen);
  4870. /* Set a safe default value in case 'next' does not get set */
  4871. next = textStart;
  4872. #ifdef XML_DTD
  4873. if (entity->is_param) {
  4874. int tok = XmlPrologTok(internalEncoding, textStart, textEnd, &next);
  4875. result = doProlog(parser, internalEncoding, textStart, textEnd, tok,
  4876. next, &next, XML_FALSE);
  4877. }
  4878. else
  4879. #endif /* XML_DTD */
  4880. result = doContent(parser, tagLevel, internalEncoding, textStart,
  4881. textEnd, &next, XML_FALSE);
  4882. if (result == XML_ERROR_NONE) {
  4883. if (textEnd != next && ps_parsing == XML_SUSPENDED) {
  4884. entity->processed = (int)(next - textStart);
  4885. processor = internalEntityProcessor;
  4886. }
  4887. else {
  4888. entity->open = XML_FALSE;
  4889. openInternalEntities = openEntity->next;
  4890. /* put openEntity back in list of free instances */
  4891. openEntity->next = freeInternalEntities;
  4892. freeInternalEntities = openEntity;
  4893. }
  4894. }
  4895. return result;
  4896. }
  4897. static enum XML_Error PTRCALL
  4898. internalEntityProcessor(XML_Parser parser,
  4899. const char *s,
  4900. const char *end,
  4901. const char **nextPtr)
  4902. {
  4903. ENTITY *entity;
  4904. const char *textStart, *textEnd;
  4905. const char *next;
  4906. enum XML_Error result;
  4907. OPEN_INTERNAL_ENTITY *openEntity = openInternalEntities;
  4908. if (!openEntity)
  4909. return XML_ERROR_UNEXPECTED_STATE;
  4910. entity = openEntity->entity;
  4911. textStart = ((char *)entity->textPtr) + entity->processed;
  4912. textEnd = (char *)(entity->textPtr + entity->textLen);
  4913. /* Set a safe default value in case 'next' does not get set */
  4914. next = textStart;
  4915. #ifdef XML_DTD
  4916. if (entity->is_param) {
  4917. int tok = XmlPrologTok(internalEncoding, textStart, textEnd, &next);
  4918. result = doProlog(parser, internalEncoding, textStart, textEnd, tok,
  4919. next, &next, XML_FALSE);
  4920. }
  4921. else
  4922. #endif /* XML_DTD */
  4923. result = doContent(parser, openEntity->startTagLevel, internalEncoding,
  4924. textStart, textEnd, &next, XML_FALSE);
  4925. if (result != XML_ERROR_NONE)
  4926. return result;
  4927. else if (textEnd != next && ps_parsing == XML_SUSPENDED) {
  4928. entity->processed = (int)(next - (char *)entity->textPtr);
  4929. return result;
  4930. }
  4931. else {
  4932. entity->open = XML_FALSE;
  4933. openInternalEntities = openEntity->next;
  4934. /* put openEntity back in list of free instances */
  4935. openEntity->next = freeInternalEntities;
  4936. freeInternalEntities = openEntity;
  4937. }
  4938. #ifdef XML_DTD
  4939. if (entity->is_param) {
  4940. int tok;
  4941. processor = prologProcessor;
  4942. tok = XmlPrologTok(encoding, s, end, &next);
  4943. return doProlog(parser, encoding, s, end, tok, next, nextPtr,
  4944. (XML_Bool)!ps_finalBuffer);
  4945. }
  4946. else
  4947. #endif /* XML_DTD */
  4948. {
  4949. processor = contentProcessor;
  4950. /* see externalEntityContentProcessor vs contentProcessor */
  4951. return doContent(parser, parentParser ? 1 : 0, encoding, s, end,
  4952. nextPtr, (XML_Bool)!ps_finalBuffer);
  4953. }
  4954. }
  4955. static enum XML_Error PTRCALL
  4956. errorProcessor(XML_Parser parser,
  4957. const char *UNUSED_P(s),
  4958. const char *UNUSED_P(end),
  4959. const char **UNUSED_P(nextPtr))
  4960. {
  4961. return errorCode;
  4962. }
  4963. static enum XML_Error
  4964. storeAttributeValue(XML_Parser parser, const ENCODING *enc, XML_Bool isCdata,
  4965. const char *ptr, const char *end,
  4966. STRING_POOL *pool)
  4967. {
  4968. enum XML_Error result = appendAttributeValue(parser, enc, isCdata, ptr,
  4969. end, pool);
  4970. if (result)
  4971. return result;
  4972. if (!isCdata && poolLength(pool) && poolLastChar(pool) == 0x20)
  4973. poolChop(pool);
  4974. if (!poolAppendChar(pool, XML_T('\0')))
  4975. return XML_ERROR_NO_MEMORY;
  4976. return XML_ERROR_NONE;
  4977. }
  4978. static enum XML_Error
  4979. appendAttributeValue(XML_Parser parser, const ENCODING *enc, XML_Bool isCdata,
  4980. const char *ptr, const char *end,
  4981. STRING_POOL *pool)
  4982. {
  4983. DTD * const dtd = _dtd; /* save one level of indirection */
  4984. for (;;) {
  4985. const char *next;
  4986. int tok = XmlAttributeValueTok(enc, ptr, end, &next);
  4987. switch (tok) {
  4988. case XML_TOK_NONE:
  4989. return XML_ERROR_NONE;
  4990. case XML_TOK_INVALID:
  4991. if (enc == encoding)
  4992. eventPtr = next;
  4993. return XML_ERROR_INVALID_TOKEN;
  4994. case XML_TOK_PARTIAL:
  4995. if (enc == encoding)
  4996. eventPtr = ptr;
  4997. return XML_ERROR_INVALID_TOKEN;
  4998. case XML_TOK_CHAR_REF:
  4999. {
  5000. XML_Char buf[XML_ENCODE_MAX];
  5001. int i;
  5002. int n = XmlCharRefNumber(enc, ptr);
  5003. if (n < 0) {
  5004. if (enc == encoding)
  5005. eventPtr = ptr;
  5006. return XML_ERROR_BAD_CHAR_REF;
  5007. }
  5008. if (!isCdata
  5009. && n == 0x20 /* space */
  5010. && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
  5011. break;
  5012. n = XmlEncode(n, (ICHAR *)buf);
  5013. if (!n) {
  5014. if (enc == encoding)
  5015. eventPtr = ptr;
  5016. return XML_ERROR_BAD_CHAR_REF;
  5017. }
  5018. for (i = 0; i < n; i++) {
  5019. if (!poolAppendChar(pool, buf[i]))
  5020. return XML_ERROR_NO_MEMORY;
  5021. }
  5022. }
  5023. break;
  5024. case XML_TOK_DATA_CHARS:
  5025. if (!poolAppend(pool, enc, ptr, next))
  5026. return XML_ERROR_NO_MEMORY;
  5027. break;
  5028. case XML_TOK_TRAILING_CR:
  5029. next = ptr + enc->minBytesPerChar;
  5030. /* fall through */
  5031. case XML_TOK_ATTRIBUTE_VALUE_S:
  5032. case XML_TOK_DATA_NEWLINE:
  5033. if (!isCdata && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
  5034. break;
  5035. if (!poolAppendChar(pool, 0x20))
  5036. return XML_ERROR_NO_MEMORY;
  5037. break;
  5038. case XML_TOK_ENTITY_REF:
  5039. {
  5040. const XML_Char *name;
  5041. ENTITY *entity;
  5042. char checkEntityDecl;
  5043. XML_Char ch = (XML_Char) XmlPredefinedEntityName(enc,
  5044. ptr + enc->minBytesPerChar,
  5045. next - enc->minBytesPerChar);
  5046. if (ch) {
  5047. if (!poolAppendChar(pool, ch))
  5048. return XML_ERROR_NO_MEMORY;
  5049. break;
  5050. }
  5051. name = poolStoreString(&temp2Pool, enc,
  5052. ptr + enc->minBytesPerChar,
  5053. next - enc->minBytesPerChar);
  5054. if (!name)
  5055. return XML_ERROR_NO_MEMORY;
  5056. entity = (ENTITY *)lookup(parser, &dtd->generalEntities, name, 0);
  5057. poolDiscard(&temp2Pool);
  5058. /* First, determine if a check for an existing declaration is needed;
  5059. if yes, check that the entity exists, and that it is internal.
  5060. */
  5061. if (pool == &dtd->pool) /* are we called from prolog? */
  5062. checkEntityDecl =
  5063. #ifdef XML_DTD
  5064. prologState.documentEntity &&
  5065. #endif /* XML_DTD */
  5066. (dtd->standalone
  5067. ? !openInternalEntities
  5068. : !dtd->hasParamEntityRefs);
  5069. else /* if (pool == &tempPool): we are called from content */
  5070. checkEntityDecl = !dtd->hasParamEntityRefs || dtd->standalone;
  5071. if (checkEntityDecl) {
  5072. if (!entity)
  5073. return XML_ERROR_UNDEFINED_ENTITY;
  5074. else if (!entity->is_internal)
  5075. return XML_ERROR_ENTITY_DECLARED_IN_PE;
  5076. }
  5077. else if (!entity) {
  5078. /* Cannot report skipped entity here - see comments on
  5079. skippedEntityHandler.
  5080. if (skippedEntityHandler)
  5081. skippedEntityHandler(handlerArg, name, 0);
  5082. */
  5083. /* Cannot call the default handler because this would be
  5084. out of sync with the call to the startElementHandler.
  5085. if ((pool == &tempPool) && defaultHandler)
  5086. reportDefault(parser, enc, ptr, next);
  5087. */
  5088. break;
  5089. }
  5090. if (entity->open) {
  5091. if (enc == encoding)
  5092. eventPtr = ptr;
  5093. return XML_ERROR_RECURSIVE_ENTITY_REF;
  5094. }
  5095. if (entity->notation) {
  5096. if (enc == encoding)
  5097. eventPtr = ptr;
  5098. return XML_ERROR_BINARY_ENTITY_REF;
  5099. }
  5100. if (!entity->textPtr) {
  5101. if (enc == encoding)
  5102. eventPtr = ptr;
  5103. return XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF;
  5104. }
  5105. else {
  5106. enum XML_Error result;
  5107. const XML_Char *textEnd = entity->textPtr + entity->textLen;
  5108. entity->open = XML_TRUE;
  5109. result = appendAttributeValue(parser, internalEncoding, isCdata,
  5110. (char *)entity->textPtr,
  5111. (char *)textEnd, pool);
  5112. entity->open = XML_FALSE;
  5113. if (result)
  5114. return result;
  5115. }
  5116. }
  5117. break;
  5118. default:
  5119. if (enc == encoding)
  5120. eventPtr = ptr;
  5121. return XML_ERROR_UNEXPECTED_STATE;
  5122. }
  5123. ptr = next;
  5124. }
  5125. /* not reached */
  5126. }
  5127. static enum XML_Error
  5128. storeEntityValue(XML_Parser parser,
  5129. const ENCODING *enc,
  5130. const char *entityTextPtr,
  5131. const char *entityTextEnd)
  5132. {
  5133. DTD * const dtd = _dtd; /* save one level of indirection */
  5134. STRING_POOL *pool = &(dtd->entityValuePool);
  5135. enum XML_Error result = XML_ERROR_NONE;
  5136. #ifdef XML_DTD
  5137. int oldInEntityValue = prologState.inEntityValue;
  5138. prologState.inEntityValue = 1;
  5139. #endif /* XML_DTD */
  5140. /* never return Null for the value argument in EntityDeclHandler,
  5141. since this would indicate an external entity; therefore we
  5142. have to make sure that entityValuePool.start is not null */
  5143. if (!pool->blocks) {
  5144. if (!poolGrow(pool))
  5145. return XML_ERROR_NO_MEMORY;
  5146. }
  5147. for (;;) {
  5148. const char *next;
  5149. int tok = XmlEntityValueTok(enc, entityTextPtr, entityTextEnd, &next);
  5150. switch (tok) {
  5151. case XML_TOK_PARAM_ENTITY_REF:
  5152. #ifdef XML_DTD
  5153. if (isParamEntity || enc != encoding) {
  5154. const XML_Char *name;
  5155. ENTITY *entity;
  5156. name = poolStoreString(&tempPool, enc,
  5157. entityTextPtr + enc->minBytesPerChar,
  5158. next - enc->minBytesPerChar);
  5159. if (!name) {
  5160. result = XML_ERROR_NO_MEMORY;
  5161. goto endEntityValue;
  5162. }
  5163. entity = (ENTITY *)lookup(parser, &dtd->paramEntities, name, 0);
  5164. poolDiscard(&tempPool);
  5165. if (!entity) {
  5166. /* not a well-formedness error - see XML 1.0: WFC Entity Declared */
  5167. /* cannot report skipped entity here - see comments on
  5168. skippedEntityHandler
  5169. if (skippedEntityHandler)
  5170. skippedEntityHandler(handlerArg, name, 0);
  5171. */
  5172. dtd->keepProcessing = dtd->standalone;
  5173. goto endEntityValue;
  5174. }
  5175. if (entity->open) {
  5176. if (enc == encoding)
  5177. eventPtr = entityTextPtr;
  5178. result = XML_ERROR_RECURSIVE_ENTITY_REF;
  5179. goto endEntityValue;
  5180. }
  5181. if (entity->systemId) {
  5182. if (externalEntityRefHandler) {
  5183. dtd->paramEntityRead = XML_FALSE;
  5184. entity->open = XML_TRUE;
  5185. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  5186. 0,
  5187. entity->base,
  5188. entity->systemId,
  5189. entity->publicId)) {
  5190. entity->open = XML_FALSE;
  5191. result = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  5192. goto endEntityValue;
  5193. }
  5194. entity->open = XML_FALSE;
  5195. if (!dtd->paramEntityRead)
  5196. dtd->keepProcessing = dtd->standalone;
  5197. }
  5198. else
  5199. dtd->keepProcessing = dtd->standalone;
  5200. }
  5201. else {
  5202. entity->open = XML_TRUE;
  5203. result = storeEntityValue(parser,
  5204. internalEncoding,
  5205. (char *)entity->textPtr,
  5206. (char *)(entity->textPtr
  5207. + entity->textLen));
  5208. entity->open = XML_FALSE;
  5209. if (result)
  5210. goto endEntityValue;
  5211. }
  5212. break;
  5213. }
  5214. #endif /* XML_DTD */
  5215. /* In the internal subset, PE references are not legal
  5216. within markup declarations, e.g entity values in this case. */
  5217. eventPtr = entityTextPtr;
  5218. result = XML_ERROR_PARAM_ENTITY_REF;
  5219. goto endEntityValue;
  5220. case XML_TOK_NONE:
  5221. result = XML_ERROR_NONE;
  5222. goto endEntityValue;
  5223. case XML_TOK_ENTITY_REF:
  5224. case XML_TOK_DATA_CHARS:
  5225. if (!poolAppend(pool, enc, entityTextPtr, next)) {
  5226. result = XML_ERROR_NO_MEMORY;
  5227. goto endEntityValue;
  5228. }
  5229. break;
  5230. case XML_TOK_TRAILING_CR:
  5231. next = entityTextPtr + enc->minBytesPerChar;
  5232. /* fall through */
  5233. case XML_TOK_DATA_NEWLINE:
  5234. if (pool->end == pool->ptr && !poolGrow(pool)) {
  5235. result = XML_ERROR_NO_MEMORY;
  5236. goto endEntityValue;
  5237. }
  5238. *(pool->ptr)++ = 0xA;
  5239. break;
  5240. case XML_TOK_CHAR_REF:
  5241. {
  5242. XML_Char buf[XML_ENCODE_MAX];
  5243. int i;
  5244. int n = XmlCharRefNumber(enc, entityTextPtr);
  5245. if (n < 0) {
  5246. if (enc == encoding)
  5247. eventPtr = entityTextPtr;
  5248. result = XML_ERROR_BAD_CHAR_REF;
  5249. goto endEntityValue;
  5250. }
  5251. n = XmlEncode(n, (ICHAR *)buf);
  5252. if (!n) {
  5253. if (enc == encoding)
  5254. eventPtr = entityTextPtr;
  5255. result = XML_ERROR_BAD_CHAR_REF;
  5256. goto endEntityValue;
  5257. }
  5258. for (i = 0; i < n; i++) {
  5259. if (pool->end == pool->ptr && !poolGrow(pool)) {
  5260. result = XML_ERROR_NO_MEMORY;
  5261. goto endEntityValue;
  5262. }
  5263. *(pool->ptr)++ = buf[i];
  5264. }
  5265. }
  5266. break;
  5267. case XML_TOK_PARTIAL:
  5268. if (enc == encoding)
  5269. eventPtr = entityTextPtr;
  5270. result = XML_ERROR_INVALID_TOKEN;
  5271. goto endEntityValue;
  5272. case XML_TOK_INVALID:
  5273. if (enc == encoding)
  5274. eventPtr = next;
  5275. result = XML_ERROR_INVALID_TOKEN;
  5276. goto endEntityValue;
  5277. default:
  5278. if (enc == encoding)
  5279. eventPtr = entityTextPtr;
  5280. result = XML_ERROR_UNEXPECTED_STATE;
  5281. goto endEntityValue;
  5282. }
  5283. entityTextPtr = next;
  5284. }
  5285. endEntityValue:
  5286. #ifdef XML_DTD
  5287. prologState.inEntityValue = oldInEntityValue;
  5288. #endif /* XML_DTD */
  5289. return result;
  5290. }
  5291. static void FASTCALL
  5292. normalizeLines(XML_Char *s)
  5293. {
  5294. XML_Char *p;
  5295. for (;; s++) {
  5296. if (*s == XML_T('\0'))
  5297. return;
  5298. if (*s == 0xD)
  5299. break;
  5300. }
  5301. p = s;
  5302. do {
  5303. if (*s == 0xD) {
  5304. *p++ = 0xA;
  5305. if (*++s == 0xA)
  5306. s++;
  5307. }
  5308. else
  5309. *p++ = *s++;
  5310. } while (*s);
  5311. *p = XML_T('\0');
  5312. }
  5313. static int
  5314. reportProcessingInstruction(XML_Parser parser, const ENCODING *enc,
  5315. const char *start, const char *end)
  5316. {
  5317. const XML_Char *target;
  5318. XML_Char *data;
  5319. const char *tem;
  5320. if (!processingInstructionHandler) {
  5321. if (defaultHandler)
  5322. reportDefault(parser, enc, start, end);
  5323. return 1;
  5324. }
  5325. start += enc->minBytesPerChar * 2;
  5326. tem = start + XmlNameLength(enc, start);
  5327. target = poolStoreString(&tempPool, enc, start, tem);
  5328. if (!target)
  5329. return 0;
  5330. poolFinish(&tempPool);
  5331. data = poolStoreString(&tempPool, enc,
  5332. XmlSkipS(enc, tem),
  5333. end - enc->minBytesPerChar*2);
  5334. if (!data)
  5335. return 0;
  5336. normalizeLines(data);
  5337. processingInstructionHandler(handlerArg, target, data);
  5338. poolClear(&tempPool);
  5339. return 1;
  5340. }
  5341. static int
  5342. reportComment(XML_Parser parser, const ENCODING *enc,
  5343. const char *start, const char *end)
  5344. {
  5345. XML_Char *data;
  5346. if (!commentHandler) {
  5347. if (defaultHandler)
  5348. reportDefault(parser, enc, start, end);
  5349. return 1;
  5350. }
  5351. data = poolStoreString(&tempPool,
  5352. enc,
  5353. start + enc->minBytesPerChar * 4,
  5354. end - enc->minBytesPerChar * 3);
  5355. if (!data)
  5356. return 0;
  5357. normalizeLines(data);
  5358. commentHandler(handlerArg, data);
  5359. poolClear(&tempPool);
  5360. return 1;
  5361. }
  5362. static void
  5363. reportDefault(XML_Parser parser, const ENCODING *enc,
  5364. const char *s, const char *end)
  5365. {
  5366. if (MUST_CONVERT(enc, s)) {
  5367. enum XML_Convert_Result convert_res;
  5368. const char **eventPP;
  5369. const char **eventEndPP;
  5370. if (enc == encoding) {
  5371. eventPP = &eventPtr;
  5372. eventEndPP = &eventEndPtr;
  5373. }
  5374. else {
  5375. eventPP = &(openInternalEntities->internalEventPtr);
  5376. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  5377. }
  5378. do {
  5379. ICHAR *dataPtr = (ICHAR *)dataBuf;
  5380. convert_res = XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
  5381. *eventEndPP = s;
  5382. defaultHandler(handlerArg, dataBuf, (int)(dataPtr - (ICHAR *)dataBuf));
  5383. *eventPP = s;
  5384. } while ((convert_res != XML_CONVERT_COMPLETED) && (convert_res != XML_CONVERT_INPUT_INCOMPLETE));
  5385. }
  5386. else
  5387. defaultHandler(handlerArg, (XML_Char *)s, (int)((XML_Char *)end - (XML_Char *)s));
  5388. }
  5389. static int
  5390. defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *attId, XML_Bool isCdata,
  5391. XML_Bool isId, const XML_Char *value, XML_Parser parser)
  5392. {
  5393. DEFAULT_ATTRIBUTE *att;
  5394. if (value || isId) {
  5395. /* The handling of default attributes gets messed up if we have
  5396. a default which duplicates a non-default. */
  5397. int i;
  5398. for (i = 0; i < type->nDefaultAtts; i++)
  5399. if (attId == type->defaultAtts[i].id)
  5400. return 1;
  5401. if (isId && !type->idAtt && !attId->xmlns)
  5402. type->idAtt = attId;
  5403. }
  5404. if (type->nDefaultAtts == type->allocDefaultAtts) {
  5405. if (type->allocDefaultAtts == 0) {
  5406. type->allocDefaultAtts = 8;
  5407. type->defaultAtts = (DEFAULT_ATTRIBUTE *)MALLOC(type->allocDefaultAtts
  5408. * sizeof(DEFAULT_ATTRIBUTE));
  5409. if (!type->defaultAtts)
  5410. return 0;
  5411. }
  5412. else {
  5413. DEFAULT_ATTRIBUTE *temp;
  5414. int count = type->allocDefaultAtts * 2;
  5415. temp = (DEFAULT_ATTRIBUTE *)
  5416. REALLOC(type->defaultAtts, (count * sizeof(DEFAULT_ATTRIBUTE)));
  5417. if (temp == NULL)
  5418. return 0;
  5419. type->allocDefaultAtts = count;
  5420. type->defaultAtts = temp;
  5421. }
  5422. }
  5423. att = type->defaultAtts + type->nDefaultAtts;
  5424. att->id = attId;
  5425. att->value = value;
  5426. att->isCdata = isCdata;
  5427. if (!isCdata)
  5428. attId->maybeTokenized = XML_TRUE;
  5429. type->nDefaultAtts += 1;
  5430. return 1;
  5431. }
  5432. static int
  5433. setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *elementType)
  5434. {
  5435. DTD * const dtd = _dtd; /* save one level of indirection */
  5436. const XML_Char *name;
  5437. for (name = elementType->name; *name; name++) {
  5438. if (*name == XML_T(ASCII_COLON)) {
  5439. PREFIX *prefix;
  5440. const XML_Char *s;
  5441. for (s = elementType->name; s != name; s++) {
  5442. if (!poolAppendChar(&dtd->pool, *s))
  5443. return 0;
  5444. }
  5445. if (!poolAppendChar(&dtd->pool, XML_T('\0')))
  5446. return 0;
  5447. prefix = (PREFIX *)lookup(parser, &dtd->prefixes, poolStart(&dtd->pool),
  5448. sizeof(PREFIX));
  5449. if (!prefix)
  5450. return 0;
  5451. if (prefix->name == poolStart(&dtd->pool))
  5452. poolFinish(&dtd->pool);
  5453. else
  5454. poolDiscard(&dtd->pool);
  5455. elementType->prefix = prefix;
  5456. }
  5457. }
  5458. return 1;
  5459. }
  5460. static ATTRIBUTE_ID *
  5461. getAttributeId(XML_Parser parser, const ENCODING *enc,
  5462. const char *start, const char *end)
  5463. {
  5464. DTD * const dtd = _dtd; /* save one level of indirection */
  5465. ATTRIBUTE_ID *id;
  5466. const XML_Char *name;
  5467. if (!poolAppendChar(&dtd->pool, XML_T('\0')))
  5468. return NULL;
  5469. name = poolStoreString(&dtd->pool, enc, start, end);
  5470. if (!name)
  5471. return NULL;
  5472. /* skip quotation mark - its storage will be re-used (like in name[-1]) */
  5473. ++name;
  5474. id = (ATTRIBUTE_ID *)lookup(parser, &dtd->attributeIds, name, sizeof(ATTRIBUTE_ID));
  5475. if (!id)
  5476. return NULL;
  5477. if (id->name != name)
  5478. poolDiscard(&dtd->pool);
  5479. else {
  5480. poolFinish(&dtd->pool);
  5481. if (!ns)
  5482. ;
  5483. else if (name[0] == XML_T(ASCII_x)
  5484. && name[1] == XML_T(ASCII_m)
  5485. && name[2] == XML_T(ASCII_l)
  5486. && name[3] == XML_T(ASCII_n)
  5487. && name[4] == XML_T(ASCII_s)
  5488. && (name[5] == XML_T('\0') || name[5] == XML_T(ASCII_COLON))) {
  5489. if (name[5] == XML_T('\0'))
  5490. id->prefix = &dtd->defaultPrefix;
  5491. else
  5492. id->prefix = (PREFIX *)lookup(parser, &dtd->prefixes, name + 6, sizeof(PREFIX));
  5493. id->xmlns = XML_TRUE;
  5494. }
  5495. else {
  5496. int i;
  5497. for (i = 0; name[i]; i++) {
  5498. /* attributes without prefix are *not* in the default namespace */
  5499. if (name[i] == XML_T(ASCII_COLON)) {
  5500. int j;
  5501. for (j = 0; j < i; j++) {
  5502. if (!poolAppendChar(&dtd->pool, name[j]))
  5503. return NULL;
  5504. }
  5505. if (!poolAppendChar(&dtd->pool, XML_T('\0')))
  5506. return NULL;
  5507. id->prefix = (PREFIX *)lookup(parser, &dtd->prefixes, poolStart(&dtd->pool),
  5508. sizeof(PREFIX));
  5509. if (!id->prefix)
  5510. return NULL;
  5511. if (id->prefix->name == poolStart(&dtd->pool))
  5512. poolFinish(&dtd->pool);
  5513. else
  5514. poolDiscard(&dtd->pool);
  5515. break;
  5516. }
  5517. }
  5518. }
  5519. }
  5520. return id;
  5521. }
  5522. #define CONTEXT_SEP XML_T(ASCII_FF)
  5523. static const XML_Char *
  5524. getContext(XML_Parser parser)
  5525. {
  5526. DTD * const dtd = _dtd; /* save one level of indirection */
  5527. HASH_TABLE_ITER iter;
  5528. XML_Bool needSep = XML_FALSE;
  5529. if (dtd->defaultPrefix.binding) {
  5530. int i;
  5531. int len;
  5532. if (!poolAppendChar(&tempPool, XML_T(ASCII_EQUALS)))
  5533. return NULL;
  5534. len = dtd->defaultPrefix.binding->uriLen;
  5535. if (namespaceSeparator)
  5536. len--;
  5537. for (i = 0; i < len; i++)
  5538. if (!poolAppendChar(&tempPool, dtd->defaultPrefix.binding->uri[i]))
  5539. return NULL;
  5540. needSep = XML_TRUE;
  5541. }
  5542. hashTableIterInit(&iter, &(dtd->prefixes));
  5543. for (;;) {
  5544. int i;
  5545. int len;
  5546. const XML_Char *s;
  5547. PREFIX *prefix = (PREFIX *)hashTableIterNext(&iter);
  5548. if (!prefix)
  5549. break;
  5550. if (!prefix->binding)
  5551. continue;
  5552. if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
  5553. return NULL;
  5554. for (s = prefix->name; *s; s++)
  5555. if (!poolAppendChar(&tempPool, *s))
  5556. return NULL;
  5557. if (!poolAppendChar(&tempPool, XML_T(ASCII_EQUALS)))
  5558. return NULL;
  5559. len = prefix->binding->uriLen;
  5560. if (namespaceSeparator)
  5561. len--;
  5562. for (i = 0; i < len; i++)
  5563. if (!poolAppendChar(&tempPool, prefix->binding->uri[i]))
  5564. return NULL;
  5565. needSep = XML_TRUE;
  5566. }
  5567. hashTableIterInit(&iter, &(dtd->generalEntities));
  5568. for (;;) {
  5569. const XML_Char *s;
  5570. ENTITY *e = (ENTITY *)hashTableIterNext(&iter);
  5571. if (!e)
  5572. break;
  5573. if (!e->open)
  5574. continue;
  5575. if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
  5576. return NULL;
  5577. for (s = e->name; *s; s++)
  5578. if (!poolAppendChar(&tempPool, *s))
  5579. return 0;
  5580. needSep = XML_TRUE;
  5581. }
  5582. if (!poolAppendChar(&tempPool, XML_T('\0')))
  5583. return NULL;
  5584. return tempPool.start;
  5585. }
  5586. static XML_Bool
  5587. setContext(XML_Parser parser, const XML_Char *context)
  5588. {
  5589. DTD * const dtd = _dtd; /* save one level of indirection */
  5590. const XML_Char *s = context;
  5591. while (*context != XML_T('\0')) {
  5592. if (*s == CONTEXT_SEP || *s == XML_T('\0')) {
  5593. ENTITY *e;
  5594. if (!poolAppendChar(&tempPool, XML_T('\0')))
  5595. return XML_FALSE;
  5596. e = (ENTITY *)lookup(parser, &dtd->generalEntities, poolStart(&tempPool), 0);
  5597. if (e)
  5598. e->open = XML_TRUE;
  5599. if (*s != XML_T('\0'))
  5600. s++;
  5601. context = s;
  5602. poolDiscard(&tempPool);
  5603. }
  5604. else if (*s == XML_T(ASCII_EQUALS)) {
  5605. PREFIX *prefix;
  5606. if (poolLength(&tempPool) == 0)
  5607. prefix = &dtd->defaultPrefix;
  5608. else {
  5609. if (!poolAppendChar(&tempPool, XML_T('\0')))
  5610. return XML_FALSE;
  5611. prefix = (PREFIX *)lookup(parser, &dtd->prefixes, poolStart(&tempPool),
  5612. sizeof(PREFIX));
  5613. if (!prefix)
  5614. return XML_FALSE;
  5615. if (prefix->name == poolStart(&tempPool)) {
  5616. prefix->name = poolCopyString(&dtd->pool, prefix->name);
  5617. if (!prefix->name)
  5618. return XML_FALSE;
  5619. }
  5620. poolDiscard(&tempPool);
  5621. }
  5622. for (context = s + 1;
  5623. *context != CONTEXT_SEP && *context != XML_T('\0');
  5624. context++)
  5625. if (!poolAppendChar(&tempPool, *context))
  5626. return XML_FALSE;
  5627. if (!poolAppendChar(&tempPool, XML_T('\0')))
  5628. return XML_FALSE;
  5629. if (addBinding(parser, prefix, NULL, poolStart(&tempPool),
  5630. &inheritedBindings) != XML_ERROR_NONE)
  5631. return XML_FALSE;
  5632. poolDiscard(&tempPool);
  5633. if (*context != XML_T('\0'))
  5634. ++context;
  5635. s = context;
  5636. }
  5637. else {
  5638. if (!poolAppendChar(&tempPool, *s))
  5639. return XML_FALSE;
  5640. s++;
  5641. }
  5642. }
  5643. return XML_TRUE;
  5644. }
  5645. static void FASTCALL
  5646. normalizePublicId(XML_Char *publicId)
  5647. {
  5648. XML_Char *p = publicId;
  5649. XML_Char *s;
  5650. for (s = publicId; *s; s++) {
  5651. switch (*s) {
  5652. case 0x20:
  5653. case 0xD:
  5654. case 0xA:
  5655. if (p != publicId && p[-1] != 0x20)
  5656. *p++ = 0x20;
  5657. break;
  5658. default:
  5659. *p++ = *s;
  5660. }
  5661. }
  5662. if (p != publicId && p[-1] == 0x20)
  5663. --p;
  5664. *p = XML_T('\0');
  5665. }
  5666. static DTD *
  5667. dtdCreate(const XML_Memory_Handling_Suite *ms)
  5668. {
  5669. DTD *p = (DTD *)ms->malloc_fcn(sizeof(DTD));
  5670. if (p == NULL)
  5671. return p;
  5672. poolInit(&(p->pool), ms);
  5673. poolInit(&(p->entityValuePool), ms);
  5674. hashTableInit(&(p->generalEntities), ms);
  5675. hashTableInit(&(p->elementTypes), ms);
  5676. hashTableInit(&(p->attributeIds), ms);
  5677. hashTableInit(&(p->prefixes), ms);
  5678. #ifdef XML_DTD
  5679. p->paramEntityRead = XML_FALSE;
  5680. hashTableInit(&(p->paramEntities), ms);
  5681. #endif /* XML_DTD */
  5682. p->defaultPrefix.name = NULL;
  5683. p->defaultPrefix.binding = NULL;
  5684. p->in_eldecl = XML_FALSE;
  5685. p->scaffIndex = NULL;
  5686. p->scaffold = NULL;
  5687. p->scaffLevel = 0;
  5688. p->scaffSize = 0;
  5689. p->scaffCount = 0;
  5690. p->contentStringLen = 0;
  5691. p->keepProcessing = XML_TRUE;
  5692. p->hasParamEntityRefs = XML_FALSE;
  5693. p->standalone = XML_FALSE;
  5694. return p;
  5695. }
  5696. static void
  5697. dtdReset(DTD *p, const XML_Memory_Handling_Suite *ms)
  5698. {
  5699. HASH_TABLE_ITER iter;
  5700. hashTableIterInit(&iter, &(p->elementTypes));
  5701. for (;;) {
  5702. ELEMENT_TYPE *e = (ELEMENT_TYPE *)hashTableIterNext(&iter);
  5703. if (!e)
  5704. break;
  5705. if (e->allocDefaultAtts != 0)
  5706. ms->free_fcn(e->defaultAtts);
  5707. }
  5708. hashTableClear(&(p->generalEntities));
  5709. #ifdef XML_DTD
  5710. p->paramEntityRead = XML_FALSE;
  5711. hashTableClear(&(p->paramEntities));
  5712. #endif /* XML_DTD */
  5713. hashTableClear(&(p->elementTypes));
  5714. hashTableClear(&(p->attributeIds));
  5715. hashTableClear(&(p->prefixes));
  5716. poolClear(&(p->pool));
  5717. poolClear(&(p->entityValuePool));
  5718. p->defaultPrefix.name = NULL;
  5719. p->defaultPrefix.binding = NULL;
  5720. p->in_eldecl = XML_FALSE;
  5721. ms->free_fcn(p->scaffIndex);
  5722. p->scaffIndex = NULL;
  5723. ms->free_fcn(p->scaffold);
  5724. p->scaffold = NULL;
  5725. p->scaffLevel = 0;
  5726. p->scaffSize = 0;
  5727. p->scaffCount = 0;
  5728. p->contentStringLen = 0;
  5729. p->keepProcessing = XML_TRUE;
  5730. p->hasParamEntityRefs = XML_FALSE;
  5731. p->standalone = XML_FALSE;
  5732. }
  5733. static void
  5734. dtdDestroy(DTD *p, XML_Bool isDocEntity, const XML_Memory_Handling_Suite *ms)
  5735. {
  5736. HASH_TABLE_ITER iter;
  5737. hashTableIterInit(&iter, &(p->elementTypes));
  5738. for (;;) {
  5739. ELEMENT_TYPE *e = (ELEMENT_TYPE *)hashTableIterNext(&iter);
  5740. if (!e)
  5741. break;
  5742. if (e->allocDefaultAtts != 0)
  5743. ms->free_fcn(e->defaultAtts);
  5744. }
  5745. hashTableDestroy(&(p->generalEntities));
  5746. #ifdef XML_DTD
  5747. hashTableDestroy(&(p->paramEntities));
  5748. #endif /* XML_DTD */
  5749. hashTableDestroy(&(p->elementTypes));
  5750. hashTableDestroy(&(p->attributeIds));
  5751. hashTableDestroy(&(p->prefixes));
  5752. poolDestroy(&(p->pool));
  5753. poolDestroy(&(p->entityValuePool));
  5754. if (isDocEntity) {
  5755. ms->free_fcn(p->scaffIndex);
  5756. ms->free_fcn(p->scaffold);
  5757. }
  5758. ms->free_fcn(p);
  5759. }
  5760. /* Do a deep copy of the DTD. Return 0 for out of memory, non-zero otherwise.
  5761. The new DTD has already been initialized.
  5762. */
  5763. static int
  5764. dtdCopy(XML_Parser oldParser, DTD *newDtd, const DTD *oldDtd, const XML_Memory_Handling_Suite *ms)
  5765. {
  5766. HASH_TABLE_ITER iter;
  5767. /* Copy the prefix table. */
  5768. hashTableIterInit(&iter, &(oldDtd->prefixes));
  5769. for (;;) {
  5770. const XML_Char *name;
  5771. const PREFIX *oldP = (PREFIX *)hashTableIterNext(&iter);
  5772. if (!oldP)
  5773. break;
  5774. name = poolCopyString(&(newDtd->pool), oldP->name);
  5775. if (!name)
  5776. return 0;
  5777. if (!lookup(oldParser, &(newDtd->prefixes), name, sizeof(PREFIX)))
  5778. return 0;
  5779. }
  5780. hashTableIterInit(&iter, &(oldDtd->attributeIds));
  5781. /* Copy the attribute id table. */
  5782. for (;;) {
  5783. ATTRIBUTE_ID *newA;
  5784. const XML_Char *name;
  5785. const ATTRIBUTE_ID *oldA = (ATTRIBUTE_ID *)hashTableIterNext(&iter);
  5786. if (!oldA)
  5787. break;
  5788. /* Remember to allocate the scratch byte before the name. */
  5789. if (!poolAppendChar(&(newDtd->pool), XML_T('\0')))
  5790. return 0;
  5791. name = poolCopyString(&(newDtd->pool), oldA->name);
  5792. if (!name)
  5793. return 0;
  5794. ++name;
  5795. newA = (ATTRIBUTE_ID *)lookup(oldParser, &(newDtd->attributeIds), name,
  5796. sizeof(ATTRIBUTE_ID));
  5797. if (!newA)
  5798. return 0;
  5799. newA->maybeTokenized = oldA->maybeTokenized;
  5800. if (oldA->prefix) {
  5801. newA->xmlns = oldA->xmlns;
  5802. if (oldA->prefix == &oldDtd->defaultPrefix)
  5803. newA->prefix = &newDtd->defaultPrefix;
  5804. else
  5805. newA->prefix = (PREFIX *)lookup(oldParser, &(newDtd->prefixes),
  5806. oldA->prefix->name, 0);
  5807. }
  5808. }
  5809. /* Copy the element type table. */
  5810. hashTableIterInit(&iter, &(oldDtd->elementTypes));
  5811. for (;;) {
  5812. int i;
  5813. ELEMENT_TYPE *newE;
  5814. const XML_Char *name;
  5815. const ELEMENT_TYPE *oldE = (ELEMENT_TYPE *)hashTableIterNext(&iter);
  5816. if (!oldE)
  5817. break;
  5818. name = poolCopyString(&(newDtd->pool), oldE->name);
  5819. if (!name)
  5820. return 0;
  5821. newE = (ELEMENT_TYPE *)lookup(oldParser, &(newDtd->elementTypes), name,
  5822. sizeof(ELEMENT_TYPE));
  5823. if (!newE)
  5824. return 0;
  5825. if (oldE->nDefaultAtts) {
  5826. newE->defaultAtts = (DEFAULT_ATTRIBUTE *)
  5827. ms->malloc_fcn(oldE->nDefaultAtts * sizeof(DEFAULT_ATTRIBUTE));
  5828. if (!newE->defaultAtts) {
  5829. return 0;
  5830. }
  5831. }
  5832. if (oldE->idAtt)
  5833. newE->idAtt = (ATTRIBUTE_ID *)
  5834. lookup(oldParser, &(newDtd->attributeIds), oldE->idAtt->name, 0);
  5835. newE->allocDefaultAtts = newE->nDefaultAtts = oldE->nDefaultAtts;
  5836. if (oldE->prefix)
  5837. newE->prefix = (PREFIX *)lookup(oldParser, &(newDtd->prefixes),
  5838. oldE->prefix->name, 0);
  5839. for (i = 0; i < newE->nDefaultAtts; i++) {
  5840. newE->defaultAtts[i].id = (ATTRIBUTE_ID *)
  5841. lookup(oldParser, &(newDtd->attributeIds), oldE->defaultAtts[i].id->name, 0);
  5842. newE->defaultAtts[i].isCdata = oldE->defaultAtts[i].isCdata;
  5843. if (oldE->defaultAtts[i].value) {
  5844. newE->defaultAtts[i].value
  5845. = poolCopyString(&(newDtd->pool), oldE->defaultAtts[i].value);
  5846. if (!newE->defaultAtts[i].value)
  5847. return 0;
  5848. }
  5849. else
  5850. newE->defaultAtts[i].value = NULL;
  5851. }
  5852. }
  5853. /* Copy the entity tables. */
  5854. if (!copyEntityTable(oldParser,
  5855. &(newDtd->generalEntities),
  5856. &(newDtd->pool),
  5857. &(oldDtd->generalEntities)))
  5858. return 0;
  5859. #ifdef XML_DTD
  5860. if (!copyEntityTable(oldParser,
  5861. &(newDtd->paramEntities),
  5862. &(newDtd->pool),
  5863. &(oldDtd->paramEntities)))
  5864. return 0;
  5865. newDtd->paramEntityRead = oldDtd->paramEntityRead;
  5866. #endif /* XML_DTD */
  5867. newDtd->keepProcessing = oldDtd->keepProcessing;
  5868. newDtd->hasParamEntityRefs = oldDtd->hasParamEntityRefs;
  5869. newDtd->standalone = oldDtd->standalone;
  5870. /* Don't want deep copying for scaffolding */
  5871. newDtd->in_eldecl = oldDtd->in_eldecl;
  5872. newDtd->scaffold = oldDtd->scaffold;
  5873. newDtd->contentStringLen = oldDtd->contentStringLen;
  5874. newDtd->scaffSize = oldDtd->scaffSize;
  5875. newDtd->scaffLevel = oldDtd->scaffLevel;
  5876. newDtd->scaffIndex = oldDtd->scaffIndex;
  5877. return 1;
  5878. } /* End dtdCopy */
  5879. static int
  5880. copyEntityTable(XML_Parser oldParser,
  5881. HASH_TABLE *newTable,
  5882. STRING_POOL *newPool,
  5883. const HASH_TABLE *oldTable)
  5884. {
  5885. HASH_TABLE_ITER iter;
  5886. const XML_Char *cachedOldBase = NULL;
  5887. const XML_Char *cachedNewBase = NULL;
  5888. hashTableIterInit(&iter, oldTable);
  5889. for (;;) {
  5890. ENTITY *newE;
  5891. const XML_Char *name;
  5892. const ENTITY *oldE = (ENTITY *)hashTableIterNext(&iter);
  5893. if (!oldE)
  5894. break;
  5895. name = poolCopyString(newPool, oldE->name);
  5896. if (!name)
  5897. return 0;
  5898. newE = (ENTITY *)lookup(oldParser, newTable, name, sizeof(ENTITY));
  5899. if (!newE)
  5900. return 0;
  5901. if (oldE->systemId) {
  5902. const XML_Char *tem = poolCopyString(newPool, oldE->systemId);
  5903. if (!tem)
  5904. return 0;
  5905. newE->systemId = tem;
  5906. if (oldE->base) {
  5907. if (oldE->base == cachedOldBase)
  5908. newE->base = cachedNewBase;
  5909. else {
  5910. cachedOldBase = oldE->base;
  5911. tem = poolCopyString(newPool, cachedOldBase);
  5912. if (!tem)
  5913. return 0;
  5914. cachedNewBase = newE->base = tem;
  5915. }
  5916. }
  5917. if (oldE->publicId) {
  5918. tem = poolCopyString(newPool, oldE->publicId);
  5919. if (!tem)
  5920. return 0;
  5921. newE->publicId = tem;
  5922. }
  5923. }
  5924. else {
  5925. const XML_Char *tem = poolCopyStringN(newPool, oldE->textPtr,
  5926. oldE->textLen);
  5927. if (!tem)
  5928. return 0;
  5929. newE->textPtr = tem;
  5930. newE->textLen = oldE->textLen;
  5931. }
  5932. if (oldE->notation) {
  5933. const XML_Char *tem = poolCopyString(newPool, oldE->notation);
  5934. if (!tem)
  5935. return 0;
  5936. newE->notation = tem;
  5937. }
  5938. newE->is_param = oldE->is_param;
  5939. newE->is_internal = oldE->is_internal;
  5940. }
  5941. return 1;
  5942. }
  5943. #define INIT_POWER 6
  5944. static XML_Bool FASTCALL
  5945. keyeq(KEY s1, KEY s2)
  5946. {
  5947. for (; *s1 == *s2; s1++, s2++)
  5948. if (*s1 == 0)
  5949. return XML_TRUE;
  5950. return XML_FALSE;
  5951. }
  5952. static size_t
  5953. keylen(KEY s)
  5954. {
  5955. size_t len = 0;
  5956. for (; *s; s++, len++);
  5957. return len;
  5958. }
  5959. static void
  5960. copy_salt_to_sipkey(XML_Parser parser, struct sipkey * key)
  5961. {
  5962. key->k[0] = 0;
  5963. key->k[1] = get_hash_secret_salt(parser);
  5964. }
  5965. static unsigned long FASTCALL
  5966. hash(XML_Parser parser, KEY s)
  5967. {
  5968. struct siphash state;
  5969. struct sipkey key;
  5970. (void)sip_tobin;
  5971. (void)sip24_valid;
  5972. copy_salt_to_sipkey(parser, &key);
  5973. sip24_init(&state, &key);
  5974. sip24_update(&state, s, keylen(s) * sizeof(XML_Char));
  5975. return (unsigned long)sip24_final(&state);
  5976. }
  5977. static NAMED *
  5978. lookup(XML_Parser parser, HASH_TABLE *table, KEY name, size_t createSize)
  5979. {
  5980. size_t i;
  5981. if (table->size == 0) {
  5982. size_t tsize;
  5983. if (!createSize)
  5984. return NULL;
  5985. table->power = INIT_POWER;
  5986. /* table->size is a power of 2 */
  5987. table->size = (size_t)1 << INIT_POWER;
  5988. tsize = table->size * sizeof(NAMED *);
  5989. table->v = (NAMED **)table->mem->malloc_fcn(tsize);
  5990. if (!table->v) {
  5991. table->size = 0;
  5992. return NULL;
  5993. }
  5994. memset(table->v, 0, tsize);
  5995. i = hash(parser, name) & ((unsigned long)table->size - 1);
  5996. }
  5997. else {
  5998. unsigned long h = hash(parser, name);
  5999. unsigned long mask = (unsigned long)table->size - 1;
  6000. unsigned char step = 0;
  6001. i = h & mask;
  6002. while (table->v[i]) {
  6003. if (keyeq(name, table->v[i]->name))
  6004. return table->v[i];
  6005. if (!step)
  6006. step = PROBE_STEP(h, mask, table->power);
  6007. i < step ? (i += table->size - step) : (i -= step);
  6008. }
  6009. if (!createSize)
  6010. return NULL;
  6011. /* check for overflow (table is half full) */
  6012. if (table->used >> (table->power - 1)) {
  6013. unsigned char newPower = table->power + 1;
  6014. size_t newSize = (size_t)1 << newPower;
  6015. unsigned long newMask = (unsigned long)newSize - 1;
  6016. size_t tsize = newSize * sizeof(NAMED *);
  6017. NAMED **newV = (NAMED **)table->mem->malloc_fcn(tsize);
  6018. if (!newV)
  6019. return NULL;
  6020. memset(newV, 0, tsize);
  6021. for (i = 0; i < table->size; i++)
  6022. if (table->v[i]) {
  6023. unsigned long newHash = hash(parser, table->v[i]->name);
  6024. size_t j = newHash & newMask;
  6025. step = 0;
  6026. while (newV[j]) {
  6027. if (!step)
  6028. step = PROBE_STEP(newHash, newMask, newPower);
  6029. j < step ? (j += newSize - step) : (j -= step);
  6030. }
  6031. newV[j] = table->v[i];
  6032. }
  6033. table->mem->free_fcn(table->v);
  6034. table->v = newV;
  6035. table->power = newPower;
  6036. table->size = newSize;
  6037. i = h & newMask;
  6038. step = 0;
  6039. while (table->v[i]) {
  6040. if (!step)
  6041. step = PROBE_STEP(h, newMask, newPower);
  6042. i < step ? (i += newSize - step) : (i -= step);
  6043. }
  6044. }
  6045. }
  6046. table->v[i] = (NAMED *)table->mem->malloc_fcn(createSize);
  6047. if (!table->v[i])
  6048. return NULL;
  6049. memset(table->v[i], 0, createSize);
  6050. table->v[i]->name = name;
  6051. (table->used)++;
  6052. return table->v[i];
  6053. }
  6054. static void FASTCALL
  6055. hashTableClear(HASH_TABLE *table)
  6056. {
  6057. size_t i;
  6058. for (i = 0; i < table->size; i++) {
  6059. table->mem->free_fcn(table->v[i]);
  6060. table->v[i] = NULL;
  6061. }
  6062. table->used = 0;
  6063. }
  6064. static void FASTCALL
  6065. hashTableDestroy(HASH_TABLE *table)
  6066. {
  6067. size_t i;
  6068. for (i = 0; i < table->size; i++)
  6069. table->mem->free_fcn(table->v[i]);
  6070. table->mem->free_fcn(table->v);
  6071. }
  6072. static void FASTCALL
  6073. hashTableInit(HASH_TABLE *p, const XML_Memory_Handling_Suite *ms)
  6074. {
  6075. p->power = 0;
  6076. p->size = 0;
  6077. p->used = 0;
  6078. p->v = NULL;
  6079. p->mem = ms;
  6080. }
  6081. static void FASTCALL
  6082. hashTableIterInit(HASH_TABLE_ITER *iter, const HASH_TABLE *table)
  6083. {
  6084. iter->p = table->v;
  6085. iter->end = iter->p + table->size;
  6086. }
  6087. static NAMED * FASTCALL
  6088. hashTableIterNext(HASH_TABLE_ITER *iter)
  6089. {
  6090. while (iter->p != iter->end) {
  6091. NAMED *tem = *(iter->p)++;
  6092. if (tem)
  6093. return tem;
  6094. }
  6095. return NULL;
  6096. }
  6097. static void FASTCALL
  6098. poolInit(STRING_POOL *pool, const XML_Memory_Handling_Suite *ms)
  6099. {
  6100. pool->blocks = NULL;
  6101. pool->freeBlocks = NULL;
  6102. pool->start = NULL;
  6103. pool->ptr = NULL;
  6104. pool->end = NULL;
  6105. pool->mem = ms;
  6106. }
  6107. static void FASTCALL
  6108. poolClear(STRING_POOL *pool)
  6109. {
  6110. if (!pool->freeBlocks)
  6111. pool->freeBlocks = pool->blocks;
  6112. else {
  6113. BLOCK *p = pool->blocks;
  6114. while (p) {
  6115. BLOCK *tem = p->next;
  6116. p->next = pool->freeBlocks;
  6117. pool->freeBlocks = p;
  6118. p = tem;
  6119. }
  6120. }
  6121. pool->blocks = NULL;
  6122. pool->start = NULL;
  6123. pool->ptr = NULL;
  6124. pool->end = NULL;
  6125. }
  6126. static void FASTCALL
  6127. poolDestroy(STRING_POOL *pool)
  6128. {
  6129. BLOCK *p = pool->blocks;
  6130. while (p) {
  6131. BLOCK *tem = p->next;
  6132. pool->mem->free_fcn(p);
  6133. p = tem;
  6134. }
  6135. p = pool->freeBlocks;
  6136. while (p) {
  6137. BLOCK *tem = p->next;
  6138. pool->mem->free_fcn(p);
  6139. p = tem;
  6140. }
  6141. }
  6142. static XML_Char *
  6143. poolAppend(STRING_POOL *pool, const ENCODING *enc,
  6144. const char *ptr, const char *end)
  6145. {
  6146. if (!pool->ptr && !poolGrow(pool))
  6147. return NULL;
  6148. for (;;) {
  6149. const enum XML_Convert_Result convert_res = XmlConvert(enc, &ptr, end, (ICHAR **)&(pool->ptr), (ICHAR *)pool->end);
  6150. if ((convert_res == XML_CONVERT_COMPLETED) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE))
  6151. break;
  6152. if (!poolGrow(pool))
  6153. return NULL;
  6154. }
  6155. return pool->start;
  6156. }
  6157. static const XML_Char * FASTCALL
  6158. poolCopyString(STRING_POOL *pool, const XML_Char *s)
  6159. {
  6160. do {
  6161. if (!poolAppendChar(pool, *s))
  6162. return NULL;
  6163. } while (*s++);
  6164. s = pool->start;
  6165. poolFinish(pool);
  6166. return s;
  6167. }
  6168. static const XML_Char *
  6169. poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n)
  6170. {
  6171. if (!pool->ptr && !poolGrow(pool))
  6172. return NULL;
  6173. for (; n > 0; --n, s++) {
  6174. if (!poolAppendChar(pool, *s))
  6175. return NULL;
  6176. }
  6177. s = pool->start;
  6178. poolFinish(pool);
  6179. return s;
  6180. }
  6181. static const XML_Char * FASTCALL
  6182. poolAppendString(STRING_POOL *pool, const XML_Char *s)
  6183. {
  6184. while (*s) {
  6185. if (!poolAppendChar(pool, *s))
  6186. return NULL;
  6187. s++;
  6188. }
  6189. return pool->start;
  6190. }
  6191. static XML_Char *
  6192. poolStoreString(STRING_POOL *pool, const ENCODING *enc,
  6193. const char *ptr, const char *end)
  6194. {
  6195. if (!poolAppend(pool, enc, ptr, end))
  6196. return NULL;
  6197. if (pool->ptr == pool->end && !poolGrow(pool))
  6198. return NULL;
  6199. *(pool->ptr)++ = 0;
  6200. return pool->start;
  6201. }
  6202. static size_t
  6203. poolBytesToAllocateFor(int blockSize)
  6204. {
  6205. /* Unprotected math would be:
  6206. ** return offsetof(BLOCK, s) + blockSize * sizeof(XML_Char);
  6207. **
  6208. ** Detect overflow, avoiding _signed_ overflow undefined behavior
  6209. ** For a + b * c we check b * c in isolation first, so that addition of a
  6210. ** on top has no chance of making us accept a small non-negative number
  6211. */
  6212. const size_t stretch = sizeof(XML_Char); /* can be 4 bytes */
  6213. if (blockSize <= 0)
  6214. return 0;
  6215. if (blockSize > (int)(INT_MAX / stretch))
  6216. return 0;
  6217. {
  6218. const int stretchedBlockSize = blockSize * (int)stretch;
  6219. const int bytesToAllocate = (int)(
  6220. offsetof(BLOCK, s) + (unsigned)stretchedBlockSize);
  6221. if (bytesToAllocate < 0)
  6222. return 0;
  6223. return (size_t)bytesToAllocate;
  6224. }
  6225. }
  6226. static XML_Bool FASTCALL
  6227. poolGrow(STRING_POOL *pool)
  6228. {
  6229. if (pool->freeBlocks) {
  6230. if (pool->start == 0) {
  6231. pool->blocks = pool->freeBlocks;
  6232. pool->freeBlocks = pool->freeBlocks->next;
  6233. pool->blocks->next = NULL;
  6234. pool->start = pool->blocks->s;
  6235. pool->end = pool->start + pool->blocks->size;
  6236. pool->ptr = pool->start;
  6237. return XML_TRUE;
  6238. }
  6239. if (pool->end - pool->start < pool->freeBlocks->size) {
  6240. BLOCK *tem = pool->freeBlocks->next;
  6241. pool->freeBlocks->next = pool->blocks;
  6242. pool->blocks = pool->freeBlocks;
  6243. pool->freeBlocks = tem;
  6244. memcpy(pool->blocks->s, pool->start,
  6245. (pool->end - pool->start) * sizeof(XML_Char));
  6246. pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
  6247. pool->start = pool->blocks->s;
  6248. pool->end = pool->start + pool->blocks->size;
  6249. return XML_TRUE;
  6250. }
  6251. }
  6252. if (pool->blocks && pool->start == pool->blocks->s) {
  6253. BLOCK *temp;
  6254. int blockSize = (int)((unsigned)(pool->end - pool->start)*2U);
  6255. size_t bytesToAllocate;
  6256. if (blockSize < 0)
  6257. return XML_FALSE;
  6258. bytesToAllocate = poolBytesToAllocateFor(blockSize);
  6259. if (bytesToAllocate == 0)
  6260. return XML_FALSE;
  6261. temp = (BLOCK *)
  6262. pool->mem->realloc_fcn(pool->blocks, (unsigned)bytesToAllocate);
  6263. if (temp == NULL)
  6264. return XML_FALSE;
  6265. pool->blocks = temp;
  6266. pool->blocks->size = blockSize;
  6267. pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
  6268. pool->start = pool->blocks->s;
  6269. pool->end = pool->start + blockSize;
  6270. }
  6271. else {
  6272. BLOCK *tem;
  6273. int blockSize = (int)(pool->end - pool->start);
  6274. size_t bytesToAllocate;
  6275. if (blockSize < 0)
  6276. return XML_FALSE;
  6277. if (blockSize < INIT_BLOCK_SIZE)
  6278. blockSize = INIT_BLOCK_SIZE;
  6279. else {
  6280. /* Detect overflow, avoiding _signed_ overflow undefined behavior */
  6281. if ((int)((unsigned)blockSize * 2U) < 0) {
  6282. return XML_FALSE;
  6283. }
  6284. blockSize *= 2;
  6285. }
  6286. bytesToAllocate = poolBytesToAllocateFor(blockSize);
  6287. if (bytesToAllocate == 0)
  6288. return XML_FALSE;
  6289. tem = (BLOCK *)pool->mem->malloc_fcn(bytesToAllocate);
  6290. if (!tem)
  6291. return XML_FALSE;
  6292. tem->size = blockSize;
  6293. tem->next = pool->blocks;
  6294. pool->blocks = tem;
  6295. if (pool->ptr != pool->start)
  6296. memcpy(tem->s, pool->start,
  6297. (pool->ptr - pool->start) * sizeof(XML_Char));
  6298. pool->ptr = tem->s + (pool->ptr - pool->start);
  6299. pool->start = tem->s;
  6300. pool->end = tem->s + blockSize;
  6301. }
  6302. return XML_TRUE;
  6303. }
  6304. static int FASTCALL
  6305. nextScaffoldPart(XML_Parser parser)
  6306. {
  6307. DTD * const dtd = _dtd; /* save one level of indirection */
  6308. CONTENT_SCAFFOLD * me;
  6309. int next;
  6310. if (!dtd->scaffIndex) {
  6311. dtd->scaffIndex = (int *)MALLOC(groupSize * sizeof(int));
  6312. if (!dtd->scaffIndex)
  6313. return -1;
  6314. dtd->scaffIndex[0] = 0;
  6315. }
  6316. if (dtd->scaffCount >= dtd->scaffSize) {
  6317. CONTENT_SCAFFOLD *temp;
  6318. if (dtd->scaffold) {
  6319. temp = (CONTENT_SCAFFOLD *)
  6320. REALLOC(dtd->scaffold, dtd->scaffSize * 2 * sizeof(CONTENT_SCAFFOLD));
  6321. if (temp == NULL)
  6322. return -1;
  6323. dtd->scaffSize *= 2;
  6324. }
  6325. else {
  6326. temp = (CONTENT_SCAFFOLD *)MALLOC(INIT_SCAFFOLD_ELEMENTS
  6327. * sizeof(CONTENT_SCAFFOLD));
  6328. if (temp == NULL)
  6329. return -1;
  6330. dtd->scaffSize = INIT_SCAFFOLD_ELEMENTS;
  6331. }
  6332. dtd->scaffold = temp;
  6333. }
  6334. next = dtd->scaffCount++;
  6335. me = &dtd->scaffold[next];
  6336. if (dtd->scaffLevel) {
  6337. CONTENT_SCAFFOLD *parent = &dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel-1]];
  6338. if (parent->lastchild) {
  6339. dtd->scaffold[parent->lastchild].nextsib = next;
  6340. }
  6341. if (!parent->childcnt)
  6342. parent->firstchild = next;
  6343. parent->lastchild = next;
  6344. parent->childcnt++;
  6345. }
  6346. me->firstchild = me->lastchild = me->childcnt = me->nextsib = 0;
  6347. return next;
  6348. }
  6349. static void
  6350. build_node(XML_Parser parser,
  6351. int src_node,
  6352. XML_Content *dest,
  6353. XML_Content **contpos,
  6354. XML_Char **strpos)
  6355. {
  6356. DTD * const dtd = _dtd; /* save one level of indirection */
  6357. dest->type = dtd->scaffold[src_node].type;
  6358. dest->quant = dtd->scaffold[src_node].quant;
  6359. if (dest->type == XML_CTYPE_NAME) {
  6360. const XML_Char *src;
  6361. dest->name = *strpos;
  6362. src = dtd->scaffold[src_node].name;
  6363. for (;;) {
  6364. *(*strpos)++ = *src;
  6365. if (!*src)
  6366. break;
  6367. src++;
  6368. }
  6369. dest->numchildren = 0;
  6370. dest->children = NULL;
  6371. }
  6372. else {
  6373. unsigned int i;
  6374. int cn;
  6375. dest->numchildren = dtd->scaffold[src_node].childcnt;
  6376. dest->children = *contpos;
  6377. *contpos += dest->numchildren;
  6378. for (i = 0, cn = dtd->scaffold[src_node].firstchild;
  6379. i < dest->numchildren;
  6380. i++, cn = dtd->scaffold[cn].nextsib) {
  6381. build_node(parser, cn, &(dest->children[i]), contpos, strpos);
  6382. }
  6383. dest->name = NULL;
  6384. }
  6385. }
  6386. static XML_Content *
  6387. build_model (XML_Parser parser)
  6388. {
  6389. DTD * const dtd = _dtd; /* save one level of indirection */
  6390. XML_Content *ret;
  6391. XML_Content *cpos;
  6392. XML_Char * str;
  6393. int allocsize = (dtd->scaffCount * sizeof(XML_Content)
  6394. + (dtd->contentStringLen * sizeof(XML_Char)));
  6395. ret = (XML_Content *)MALLOC(allocsize);
  6396. if (!ret)
  6397. return NULL;
  6398. str = (XML_Char *) (&ret[dtd->scaffCount]);
  6399. cpos = &ret[1];
  6400. build_node(parser, 0, ret, &cpos, &str);
  6401. return ret;
  6402. }
  6403. static ELEMENT_TYPE *
  6404. getElementType(XML_Parser parser,
  6405. const ENCODING *enc,
  6406. const char *ptr,
  6407. const char *end)
  6408. {
  6409. DTD * const dtd = _dtd; /* save one level of indirection */
  6410. const XML_Char *name = poolStoreString(&dtd->pool, enc, ptr, end);
  6411. ELEMENT_TYPE *ret;
  6412. if (!name)
  6413. return NULL;
  6414. ret = (ELEMENT_TYPE *) lookup(parser, &dtd->elementTypes, name, sizeof(ELEMENT_TYPE));
  6415. if (!ret)
  6416. return NULL;
  6417. if (ret->name != name)
  6418. poolDiscard(&dtd->pool);
  6419. else {
  6420. poolFinish(&dtd->pool);
  6421. if (!setElementTypePrefix(parser, ret))
  6422. return NULL;
  6423. }
  6424. return ret;
  6425. }