xmlparse.c 195 KB

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