xmlparse.c 196 KB

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