xmlregexp.c 215 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153
  1. /*
  2. * regexp.c: generic and extensible Regular Expression engine
  3. *
  4. * Basically designed with the purpose of compiling regexps for
  5. * the variety of validation/shemas mechanisms now available in
  6. * XML related specifications these include:
  7. * - XML-1.0 DTD validation
  8. * - XML Schemas structure part 1
  9. * - XML Schemas Datatypes part 2 especially Appendix F
  10. * - RELAX-NG/TREX i.e. the counter proposal
  11. *
  12. * See Copyright for the status of this software.
  13. *
  14. * Daniel Veillard <veillard@redhat.com>
  15. */
  16. #define IN_LIBXML
  17. #include "libxml.h"
  18. #ifdef LIBXML_REGEXP_ENABLED
  19. /* #define DEBUG_ERR */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #ifdef HAVE_LIMITS_H
  23. #include <limits.h>
  24. #endif
  25. #include <libxml/tree.h>
  26. #include <libxml/parserInternals.h>
  27. #include <libxml/xmlregexp.h>
  28. #include <libxml/xmlautomata.h>
  29. #include <libxml/xmlunicode.h>
  30. #ifndef INT_MAX
  31. #define INT_MAX 123456789 /* easy to flag and big enough for our needs */
  32. #endif
  33. /* #define DEBUG_REGEXP_GRAPH */
  34. /* #define DEBUG_REGEXP_EXEC */
  35. /* #define DEBUG_PUSH */
  36. /* #define DEBUG_COMPACTION */
  37. #define MAX_PUSH 10000000
  38. #define ERROR(str) \
  39. ctxt->error = XML_REGEXP_COMPILE_ERROR; \
  40. xmlRegexpErrCompile(ctxt, str);
  41. #define NEXT ctxt->cur++
  42. #define CUR (*(ctxt->cur))
  43. #define NXT(index) (ctxt->cur[index])
  44. #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
  45. #define NEXTL(l) ctxt->cur += l;
  46. #define XML_REG_STRING_SEPARATOR '|'
  47. /*
  48. * Need PREV to check on a '-' within a Character Group. May only be used
  49. * when it's guaranteed that cur is not at the beginning of ctxt->string!
  50. */
  51. #define PREV (ctxt->cur[-1])
  52. /**
  53. * TODO:
  54. *
  55. * macro to flag unimplemented blocks
  56. */
  57. #define TODO \
  58. xmlGenericError(xmlGenericErrorContext, \
  59. "Unimplemented block at %s:%d\n", \
  60. __FILE__, __LINE__);
  61. /************************************************************************
  62. * *
  63. * Datatypes and structures *
  64. * *
  65. ************************************************************************/
  66. /*
  67. * Note: the order of the enums below is significant, do not shuffle
  68. */
  69. typedef enum {
  70. XML_REGEXP_EPSILON = 1,
  71. XML_REGEXP_CHARVAL,
  72. XML_REGEXP_RANGES,
  73. XML_REGEXP_SUBREG, /* used for () sub regexps */
  74. XML_REGEXP_STRING,
  75. XML_REGEXP_ANYCHAR, /* . */
  76. XML_REGEXP_ANYSPACE, /* \s */
  77. XML_REGEXP_NOTSPACE, /* \S */
  78. XML_REGEXP_INITNAME, /* \l */
  79. XML_REGEXP_NOTINITNAME, /* \L */
  80. XML_REGEXP_NAMECHAR, /* \c */
  81. XML_REGEXP_NOTNAMECHAR, /* \C */
  82. XML_REGEXP_DECIMAL, /* \d */
  83. XML_REGEXP_NOTDECIMAL, /* \D */
  84. XML_REGEXP_REALCHAR, /* \w */
  85. XML_REGEXP_NOTREALCHAR, /* \W */
  86. XML_REGEXP_LETTER = 100,
  87. XML_REGEXP_LETTER_UPPERCASE,
  88. XML_REGEXP_LETTER_LOWERCASE,
  89. XML_REGEXP_LETTER_TITLECASE,
  90. XML_REGEXP_LETTER_MODIFIER,
  91. XML_REGEXP_LETTER_OTHERS,
  92. XML_REGEXP_MARK,
  93. XML_REGEXP_MARK_NONSPACING,
  94. XML_REGEXP_MARK_SPACECOMBINING,
  95. XML_REGEXP_MARK_ENCLOSING,
  96. XML_REGEXP_NUMBER,
  97. XML_REGEXP_NUMBER_DECIMAL,
  98. XML_REGEXP_NUMBER_LETTER,
  99. XML_REGEXP_NUMBER_OTHERS,
  100. XML_REGEXP_PUNCT,
  101. XML_REGEXP_PUNCT_CONNECTOR,
  102. XML_REGEXP_PUNCT_DASH,
  103. XML_REGEXP_PUNCT_OPEN,
  104. XML_REGEXP_PUNCT_CLOSE,
  105. XML_REGEXP_PUNCT_INITQUOTE,
  106. XML_REGEXP_PUNCT_FINQUOTE,
  107. XML_REGEXP_PUNCT_OTHERS,
  108. XML_REGEXP_SEPAR,
  109. XML_REGEXP_SEPAR_SPACE,
  110. XML_REGEXP_SEPAR_LINE,
  111. XML_REGEXP_SEPAR_PARA,
  112. XML_REGEXP_SYMBOL,
  113. XML_REGEXP_SYMBOL_MATH,
  114. XML_REGEXP_SYMBOL_CURRENCY,
  115. XML_REGEXP_SYMBOL_MODIFIER,
  116. XML_REGEXP_SYMBOL_OTHERS,
  117. XML_REGEXP_OTHER,
  118. XML_REGEXP_OTHER_CONTROL,
  119. XML_REGEXP_OTHER_FORMAT,
  120. XML_REGEXP_OTHER_PRIVATE,
  121. XML_REGEXP_OTHER_NA,
  122. XML_REGEXP_BLOCK_NAME
  123. } xmlRegAtomType;
  124. typedef enum {
  125. XML_REGEXP_QUANT_EPSILON = 1,
  126. XML_REGEXP_QUANT_ONCE,
  127. XML_REGEXP_QUANT_OPT,
  128. XML_REGEXP_QUANT_MULT,
  129. XML_REGEXP_QUANT_PLUS,
  130. XML_REGEXP_QUANT_ONCEONLY,
  131. XML_REGEXP_QUANT_ALL,
  132. XML_REGEXP_QUANT_RANGE
  133. } xmlRegQuantType;
  134. typedef enum {
  135. XML_REGEXP_START_STATE = 1,
  136. XML_REGEXP_FINAL_STATE,
  137. XML_REGEXP_TRANS_STATE,
  138. XML_REGEXP_SINK_STATE,
  139. XML_REGEXP_UNREACH_STATE
  140. } xmlRegStateType;
  141. typedef enum {
  142. XML_REGEXP_MARK_NORMAL = 0,
  143. XML_REGEXP_MARK_START,
  144. XML_REGEXP_MARK_VISITED
  145. } xmlRegMarkedType;
  146. typedef struct _xmlRegRange xmlRegRange;
  147. typedef xmlRegRange *xmlRegRangePtr;
  148. struct _xmlRegRange {
  149. int neg; /* 0 normal, 1 not, 2 exclude */
  150. xmlRegAtomType type;
  151. int start;
  152. int end;
  153. xmlChar *blockName;
  154. };
  155. typedef struct _xmlRegAtom xmlRegAtom;
  156. typedef xmlRegAtom *xmlRegAtomPtr;
  157. typedef struct _xmlAutomataState xmlRegState;
  158. typedef xmlRegState *xmlRegStatePtr;
  159. struct _xmlRegAtom {
  160. int no;
  161. xmlRegAtomType type;
  162. xmlRegQuantType quant;
  163. int min;
  164. int max;
  165. void *valuep;
  166. void *valuep2;
  167. int neg;
  168. int codepoint;
  169. xmlRegStatePtr start;
  170. xmlRegStatePtr start0;
  171. xmlRegStatePtr stop;
  172. int maxRanges;
  173. int nbRanges;
  174. xmlRegRangePtr *ranges;
  175. void *data;
  176. };
  177. typedef struct _xmlRegCounter xmlRegCounter;
  178. typedef xmlRegCounter *xmlRegCounterPtr;
  179. struct _xmlRegCounter {
  180. int min;
  181. int max;
  182. };
  183. typedef struct _xmlRegTrans xmlRegTrans;
  184. typedef xmlRegTrans *xmlRegTransPtr;
  185. struct _xmlRegTrans {
  186. xmlRegAtomPtr atom;
  187. int to;
  188. int counter;
  189. int count;
  190. int nd;
  191. };
  192. struct _xmlAutomataState {
  193. xmlRegStateType type;
  194. xmlRegMarkedType mark;
  195. xmlRegMarkedType reached;
  196. int no;
  197. int maxTrans;
  198. int nbTrans;
  199. xmlRegTrans *trans;
  200. /* knowing states ponting to us can speed things up */
  201. int maxTransTo;
  202. int nbTransTo;
  203. int *transTo;
  204. };
  205. typedef struct _xmlAutomata xmlRegParserCtxt;
  206. typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
  207. #define AM_AUTOMATA_RNG 1
  208. struct _xmlAutomata {
  209. xmlChar *string;
  210. xmlChar *cur;
  211. int error;
  212. int neg;
  213. xmlRegStatePtr start;
  214. xmlRegStatePtr end;
  215. xmlRegStatePtr state;
  216. xmlRegAtomPtr atom;
  217. int maxAtoms;
  218. int nbAtoms;
  219. xmlRegAtomPtr *atoms;
  220. int maxStates;
  221. int nbStates;
  222. xmlRegStatePtr *states;
  223. int maxCounters;
  224. int nbCounters;
  225. xmlRegCounter *counters;
  226. int determinist;
  227. int negs;
  228. int flags;
  229. };
  230. struct _xmlRegexp {
  231. xmlChar *string;
  232. int nbStates;
  233. xmlRegStatePtr *states;
  234. int nbAtoms;
  235. xmlRegAtomPtr *atoms;
  236. int nbCounters;
  237. xmlRegCounter *counters;
  238. int determinist;
  239. int flags;
  240. /*
  241. * That's the compact form for determinists automatas
  242. */
  243. int nbstates;
  244. int *compact;
  245. void **transdata;
  246. int nbstrings;
  247. xmlChar **stringMap;
  248. };
  249. typedef struct _xmlRegExecRollback xmlRegExecRollback;
  250. typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
  251. struct _xmlRegExecRollback {
  252. xmlRegStatePtr state;/* the current state */
  253. int index; /* the index in the input stack */
  254. int nextbranch; /* the next transition to explore in that state */
  255. int *counts; /* save the automata state if it has some */
  256. };
  257. typedef struct _xmlRegInputToken xmlRegInputToken;
  258. typedef xmlRegInputToken *xmlRegInputTokenPtr;
  259. struct _xmlRegInputToken {
  260. xmlChar *value;
  261. void *data;
  262. };
  263. struct _xmlRegExecCtxt {
  264. int status; /* execution status != 0 indicate an error */
  265. int determinist; /* did we find an indeterministic behaviour */
  266. xmlRegexpPtr comp; /* the compiled regexp */
  267. xmlRegExecCallbacks callback;
  268. void *data;
  269. xmlRegStatePtr state;/* the current state */
  270. int transno; /* the current transition on that state */
  271. int transcount; /* the number of chars in char counted transitions */
  272. /*
  273. * A stack of rollback states
  274. */
  275. int maxRollbacks;
  276. int nbRollbacks;
  277. xmlRegExecRollback *rollbacks;
  278. /*
  279. * The state of the automata if any
  280. */
  281. int *counts;
  282. /*
  283. * The input stack
  284. */
  285. int inputStackMax;
  286. int inputStackNr;
  287. int index;
  288. int *charStack;
  289. const xmlChar *inputString; /* when operating on characters */
  290. xmlRegInputTokenPtr inputStack;/* when operating on strings */
  291. /*
  292. * error handling
  293. */
  294. int errStateNo; /* the error state number */
  295. xmlRegStatePtr errState; /* the error state */
  296. xmlChar *errString; /* the string raising the error */
  297. int *errCounts; /* counters at the error state */
  298. int nbPush;
  299. };
  300. #define REGEXP_ALL_COUNTER 0x123456
  301. #define REGEXP_ALL_LAX_COUNTER 0x123457
  302. static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
  303. static void xmlRegFreeState(xmlRegStatePtr state);
  304. static void xmlRegFreeAtom(xmlRegAtomPtr atom);
  305. static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
  306. static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
  307. static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
  308. int neg, int start, int end, const xmlChar *blockName);
  309. void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
  310. /************************************************************************
  311. * *
  312. * Regexp memory error handler *
  313. * *
  314. ************************************************************************/
  315. /**
  316. * xmlRegexpErrMemory:
  317. * @extra: extra information
  318. *
  319. * Handle an out of memory condition
  320. */
  321. static void
  322. xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
  323. {
  324. const char *regexp = NULL;
  325. if (ctxt != NULL) {
  326. regexp = (const char *) ctxt->string;
  327. ctxt->error = XML_ERR_NO_MEMORY;
  328. }
  329. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
  330. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
  331. regexp, NULL, 0, 0,
  332. "Memory allocation failed : %s\n", extra);
  333. }
  334. /**
  335. * xmlRegexpErrCompile:
  336. * @extra: extra information
  337. *
  338. * Handle a compilation failure
  339. */
  340. static void
  341. xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
  342. {
  343. const char *regexp = NULL;
  344. int idx = 0;
  345. if (ctxt != NULL) {
  346. regexp = (const char *) ctxt->string;
  347. idx = ctxt->cur - ctxt->string;
  348. ctxt->error = XML_REGEXP_COMPILE_ERROR;
  349. }
  350. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
  351. XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
  352. regexp, NULL, idx, 0,
  353. "failed to compile: %s\n", extra);
  354. }
  355. /************************************************************************
  356. * *
  357. * Allocation/Deallocation *
  358. * *
  359. ************************************************************************/
  360. static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
  361. /**
  362. * xmlRegEpxFromParse:
  363. * @ctxt: the parser context used to build it
  364. *
  365. * Allocate a new regexp and fill it with the result from the parser
  366. *
  367. * Returns the new regexp or NULL in case of error
  368. */
  369. static xmlRegexpPtr
  370. xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
  371. xmlRegexpPtr ret;
  372. ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
  373. if (ret == NULL) {
  374. xmlRegexpErrMemory(ctxt, "compiling regexp");
  375. return(NULL);
  376. }
  377. memset(ret, 0, sizeof(xmlRegexp));
  378. ret->string = ctxt->string;
  379. ret->nbStates = ctxt->nbStates;
  380. ret->states = ctxt->states;
  381. ret->nbAtoms = ctxt->nbAtoms;
  382. ret->atoms = ctxt->atoms;
  383. ret->nbCounters = ctxt->nbCounters;
  384. ret->counters = ctxt->counters;
  385. ret->determinist = ctxt->determinist;
  386. ret->flags = ctxt->flags;
  387. if (ret->determinist == -1) {
  388. xmlRegexpIsDeterminist(ret);
  389. }
  390. if ((ret->determinist != 0) &&
  391. (ret->nbCounters == 0) &&
  392. (ctxt->negs == 0) &&
  393. (ret->atoms != NULL) &&
  394. (ret->atoms[0] != NULL) &&
  395. (ret->atoms[0]->type == XML_REGEXP_STRING)) {
  396. int i, j, nbstates = 0, nbatoms = 0;
  397. int *stateRemap;
  398. int *stringRemap;
  399. int *transitions;
  400. void **transdata;
  401. xmlChar **stringMap;
  402. xmlChar *value;
  403. /*
  404. * Switch to a compact representation
  405. * 1/ counting the effective number of states left
  406. * 2/ counting the unique number of atoms, and check that
  407. * they are all of the string type
  408. * 3/ build a table state x atom for the transitions
  409. */
  410. stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
  411. if (stateRemap == NULL) {
  412. xmlRegexpErrMemory(ctxt, "compiling regexp");
  413. xmlFree(ret);
  414. return(NULL);
  415. }
  416. for (i = 0;i < ret->nbStates;i++) {
  417. if (ret->states[i] != NULL) {
  418. stateRemap[i] = nbstates;
  419. nbstates++;
  420. } else {
  421. stateRemap[i] = -1;
  422. }
  423. }
  424. #ifdef DEBUG_COMPACTION
  425. printf("Final: %d states\n", nbstates);
  426. #endif
  427. stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
  428. if (stringMap == NULL) {
  429. xmlRegexpErrMemory(ctxt, "compiling regexp");
  430. xmlFree(stateRemap);
  431. xmlFree(ret);
  432. return(NULL);
  433. }
  434. stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
  435. if (stringRemap == NULL) {
  436. xmlRegexpErrMemory(ctxt, "compiling regexp");
  437. xmlFree(stringMap);
  438. xmlFree(stateRemap);
  439. xmlFree(ret);
  440. return(NULL);
  441. }
  442. for (i = 0;i < ret->nbAtoms;i++) {
  443. if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
  444. (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
  445. value = ret->atoms[i]->valuep;
  446. for (j = 0;j < nbatoms;j++) {
  447. if (xmlStrEqual(stringMap[j], value)) {
  448. stringRemap[i] = j;
  449. break;
  450. }
  451. }
  452. if (j >= nbatoms) {
  453. stringRemap[i] = nbatoms;
  454. stringMap[nbatoms] = xmlStrdup(value);
  455. if (stringMap[nbatoms] == NULL) {
  456. for (i = 0;i < nbatoms;i++)
  457. xmlFree(stringMap[i]);
  458. xmlFree(stringRemap);
  459. xmlFree(stringMap);
  460. xmlFree(stateRemap);
  461. xmlFree(ret);
  462. return(NULL);
  463. }
  464. nbatoms++;
  465. }
  466. } else {
  467. xmlFree(stateRemap);
  468. xmlFree(stringRemap);
  469. for (i = 0;i < nbatoms;i++)
  470. xmlFree(stringMap[i]);
  471. xmlFree(stringMap);
  472. xmlFree(ret);
  473. return(NULL);
  474. }
  475. }
  476. #ifdef DEBUG_COMPACTION
  477. printf("Final: %d atoms\n", nbatoms);
  478. #endif
  479. transitions = (int *) xmlMalloc((nbstates + 1) *
  480. (nbatoms + 1) * sizeof(int));
  481. if (transitions == NULL) {
  482. xmlFree(stateRemap);
  483. xmlFree(stringRemap);
  484. xmlFree(stringMap);
  485. xmlFree(ret);
  486. return(NULL);
  487. }
  488. memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
  489. /*
  490. * Allocate the transition table. The first entry for each
  491. * state corresponds to the state type.
  492. */
  493. transdata = NULL;
  494. for (i = 0;i < ret->nbStates;i++) {
  495. int stateno, atomno, targetno, prev;
  496. xmlRegStatePtr state;
  497. xmlRegTransPtr trans;
  498. stateno = stateRemap[i];
  499. if (stateno == -1)
  500. continue;
  501. state = ret->states[i];
  502. transitions[stateno * (nbatoms + 1)] = state->type;
  503. for (j = 0;j < state->nbTrans;j++) {
  504. trans = &(state->trans[j]);
  505. if ((trans->to == -1) || (trans->atom == NULL))
  506. continue;
  507. atomno = stringRemap[trans->atom->no];
  508. if ((trans->atom->data != NULL) && (transdata == NULL)) {
  509. transdata = (void **) xmlMalloc(nbstates * nbatoms *
  510. sizeof(void *));
  511. if (transdata != NULL)
  512. memset(transdata, 0,
  513. nbstates * nbatoms * sizeof(void *));
  514. else {
  515. xmlRegexpErrMemory(ctxt, "compiling regexp");
  516. break;
  517. }
  518. }
  519. targetno = stateRemap[trans->to];
  520. /*
  521. * if the same atom can generate transitions to 2 different
  522. * states then it means the automata is not determinist and
  523. * the compact form can't be used !
  524. */
  525. prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
  526. if (prev != 0) {
  527. if (prev != targetno + 1) {
  528. ret->determinist = 0;
  529. #ifdef DEBUG_COMPACTION
  530. printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
  531. i, j, trans->atom->no, trans->to, atomno, targetno);
  532. printf(" previous to is %d\n", prev);
  533. #endif
  534. if (transdata != NULL)
  535. xmlFree(transdata);
  536. xmlFree(transitions);
  537. xmlFree(stateRemap);
  538. xmlFree(stringRemap);
  539. for (i = 0;i < nbatoms;i++)
  540. xmlFree(stringMap[i]);
  541. xmlFree(stringMap);
  542. goto not_determ;
  543. }
  544. } else {
  545. #if 0
  546. printf("State %d trans %d: atom %d to %d : %d to %d\n",
  547. i, j, trans->atom->no, trans->to, atomno, targetno);
  548. #endif
  549. transitions[stateno * (nbatoms + 1) + atomno + 1] =
  550. targetno + 1; /* to avoid 0 */
  551. if (transdata != NULL)
  552. transdata[stateno * nbatoms + atomno] =
  553. trans->atom->data;
  554. }
  555. }
  556. }
  557. ret->determinist = 1;
  558. #ifdef DEBUG_COMPACTION
  559. /*
  560. * Debug
  561. */
  562. for (i = 0;i < nbstates;i++) {
  563. for (j = 0;j < nbatoms + 1;j++) {
  564. printf("%02d ", transitions[i * (nbatoms + 1) + j]);
  565. }
  566. printf("\n");
  567. }
  568. printf("\n");
  569. #endif
  570. /*
  571. * Cleanup of the old data
  572. */
  573. if (ret->states != NULL) {
  574. for (i = 0;i < ret->nbStates;i++)
  575. xmlRegFreeState(ret->states[i]);
  576. xmlFree(ret->states);
  577. }
  578. ret->states = NULL;
  579. ret->nbStates = 0;
  580. if (ret->atoms != NULL) {
  581. for (i = 0;i < ret->nbAtoms;i++)
  582. xmlRegFreeAtom(ret->atoms[i]);
  583. xmlFree(ret->atoms);
  584. }
  585. ret->atoms = NULL;
  586. ret->nbAtoms = 0;
  587. ret->compact = transitions;
  588. ret->transdata = transdata;
  589. ret->stringMap = stringMap;
  590. ret->nbstrings = nbatoms;
  591. ret->nbstates = nbstates;
  592. xmlFree(stateRemap);
  593. xmlFree(stringRemap);
  594. }
  595. not_determ:
  596. ctxt->string = NULL;
  597. ctxt->nbStates = 0;
  598. ctxt->states = NULL;
  599. ctxt->nbAtoms = 0;
  600. ctxt->atoms = NULL;
  601. ctxt->nbCounters = 0;
  602. ctxt->counters = NULL;
  603. return(ret);
  604. }
  605. /**
  606. * xmlRegNewParserCtxt:
  607. * @string: the string to parse
  608. *
  609. * Allocate a new regexp parser context
  610. *
  611. * Returns the new context or NULL in case of error
  612. */
  613. static xmlRegParserCtxtPtr
  614. xmlRegNewParserCtxt(const xmlChar *string) {
  615. xmlRegParserCtxtPtr ret;
  616. ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
  617. if (ret == NULL)
  618. return(NULL);
  619. memset(ret, 0, sizeof(xmlRegParserCtxt));
  620. if (string != NULL)
  621. ret->string = xmlStrdup(string);
  622. ret->cur = ret->string;
  623. ret->neg = 0;
  624. ret->negs = 0;
  625. ret->error = 0;
  626. ret->determinist = -1;
  627. return(ret);
  628. }
  629. /**
  630. * xmlRegNewRange:
  631. * @ctxt: the regexp parser context
  632. * @neg: is that negative
  633. * @type: the type of range
  634. * @start: the start codepoint
  635. * @end: the end codepoint
  636. *
  637. * Allocate a new regexp range
  638. *
  639. * Returns the new range or NULL in case of error
  640. */
  641. static xmlRegRangePtr
  642. xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
  643. int neg, xmlRegAtomType type, int start, int end) {
  644. xmlRegRangePtr ret;
  645. ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
  646. if (ret == NULL) {
  647. xmlRegexpErrMemory(ctxt, "allocating range");
  648. return(NULL);
  649. }
  650. ret->neg = neg;
  651. ret->type = type;
  652. ret->start = start;
  653. ret->end = end;
  654. return(ret);
  655. }
  656. /**
  657. * xmlRegFreeRange:
  658. * @range: the regexp range
  659. *
  660. * Free a regexp range
  661. */
  662. static void
  663. xmlRegFreeRange(xmlRegRangePtr range) {
  664. if (range == NULL)
  665. return;
  666. if (range->blockName != NULL)
  667. xmlFree(range->blockName);
  668. xmlFree(range);
  669. }
  670. /**
  671. * xmlRegCopyRange:
  672. * @range: the regexp range
  673. *
  674. * Copy a regexp range
  675. *
  676. * Returns the new copy or NULL in case of error.
  677. */
  678. static xmlRegRangePtr
  679. xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
  680. xmlRegRangePtr ret;
  681. if (range == NULL)
  682. return(NULL);
  683. ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
  684. range->end);
  685. if (ret == NULL)
  686. return(NULL);
  687. if (range->blockName != NULL) {
  688. ret->blockName = xmlStrdup(range->blockName);
  689. if (ret->blockName == NULL) {
  690. xmlRegexpErrMemory(ctxt, "allocating range");
  691. xmlRegFreeRange(ret);
  692. return(NULL);
  693. }
  694. }
  695. return(ret);
  696. }
  697. /**
  698. * xmlRegNewAtom:
  699. * @ctxt: the regexp parser context
  700. * @type: the type of atom
  701. *
  702. * Allocate a new atom
  703. *
  704. * Returns the new atom or NULL in case of error
  705. */
  706. static xmlRegAtomPtr
  707. xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
  708. xmlRegAtomPtr ret;
  709. ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
  710. if (ret == NULL) {
  711. xmlRegexpErrMemory(ctxt, "allocating atom");
  712. return(NULL);
  713. }
  714. memset(ret, 0, sizeof(xmlRegAtom));
  715. ret->type = type;
  716. ret->quant = XML_REGEXP_QUANT_ONCE;
  717. ret->min = 0;
  718. ret->max = 0;
  719. return(ret);
  720. }
  721. /**
  722. * xmlRegFreeAtom:
  723. * @atom: the regexp atom
  724. *
  725. * Free a regexp atom
  726. */
  727. static void
  728. xmlRegFreeAtom(xmlRegAtomPtr atom) {
  729. int i;
  730. if (atom == NULL)
  731. return;
  732. for (i = 0;i < atom->nbRanges;i++)
  733. xmlRegFreeRange(atom->ranges[i]);
  734. if (atom->ranges != NULL)
  735. xmlFree(atom->ranges);
  736. if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
  737. xmlFree(atom->valuep);
  738. if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
  739. xmlFree(atom->valuep2);
  740. if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
  741. xmlFree(atom->valuep);
  742. xmlFree(atom);
  743. }
  744. /**
  745. * xmlRegCopyAtom:
  746. * @ctxt: the regexp parser context
  747. * @atom: the oiginal atom
  748. *
  749. * Allocate a new regexp range
  750. *
  751. * Returns the new atom or NULL in case of error
  752. */
  753. static xmlRegAtomPtr
  754. xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
  755. xmlRegAtomPtr ret;
  756. ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
  757. if (ret == NULL) {
  758. xmlRegexpErrMemory(ctxt, "copying atom");
  759. return(NULL);
  760. }
  761. memset(ret, 0, sizeof(xmlRegAtom));
  762. ret->type = atom->type;
  763. ret->quant = atom->quant;
  764. ret->min = atom->min;
  765. ret->max = atom->max;
  766. if (atom->nbRanges > 0) {
  767. int i;
  768. ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
  769. atom->nbRanges);
  770. if (ret->ranges == NULL) {
  771. xmlRegexpErrMemory(ctxt, "copying atom");
  772. goto error;
  773. }
  774. for (i = 0;i < atom->nbRanges;i++) {
  775. ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
  776. if (ret->ranges[i] == NULL)
  777. goto error;
  778. ret->nbRanges = i + 1;
  779. }
  780. }
  781. return(ret);
  782. error:
  783. xmlRegFreeAtom(ret);
  784. return(NULL);
  785. }
  786. static xmlRegStatePtr
  787. xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
  788. xmlRegStatePtr ret;
  789. ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
  790. if (ret == NULL) {
  791. xmlRegexpErrMemory(ctxt, "allocating state");
  792. return(NULL);
  793. }
  794. memset(ret, 0, sizeof(xmlRegState));
  795. ret->type = XML_REGEXP_TRANS_STATE;
  796. ret->mark = XML_REGEXP_MARK_NORMAL;
  797. return(ret);
  798. }
  799. /**
  800. * xmlRegFreeState:
  801. * @state: the regexp state
  802. *
  803. * Free a regexp state
  804. */
  805. static void
  806. xmlRegFreeState(xmlRegStatePtr state) {
  807. if (state == NULL)
  808. return;
  809. if (state->trans != NULL)
  810. xmlFree(state->trans);
  811. if (state->transTo != NULL)
  812. xmlFree(state->transTo);
  813. xmlFree(state);
  814. }
  815. /**
  816. * xmlRegFreeParserCtxt:
  817. * @ctxt: the regexp parser context
  818. *
  819. * Free a regexp parser context
  820. */
  821. static void
  822. xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
  823. int i;
  824. if (ctxt == NULL)
  825. return;
  826. if (ctxt->string != NULL)
  827. xmlFree(ctxt->string);
  828. if (ctxt->states != NULL) {
  829. for (i = 0;i < ctxt->nbStates;i++)
  830. xmlRegFreeState(ctxt->states[i]);
  831. xmlFree(ctxt->states);
  832. }
  833. if (ctxt->atoms != NULL) {
  834. for (i = 0;i < ctxt->nbAtoms;i++)
  835. xmlRegFreeAtom(ctxt->atoms[i]);
  836. xmlFree(ctxt->atoms);
  837. }
  838. if (ctxt->counters != NULL)
  839. xmlFree(ctxt->counters);
  840. xmlFree(ctxt);
  841. }
  842. /************************************************************************
  843. * *
  844. * Display of Data structures *
  845. * *
  846. ************************************************************************/
  847. static void
  848. xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
  849. switch (type) {
  850. case XML_REGEXP_EPSILON:
  851. fprintf(output, "epsilon "); break;
  852. case XML_REGEXP_CHARVAL:
  853. fprintf(output, "charval "); break;
  854. case XML_REGEXP_RANGES:
  855. fprintf(output, "ranges "); break;
  856. case XML_REGEXP_SUBREG:
  857. fprintf(output, "subexpr "); break;
  858. case XML_REGEXP_STRING:
  859. fprintf(output, "string "); break;
  860. case XML_REGEXP_ANYCHAR:
  861. fprintf(output, "anychar "); break;
  862. case XML_REGEXP_ANYSPACE:
  863. fprintf(output, "anyspace "); break;
  864. case XML_REGEXP_NOTSPACE:
  865. fprintf(output, "notspace "); break;
  866. case XML_REGEXP_INITNAME:
  867. fprintf(output, "initname "); break;
  868. case XML_REGEXP_NOTINITNAME:
  869. fprintf(output, "notinitname "); break;
  870. case XML_REGEXP_NAMECHAR:
  871. fprintf(output, "namechar "); break;
  872. case XML_REGEXP_NOTNAMECHAR:
  873. fprintf(output, "notnamechar "); break;
  874. case XML_REGEXP_DECIMAL:
  875. fprintf(output, "decimal "); break;
  876. case XML_REGEXP_NOTDECIMAL:
  877. fprintf(output, "notdecimal "); break;
  878. case XML_REGEXP_REALCHAR:
  879. fprintf(output, "realchar "); break;
  880. case XML_REGEXP_NOTREALCHAR:
  881. fprintf(output, "notrealchar "); break;
  882. case XML_REGEXP_LETTER:
  883. fprintf(output, "LETTER "); break;
  884. case XML_REGEXP_LETTER_UPPERCASE:
  885. fprintf(output, "LETTER_UPPERCASE "); break;
  886. case XML_REGEXP_LETTER_LOWERCASE:
  887. fprintf(output, "LETTER_LOWERCASE "); break;
  888. case XML_REGEXP_LETTER_TITLECASE:
  889. fprintf(output, "LETTER_TITLECASE "); break;
  890. case XML_REGEXP_LETTER_MODIFIER:
  891. fprintf(output, "LETTER_MODIFIER "); break;
  892. case XML_REGEXP_LETTER_OTHERS:
  893. fprintf(output, "LETTER_OTHERS "); break;
  894. case XML_REGEXP_MARK:
  895. fprintf(output, "MARK "); break;
  896. case XML_REGEXP_MARK_NONSPACING:
  897. fprintf(output, "MARK_NONSPACING "); break;
  898. case XML_REGEXP_MARK_SPACECOMBINING:
  899. fprintf(output, "MARK_SPACECOMBINING "); break;
  900. case XML_REGEXP_MARK_ENCLOSING:
  901. fprintf(output, "MARK_ENCLOSING "); break;
  902. case XML_REGEXP_NUMBER:
  903. fprintf(output, "NUMBER "); break;
  904. case XML_REGEXP_NUMBER_DECIMAL:
  905. fprintf(output, "NUMBER_DECIMAL "); break;
  906. case XML_REGEXP_NUMBER_LETTER:
  907. fprintf(output, "NUMBER_LETTER "); break;
  908. case XML_REGEXP_NUMBER_OTHERS:
  909. fprintf(output, "NUMBER_OTHERS "); break;
  910. case XML_REGEXP_PUNCT:
  911. fprintf(output, "PUNCT "); break;
  912. case XML_REGEXP_PUNCT_CONNECTOR:
  913. fprintf(output, "PUNCT_CONNECTOR "); break;
  914. case XML_REGEXP_PUNCT_DASH:
  915. fprintf(output, "PUNCT_DASH "); break;
  916. case XML_REGEXP_PUNCT_OPEN:
  917. fprintf(output, "PUNCT_OPEN "); break;
  918. case XML_REGEXP_PUNCT_CLOSE:
  919. fprintf(output, "PUNCT_CLOSE "); break;
  920. case XML_REGEXP_PUNCT_INITQUOTE:
  921. fprintf(output, "PUNCT_INITQUOTE "); break;
  922. case XML_REGEXP_PUNCT_FINQUOTE:
  923. fprintf(output, "PUNCT_FINQUOTE "); break;
  924. case XML_REGEXP_PUNCT_OTHERS:
  925. fprintf(output, "PUNCT_OTHERS "); break;
  926. case XML_REGEXP_SEPAR:
  927. fprintf(output, "SEPAR "); break;
  928. case XML_REGEXP_SEPAR_SPACE:
  929. fprintf(output, "SEPAR_SPACE "); break;
  930. case XML_REGEXP_SEPAR_LINE:
  931. fprintf(output, "SEPAR_LINE "); break;
  932. case XML_REGEXP_SEPAR_PARA:
  933. fprintf(output, "SEPAR_PARA "); break;
  934. case XML_REGEXP_SYMBOL:
  935. fprintf(output, "SYMBOL "); break;
  936. case XML_REGEXP_SYMBOL_MATH:
  937. fprintf(output, "SYMBOL_MATH "); break;
  938. case XML_REGEXP_SYMBOL_CURRENCY:
  939. fprintf(output, "SYMBOL_CURRENCY "); break;
  940. case XML_REGEXP_SYMBOL_MODIFIER:
  941. fprintf(output, "SYMBOL_MODIFIER "); break;
  942. case XML_REGEXP_SYMBOL_OTHERS:
  943. fprintf(output, "SYMBOL_OTHERS "); break;
  944. case XML_REGEXP_OTHER:
  945. fprintf(output, "OTHER "); break;
  946. case XML_REGEXP_OTHER_CONTROL:
  947. fprintf(output, "OTHER_CONTROL "); break;
  948. case XML_REGEXP_OTHER_FORMAT:
  949. fprintf(output, "OTHER_FORMAT "); break;
  950. case XML_REGEXP_OTHER_PRIVATE:
  951. fprintf(output, "OTHER_PRIVATE "); break;
  952. case XML_REGEXP_OTHER_NA:
  953. fprintf(output, "OTHER_NA "); break;
  954. case XML_REGEXP_BLOCK_NAME:
  955. fprintf(output, "BLOCK "); break;
  956. }
  957. }
  958. static void
  959. xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
  960. switch (type) {
  961. case XML_REGEXP_QUANT_EPSILON:
  962. fprintf(output, "epsilon "); break;
  963. case XML_REGEXP_QUANT_ONCE:
  964. fprintf(output, "once "); break;
  965. case XML_REGEXP_QUANT_OPT:
  966. fprintf(output, "? "); break;
  967. case XML_REGEXP_QUANT_MULT:
  968. fprintf(output, "* "); break;
  969. case XML_REGEXP_QUANT_PLUS:
  970. fprintf(output, "+ "); break;
  971. case XML_REGEXP_QUANT_RANGE:
  972. fprintf(output, "range "); break;
  973. case XML_REGEXP_QUANT_ONCEONLY:
  974. fprintf(output, "onceonly "); break;
  975. case XML_REGEXP_QUANT_ALL:
  976. fprintf(output, "all "); break;
  977. }
  978. }
  979. static void
  980. xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
  981. fprintf(output, " range: ");
  982. if (range->neg)
  983. fprintf(output, "negative ");
  984. xmlRegPrintAtomType(output, range->type);
  985. fprintf(output, "%c - %c\n", range->start, range->end);
  986. }
  987. static void
  988. xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
  989. fprintf(output, " atom: ");
  990. if (atom == NULL) {
  991. fprintf(output, "NULL\n");
  992. return;
  993. }
  994. if (atom->neg)
  995. fprintf(output, "not ");
  996. xmlRegPrintAtomType(output, atom->type);
  997. xmlRegPrintQuantType(output, atom->quant);
  998. if (atom->quant == XML_REGEXP_QUANT_RANGE)
  999. fprintf(output, "%d-%d ", atom->min, atom->max);
  1000. if (atom->type == XML_REGEXP_STRING)
  1001. fprintf(output, "'%s' ", (char *) atom->valuep);
  1002. if (atom->type == XML_REGEXP_CHARVAL)
  1003. fprintf(output, "char %c\n", atom->codepoint);
  1004. else if (atom->type == XML_REGEXP_RANGES) {
  1005. int i;
  1006. fprintf(output, "%d entries\n", atom->nbRanges);
  1007. for (i = 0; i < atom->nbRanges;i++)
  1008. xmlRegPrintRange(output, atom->ranges[i]);
  1009. } else if (atom->type == XML_REGEXP_SUBREG) {
  1010. fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
  1011. } else {
  1012. fprintf(output, "\n");
  1013. }
  1014. }
  1015. static void
  1016. xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
  1017. fprintf(output, " trans: ");
  1018. if (trans == NULL) {
  1019. fprintf(output, "NULL\n");
  1020. return;
  1021. }
  1022. if (trans->to < 0) {
  1023. fprintf(output, "removed\n");
  1024. return;
  1025. }
  1026. if (trans->nd != 0) {
  1027. if (trans->nd == 2)
  1028. fprintf(output, "last not determinist, ");
  1029. else
  1030. fprintf(output, "not determinist, ");
  1031. }
  1032. if (trans->counter >= 0) {
  1033. fprintf(output, "counted %d, ", trans->counter);
  1034. }
  1035. if (trans->count == REGEXP_ALL_COUNTER) {
  1036. fprintf(output, "all transition, ");
  1037. } else if (trans->count >= 0) {
  1038. fprintf(output, "count based %d, ", trans->count);
  1039. }
  1040. if (trans->atom == NULL) {
  1041. fprintf(output, "epsilon to %d\n", trans->to);
  1042. return;
  1043. }
  1044. if (trans->atom->type == XML_REGEXP_CHARVAL)
  1045. fprintf(output, "char %c ", trans->atom->codepoint);
  1046. fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
  1047. }
  1048. static void
  1049. xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
  1050. int i;
  1051. fprintf(output, " state: ");
  1052. if (state == NULL) {
  1053. fprintf(output, "NULL\n");
  1054. return;
  1055. }
  1056. if (state->type == XML_REGEXP_START_STATE)
  1057. fprintf(output, "START ");
  1058. if (state->type == XML_REGEXP_FINAL_STATE)
  1059. fprintf(output, "FINAL ");
  1060. fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
  1061. for (i = 0;i < state->nbTrans; i++) {
  1062. xmlRegPrintTrans(output, &(state->trans[i]));
  1063. }
  1064. }
  1065. #ifdef DEBUG_REGEXP_GRAPH
  1066. static void
  1067. xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
  1068. int i;
  1069. fprintf(output, " ctxt: ");
  1070. if (ctxt == NULL) {
  1071. fprintf(output, "NULL\n");
  1072. return;
  1073. }
  1074. fprintf(output, "'%s' ", ctxt->string);
  1075. if (ctxt->error)
  1076. fprintf(output, "error ");
  1077. if (ctxt->neg)
  1078. fprintf(output, "neg ");
  1079. fprintf(output, "\n");
  1080. fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
  1081. for (i = 0;i < ctxt->nbAtoms; i++) {
  1082. fprintf(output, " %02d ", i);
  1083. xmlRegPrintAtom(output, ctxt->atoms[i]);
  1084. }
  1085. if (ctxt->atom != NULL) {
  1086. fprintf(output, "current atom:\n");
  1087. xmlRegPrintAtom(output, ctxt->atom);
  1088. }
  1089. fprintf(output, "%d states:", ctxt->nbStates);
  1090. if (ctxt->start != NULL)
  1091. fprintf(output, " start: %d", ctxt->start->no);
  1092. if (ctxt->end != NULL)
  1093. fprintf(output, " end: %d", ctxt->end->no);
  1094. fprintf(output, "\n");
  1095. for (i = 0;i < ctxt->nbStates; i++) {
  1096. xmlRegPrintState(output, ctxt->states[i]);
  1097. }
  1098. fprintf(output, "%d counters:\n", ctxt->nbCounters);
  1099. for (i = 0;i < ctxt->nbCounters; i++) {
  1100. fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
  1101. ctxt->counters[i].max);
  1102. }
  1103. }
  1104. #endif
  1105. /************************************************************************
  1106. * *
  1107. * Finite Automata structures manipulations *
  1108. * *
  1109. ************************************************************************/
  1110. static void
  1111. xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
  1112. int neg, xmlRegAtomType type, int start, int end,
  1113. xmlChar *blockName) {
  1114. xmlRegRangePtr range;
  1115. if (atom == NULL) {
  1116. ERROR("add range: atom is NULL");
  1117. return;
  1118. }
  1119. if (atom->type != XML_REGEXP_RANGES) {
  1120. ERROR("add range: atom is not ranges");
  1121. return;
  1122. }
  1123. if (atom->maxRanges == 0) {
  1124. atom->maxRanges = 4;
  1125. atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
  1126. sizeof(xmlRegRangePtr));
  1127. if (atom->ranges == NULL) {
  1128. xmlRegexpErrMemory(ctxt, "adding ranges");
  1129. atom->maxRanges = 0;
  1130. return;
  1131. }
  1132. } else if (atom->nbRanges >= atom->maxRanges) {
  1133. xmlRegRangePtr *tmp;
  1134. atom->maxRanges *= 2;
  1135. tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
  1136. sizeof(xmlRegRangePtr));
  1137. if (tmp == NULL) {
  1138. xmlRegexpErrMemory(ctxt, "adding ranges");
  1139. atom->maxRanges /= 2;
  1140. return;
  1141. }
  1142. atom->ranges = tmp;
  1143. }
  1144. range = xmlRegNewRange(ctxt, neg, type, start, end);
  1145. if (range == NULL)
  1146. return;
  1147. range->blockName = blockName;
  1148. atom->ranges[atom->nbRanges++] = range;
  1149. }
  1150. static int
  1151. xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
  1152. if (ctxt->maxCounters == 0) {
  1153. ctxt->maxCounters = 4;
  1154. ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
  1155. sizeof(xmlRegCounter));
  1156. if (ctxt->counters == NULL) {
  1157. xmlRegexpErrMemory(ctxt, "allocating counter");
  1158. ctxt->maxCounters = 0;
  1159. return(-1);
  1160. }
  1161. } else if (ctxt->nbCounters >= ctxt->maxCounters) {
  1162. xmlRegCounter *tmp;
  1163. ctxt->maxCounters *= 2;
  1164. tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
  1165. sizeof(xmlRegCounter));
  1166. if (tmp == NULL) {
  1167. xmlRegexpErrMemory(ctxt, "allocating counter");
  1168. ctxt->maxCounters /= 2;
  1169. return(-1);
  1170. }
  1171. ctxt->counters = tmp;
  1172. }
  1173. ctxt->counters[ctxt->nbCounters].min = -1;
  1174. ctxt->counters[ctxt->nbCounters].max = -1;
  1175. return(ctxt->nbCounters++);
  1176. }
  1177. static int
  1178. xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
  1179. if (atom == NULL) {
  1180. ERROR("atom push: atom is NULL");
  1181. return(-1);
  1182. }
  1183. if (ctxt->maxAtoms == 0) {
  1184. ctxt->maxAtoms = 4;
  1185. ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
  1186. sizeof(xmlRegAtomPtr));
  1187. if (ctxt->atoms == NULL) {
  1188. xmlRegexpErrMemory(ctxt, "pushing atom");
  1189. ctxt->maxAtoms = 0;
  1190. return(-1);
  1191. }
  1192. } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
  1193. xmlRegAtomPtr *tmp;
  1194. ctxt->maxAtoms *= 2;
  1195. tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
  1196. sizeof(xmlRegAtomPtr));
  1197. if (tmp == NULL) {
  1198. xmlRegexpErrMemory(ctxt, "allocating counter");
  1199. ctxt->maxAtoms /= 2;
  1200. return(-1);
  1201. }
  1202. ctxt->atoms = tmp;
  1203. }
  1204. atom->no = ctxt->nbAtoms;
  1205. ctxt->atoms[ctxt->nbAtoms++] = atom;
  1206. return(0);
  1207. }
  1208. static void
  1209. xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
  1210. int from) {
  1211. if (target->maxTransTo == 0) {
  1212. target->maxTransTo = 8;
  1213. target->transTo = (int *) xmlMalloc(target->maxTransTo *
  1214. sizeof(int));
  1215. if (target->transTo == NULL) {
  1216. xmlRegexpErrMemory(ctxt, "adding transition");
  1217. target->maxTransTo = 0;
  1218. return;
  1219. }
  1220. } else if (target->nbTransTo >= target->maxTransTo) {
  1221. int *tmp;
  1222. target->maxTransTo *= 2;
  1223. tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
  1224. sizeof(int));
  1225. if (tmp == NULL) {
  1226. xmlRegexpErrMemory(ctxt, "adding transition");
  1227. target->maxTransTo /= 2;
  1228. return;
  1229. }
  1230. target->transTo = tmp;
  1231. }
  1232. target->transTo[target->nbTransTo] = from;
  1233. target->nbTransTo++;
  1234. }
  1235. static void
  1236. xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
  1237. xmlRegAtomPtr atom, xmlRegStatePtr target,
  1238. int counter, int count) {
  1239. int nrtrans;
  1240. if (state == NULL) {
  1241. ERROR("add state: state is NULL");
  1242. return;
  1243. }
  1244. if (target == NULL) {
  1245. ERROR("add state: target is NULL");
  1246. return;
  1247. }
  1248. /*
  1249. * Other routines follow the philosophy 'When in doubt, add a transition'
  1250. * so we check here whether such a transition is already present and, if
  1251. * so, silently ignore this request.
  1252. */
  1253. for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
  1254. xmlRegTransPtr trans = &(state->trans[nrtrans]);
  1255. if ((trans->atom == atom) &&
  1256. (trans->to == target->no) &&
  1257. (trans->counter == counter) &&
  1258. (trans->count == count)) {
  1259. #ifdef DEBUG_REGEXP_GRAPH
  1260. printf("Ignoring duplicate transition from %d to %d\n",
  1261. state->no, target->no);
  1262. #endif
  1263. return;
  1264. }
  1265. }
  1266. if (state->maxTrans == 0) {
  1267. state->maxTrans = 8;
  1268. state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
  1269. sizeof(xmlRegTrans));
  1270. if (state->trans == NULL) {
  1271. xmlRegexpErrMemory(ctxt, "adding transition");
  1272. state->maxTrans = 0;
  1273. return;
  1274. }
  1275. } else if (state->nbTrans >= state->maxTrans) {
  1276. xmlRegTrans *tmp;
  1277. state->maxTrans *= 2;
  1278. tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
  1279. sizeof(xmlRegTrans));
  1280. if (tmp == NULL) {
  1281. xmlRegexpErrMemory(ctxt, "adding transition");
  1282. state->maxTrans /= 2;
  1283. return;
  1284. }
  1285. state->trans = tmp;
  1286. }
  1287. #ifdef DEBUG_REGEXP_GRAPH
  1288. printf("Add trans from %d to %d ", state->no, target->no);
  1289. if (count == REGEXP_ALL_COUNTER)
  1290. printf("all transition\n");
  1291. else if (count >= 0)
  1292. printf("count based %d\n", count);
  1293. else if (counter >= 0)
  1294. printf("counted %d\n", counter);
  1295. else if (atom == NULL)
  1296. printf("epsilon transition\n");
  1297. else if (atom != NULL)
  1298. xmlRegPrintAtom(stdout, atom);
  1299. #endif
  1300. state->trans[state->nbTrans].atom = atom;
  1301. state->trans[state->nbTrans].to = target->no;
  1302. state->trans[state->nbTrans].counter = counter;
  1303. state->trans[state->nbTrans].count = count;
  1304. state->trans[state->nbTrans].nd = 0;
  1305. state->nbTrans++;
  1306. xmlRegStateAddTransTo(ctxt, target, state->no);
  1307. }
  1308. static int
  1309. xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
  1310. if (state == NULL) return(-1);
  1311. if (ctxt->maxStates == 0) {
  1312. ctxt->maxStates = 4;
  1313. ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
  1314. sizeof(xmlRegStatePtr));
  1315. if (ctxt->states == NULL) {
  1316. xmlRegexpErrMemory(ctxt, "adding state");
  1317. ctxt->maxStates = 0;
  1318. return(-1);
  1319. }
  1320. } else if (ctxt->nbStates >= ctxt->maxStates) {
  1321. xmlRegStatePtr *tmp;
  1322. ctxt->maxStates *= 2;
  1323. tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
  1324. sizeof(xmlRegStatePtr));
  1325. if (tmp == NULL) {
  1326. xmlRegexpErrMemory(ctxt, "adding state");
  1327. ctxt->maxStates /= 2;
  1328. return(-1);
  1329. }
  1330. ctxt->states = tmp;
  1331. }
  1332. state->no = ctxt->nbStates;
  1333. ctxt->states[ctxt->nbStates++] = state;
  1334. return(0);
  1335. }
  1336. /**
  1337. * xmlFAGenerateAllTransition:
  1338. * @ctxt: a regexp parser context
  1339. * @from: the from state
  1340. * @to: the target state or NULL for building a new one
  1341. * @lax:
  1342. *
  1343. */
  1344. static void
  1345. xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
  1346. xmlRegStatePtr from, xmlRegStatePtr to,
  1347. int lax) {
  1348. if (to == NULL) {
  1349. to = xmlRegNewState(ctxt);
  1350. xmlRegStatePush(ctxt, to);
  1351. ctxt->state = to;
  1352. }
  1353. if (lax)
  1354. xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
  1355. else
  1356. xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
  1357. }
  1358. /**
  1359. * xmlFAGenerateEpsilonTransition:
  1360. * @ctxt: a regexp parser context
  1361. * @from: the from state
  1362. * @to: the target state or NULL for building a new one
  1363. *
  1364. */
  1365. static void
  1366. xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
  1367. xmlRegStatePtr from, xmlRegStatePtr to) {
  1368. if (to == NULL) {
  1369. to = xmlRegNewState(ctxt);
  1370. xmlRegStatePush(ctxt, to);
  1371. ctxt->state = to;
  1372. }
  1373. xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
  1374. }
  1375. /**
  1376. * xmlFAGenerateCountedEpsilonTransition:
  1377. * @ctxt: a regexp parser context
  1378. * @from: the from state
  1379. * @to: the target state or NULL for building a new one
  1380. * counter: the counter for that transition
  1381. *
  1382. */
  1383. static void
  1384. xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
  1385. xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
  1386. if (to == NULL) {
  1387. to = xmlRegNewState(ctxt);
  1388. xmlRegStatePush(ctxt, to);
  1389. ctxt->state = to;
  1390. }
  1391. xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
  1392. }
  1393. /**
  1394. * xmlFAGenerateCountedTransition:
  1395. * @ctxt: a regexp parser context
  1396. * @from: the from state
  1397. * @to: the target state or NULL for building a new one
  1398. * counter: the counter for that transition
  1399. *
  1400. */
  1401. static void
  1402. xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
  1403. xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
  1404. if (to == NULL) {
  1405. to = xmlRegNewState(ctxt);
  1406. xmlRegStatePush(ctxt, to);
  1407. ctxt->state = to;
  1408. }
  1409. xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
  1410. }
  1411. /**
  1412. * xmlFAGenerateTransitions:
  1413. * @ctxt: a regexp parser context
  1414. * @from: the from state
  1415. * @to: the target state or NULL for building a new one
  1416. * @atom: the atom generating the transition
  1417. *
  1418. * Returns 0 if success and -1 in case of error.
  1419. */
  1420. static int
  1421. xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
  1422. xmlRegStatePtr to, xmlRegAtomPtr atom) {
  1423. xmlRegStatePtr end;
  1424. if (atom == NULL) {
  1425. ERROR("genrate transition: atom == NULL");
  1426. return(-1);
  1427. }
  1428. if (atom->type == XML_REGEXP_SUBREG) {
  1429. /*
  1430. * this is a subexpression handling one should not need to
  1431. * create a new node except for XML_REGEXP_QUANT_RANGE.
  1432. */
  1433. if (xmlRegAtomPush(ctxt, atom) < 0) {
  1434. return(-1);
  1435. }
  1436. if ((to != NULL) && (atom->stop != to) &&
  1437. (atom->quant != XML_REGEXP_QUANT_RANGE)) {
  1438. /*
  1439. * Generate an epsilon transition to link to the target
  1440. */
  1441. xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
  1442. #ifdef DV
  1443. } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
  1444. (atom->quant != XML_REGEXP_QUANT_ONCE)) {
  1445. to = xmlRegNewState(ctxt);
  1446. xmlRegStatePush(ctxt, to);
  1447. ctxt->state = to;
  1448. xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
  1449. #endif
  1450. }
  1451. switch (atom->quant) {
  1452. case XML_REGEXP_QUANT_OPT:
  1453. atom->quant = XML_REGEXP_QUANT_ONCE;
  1454. /*
  1455. * transition done to the state after end of atom.
  1456. * 1. set transition from atom start to new state
  1457. * 2. set transition from atom end to this state.
  1458. */
  1459. if (to == NULL) {
  1460. xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
  1461. xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
  1462. ctxt->state);
  1463. } else {
  1464. xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
  1465. }
  1466. break;
  1467. case XML_REGEXP_QUANT_MULT:
  1468. atom->quant = XML_REGEXP_QUANT_ONCE;
  1469. xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
  1470. xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
  1471. break;
  1472. case XML_REGEXP_QUANT_PLUS:
  1473. atom->quant = XML_REGEXP_QUANT_ONCE;
  1474. xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
  1475. break;
  1476. case XML_REGEXP_QUANT_RANGE: {
  1477. int counter;
  1478. xmlRegStatePtr inter, newstate;
  1479. /*
  1480. * create the final state now if needed
  1481. */
  1482. if (to != NULL) {
  1483. newstate = to;
  1484. } else {
  1485. newstate = xmlRegNewState(ctxt);
  1486. xmlRegStatePush(ctxt, newstate);
  1487. }
  1488. /*
  1489. * The principle here is to use counted transition
  1490. * to avoid explosion in the number of states in the
  1491. * graph. This is clearly more complex but should not
  1492. * be exploitable at runtime.
  1493. */
  1494. if ((atom->min == 0) && (atom->start0 == NULL)) {
  1495. xmlRegAtomPtr copy;
  1496. /*
  1497. * duplicate a transition based on atom to count next
  1498. * occurences after 1. We cannot loop to atom->start
  1499. * directly because we need an epsilon transition to
  1500. * newstate.
  1501. */
  1502. /* ???? For some reason it seems we never reach that
  1503. case, I suppose this got optimized out before when
  1504. building the automata */
  1505. copy = xmlRegCopyAtom(ctxt, atom);
  1506. if (copy == NULL)
  1507. return(-1);
  1508. copy->quant = XML_REGEXP_QUANT_ONCE;
  1509. copy->min = 0;
  1510. copy->max = 0;
  1511. if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
  1512. < 0)
  1513. return(-1);
  1514. inter = ctxt->state;
  1515. counter = xmlRegGetCounter(ctxt);
  1516. ctxt->counters[counter].min = atom->min - 1;
  1517. ctxt->counters[counter].max = atom->max - 1;
  1518. /* count the number of times we see it again */
  1519. xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
  1520. atom->stop, counter);
  1521. /* allow a way out based on the count */
  1522. xmlFAGenerateCountedTransition(ctxt, inter,
  1523. newstate, counter);
  1524. /* and also allow a direct exit for 0 */
  1525. xmlFAGenerateEpsilonTransition(ctxt, atom->start,
  1526. newstate);
  1527. } else {
  1528. /*
  1529. * either we need the atom at least once or there
  1530. * is an atom->start0 allowing to easilly plug the
  1531. * epsilon transition.
  1532. */
  1533. counter = xmlRegGetCounter(ctxt);
  1534. ctxt->counters[counter].min = atom->min - 1;
  1535. ctxt->counters[counter].max = atom->max - 1;
  1536. /* count the number of times we see it again */
  1537. xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
  1538. atom->start, counter);
  1539. /* allow a way out based on the count */
  1540. xmlFAGenerateCountedTransition(ctxt, atom->stop,
  1541. newstate, counter);
  1542. /* and if needed allow a direct exit for 0 */
  1543. if (atom->min == 0)
  1544. xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
  1545. newstate);
  1546. }
  1547. atom->min = 0;
  1548. atom->max = 0;
  1549. atom->quant = XML_REGEXP_QUANT_ONCE;
  1550. ctxt->state = newstate;
  1551. }
  1552. default:
  1553. break;
  1554. }
  1555. return(0);
  1556. }
  1557. if ((atom->min == 0) && (atom->max == 0) &&
  1558. (atom->quant == XML_REGEXP_QUANT_RANGE)) {
  1559. /*
  1560. * we can discard the atom and generate an epsilon transition instead
  1561. */
  1562. if (to == NULL) {
  1563. to = xmlRegNewState(ctxt);
  1564. if (to != NULL)
  1565. xmlRegStatePush(ctxt, to);
  1566. else {
  1567. return(-1);
  1568. }
  1569. }
  1570. xmlFAGenerateEpsilonTransition(ctxt, from, to);
  1571. ctxt->state = to;
  1572. xmlRegFreeAtom(atom);
  1573. return(0);
  1574. }
  1575. if (to == NULL) {
  1576. to = xmlRegNewState(ctxt);
  1577. if (to != NULL)
  1578. xmlRegStatePush(ctxt, to);
  1579. else {
  1580. return(-1);
  1581. }
  1582. }
  1583. end = to;
  1584. if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
  1585. (atom->quant == XML_REGEXP_QUANT_PLUS)) {
  1586. /*
  1587. * Do not pollute the target state by adding transitions from
  1588. * it as it is likely to be the shared target of multiple branches.
  1589. * So isolate with an epsilon transition.
  1590. */
  1591. xmlRegStatePtr tmp;
  1592. tmp = xmlRegNewState(ctxt);
  1593. if (tmp != NULL)
  1594. xmlRegStatePush(ctxt, tmp);
  1595. else {
  1596. return(-1);
  1597. }
  1598. xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
  1599. to = tmp;
  1600. }
  1601. if (xmlRegAtomPush(ctxt, atom) < 0) {
  1602. return(-1);
  1603. }
  1604. xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
  1605. ctxt->state = end;
  1606. switch (atom->quant) {
  1607. case XML_REGEXP_QUANT_OPT:
  1608. atom->quant = XML_REGEXP_QUANT_ONCE;
  1609. xmlFAGenerateEpsilonTransition(ctxt, from, to);
  1610. break;
  1611. case XML_REGEXP_QUANT_MULT:
  1612. atom->quant = XML_REGEXP_QUANT_ONCE;
  1613. xmlFAGenerateEpsilonTransition(ctxt, from, to);
  1614. xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
  1615. break;
  1616. case XML_REGEXP_QUANT_PLUS:
  1617. atom->quant = XML_REGEXP_QUANT_ONCE;
  1618. xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
  1619. break;
  1620. case XML_REGEXP_QUANT_RANGE:
  1621. #if DV_test
  1622. if (atom->min == 0) {
  1623. xmlFAGenerateEpsilonTransition(ctxt, from, to);
  1624. }
  1625. #endif
  1626. break;
  1627. default:
  1628. break;
  1629. }
  1630. return(0);
  1631. }
  1632. /**
  1633. * xmlFAReduceEpsilonTransitions:
  1634. * @ctxt: a regexp parser context
  1635. * @fromnr: the from state
  1636. * @tonr: the to state
  1637. * @counter: should that transition be associated to a counted
  1638. *
  1639. */
  1640. static void
  1641. xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
  1642. int tonr, int counter) {
  1643. int transnr;
  1644. xmlRegStatePtr from;
  1645. xmlRegStatePtr to;
  1646. #ifdef DEBUG_REGEXP_GRAPH
  1647. printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
  1648. #endif
  1649. from = ctxt->states[fromnr];
  1650. if (from == NULL)
  1651. return;
  1652. to = ctxt->states[tonr];
  1653. if (to == NULL)
  1654. return;
  1655. if ((to->mark == XML_REGEXP_MARK_START) ||
  1656. (to->mark == XML_REGEXP_MARK_VISITED))
  1657. return;
  1658. to->mark = XML_REGEXP_MARK_VISITED;
  1659. if (to->type == XML_REGEXP_FINAL_STATE) {
  1660. #ifdef DEBUG_REGEXP_GRAPH
  1661. printf("State %d is final, so %d becomes final\n", tonr, fromnr);
  1662. #endif
  1663. from->type = XML_REGEXP_FINAL_STATE;
  1664. }
  1665. for (transnr = 0;transnr < to->nbTrans;transnr++) {
  1666. if (to->trans[transnr].to < 0)
  1667. continue;
  1668. if (to->trans[transnr].atom == NULL) {
  1669. /*
  1670. * Don't remove counted transitions
  1671. * Don't loop either
  1672. */
  1673. if (to->trans[transnr].to != fromnr) {
  1674. if (to->trans[transnr].count >= 0) {
  1675. int newto = to->trans[transnr].to;
  1676. xmlRegStateAddTrans(ctxt, from, NULL,
  1677. ctxt->states[newto],
  1678. -1, to->trans[transnr].count);
  1679. } else {
  1680. #ifdef DEBUG_REGEXP_GRAPH
  1681. printf("Found epsilon trans %d from %d to %d\n",
  1682. transnr, tonr, to->trans[transnr].to);
  1683. #endif
  1684. if (to->trans[transnr].counter >= 0) {
  1685. xmlFAReduceEpsilonTransitions(ctxt, fromnr,
  1686. to->trans[transnr].to,
  1687. to->trans[transnr].counter);
  1688. } else {
  1689. xmlFAReduceEpsilonTransitions(ctxt, fromnr,
  1690. to->trans[transnr].to,
  1691. counter);
  1692. }
  1693. }
  1694. }
  1695. } else {
  1696. int newto = to->trans[transnr].to;
  1697. if (to->trans[transnr].counter >= 0) {
  1698. xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
  1699. ctxt->states[newto],
  1700. to->trans[transnr].counter, -1);
  1701. } else {
  1702. xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
  1703. ctxt->states[newto], counter, -1);
  1704. }
  1705. }
  1706. }
  1707. to->mark = XML_REGEXP_MARK_NORMAL;
  1708. }
  1709. /**
  1710. * xmlFAEliminateSimpleEpsilonTransitions:
  1711. * @ctxt: a regexp parser context
  1712. *
  1713. * Eliminating general epsilon transitions can get costly in the general
  1714. * algorithm due to the large amount of generated new transitions and
  1715. * associated comparisons. However for simple epsilon transition used just
  1716. * to separate building blocks when generating the automata this can be
  1717. * reduced to state elimination:
  1718. * - if there exists an epsilon from X to Y
  1719. * - if there is no other transition from X
  1720. * then X and Y are semantically equivalent and X can be eliminated
  1721. * If X is the start state then make Y the start state, else replace the
  1722. * target of all transitions to X by transitions to Y.
  1723. */
  1724. static void
  1725. xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
  1726. int statenr, i, j, newto;
  1727. xmlRegStatePtr state, tmp;
  1728. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1729. state = ctxt->states[statenr];
  1730. if (state == NULL)
  1731. continue;
  1732. if (state->nbTrans != 1)
  1733. continue;
  1734. if (state->type == XML_REGEXP_UNREACH_STATE)
  1735. continue;
  1736. /* is the only transition out a basic transition */
  1737. if ((state->trans[0].atom == NULL) &&
  1738. (state->trans[0].to >= 0) &&
  1739. (state->trans[0].to != statenr) &&
  1740. (state->trans[0].counter < 0) &&
  1741. (state->trans[0].count < 0)) {
  1742. newto = state->trans[0].to;
  1743. if (state->type == XML_REGEXP_START_STATE) {
  1744. #ifdef DEBUG_REGEXP_GRAPH
  1745. printf("Found simple epsilon trans from start %d to %d\n",
  1746. statenr, newto);
  1747. #endif
  1748. } else {
  1749. #ifdef DEBUG_REGEXP_GRAPH
  1750. printf("Found simple epsilon trans from %d to %d\n",
  1751. statenr, newto);
  1752. #endif
  1753. for (i = 0;i < state->nbTransTo;i++) {
  1754. tmp = ctxt->states[state->transTo[i]];
  1755. for (j = 0;j < tmp->nbTrans;j++) {
  1756. if (tmp->trans[j].to == statenr) {
  1757. #ifdef DEBUG_REGEXP_GRAPH
  1758. printf("Changed transition %d on %d to go to %d\n",
  1759. j, tmp->no, newto);
  1760. #endif
  1761. tmp->trans[j].to = -1;
  1762. xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
  1763. ctxt->states[newto],
  1764. tmp->trans[j].counter,
  1765. tmp->trans[j].count);
  1766. }
  1767. }
  1768. }
  1769. if (state->type == XML_REGEXP_FINAL_STATE)
  1770. ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
  1771. /* eliminate the transition completely */
  1772. state->nbTrans = 0;
  1773. state->type = XML_REGEXP_UNREACH_STATE;
  1774. }
  1775. }
  1776. }
  1777. }
  1778. /**
  1779. * xmlFAEliminateEpsilonTransitions:
  1780. * @ctxt: a regexp parser context
  1781. *
  1782. */
  1783. static void
  1784. xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
  1785. int statenr, transnr;
  1786. xmlRegStatePtr state;
  1787. int has_epsilon;
  1788. if (ctxt->states == NULL) return;
  1789. /*
  1790. * Eliminate simple epsilon transition and the associated unreachable
  1791. * states.
  1792. */
  1793. xmlFAEliminateSimpleEpsilonTransitions(ctxt);
  1794. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1795. state = ctxt->states[statenr];
  1796. if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
  1797. #ifdef DEBUG_REGEXP_GRAPH
  1798. printf("Removed unreachable state %d\n", statenr);
  1799. #endif
  1800. xmlRegFreeState(state);
  1801. ctxt->states[statenr] = NULL;
  1802. }
  1803. }
  1804. has_epsilon = 0;
  1805. /*
  1806. * Build the completed transitions bypassing the epsilons
  1807. * Use a marking algorithm to avoid loops
  1808. * Mark sink states too.
  1809. * Process from the latests states backward to the start when
  1810. * there is long cascading epsilon chains this minimize the
  1811. * recursions and transition compares when adding the new ones
  1812. */
  1813. for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
  1814. state = ctxt->states[statenr];
  1815. if (state == NULL)
  1816. continue;
  1817. if ((state->nbTrans == 0) &&
  1818. (state->type != XML_REGEXP_FINAL_STATE)) {
  1819. state->type = XML_REGEXP_SINK_STATE;
  1820. }
  1821. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  1822. if ((state->trans[transnr].atom == NULL) &&
  1823. (state->trans[transnr].to >= 0)) {
  1824. if (state->trans[transnr].to == statenr) {
  1825. state->trans[transnr].to = -1;
  1826. #ifdef DEBUG_REGEXP_GRAPH
  1827. printf("Removed loopback epsilon trans %d on %d\n",
  1828. transnr, statenr);
  1829. #endif
  1830. } else if (state->trans[transnr].count < 0) {
  1831. int newto = state->trans[transnr].to;
  1832. #ifdef DEBUG_REGEXP_GRAPH
  1833. printf("Found epsilon trans %d from %d to %d\n",
  1834. transnr, statenr, newto);
  1835. #endif
  1836. has_epsilon = 1;
  1837. state->trans[transnr].to = -2;
  1838. state->mark = XML_REGEXP_MARK_START;
  1839. xmlFAReduceEpsilonTransitions(ctxt, statenr,
  1840. newto, state->trans[transnr].counter);
  1841. state->mark = XML_REGEXP_MARK_NORMAL;
  1842. #ifdef DEBUG_REGEXP_GRAPH
  1843. } else {
  1844. printf("Found counted transition %d on %d\n",
  1845. transnr, statenr);
  1846. #endif
  1847. }
  1848. }
  1849. }
  1850. }
  1851. /*
  1852. * Eliminate the epsilon transitions
  1853. */
  1854. if (has_epsilon) {
  1855. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1856. state = ctxt->states[statenr];
  1857. if (state == NULL)
  1858. continue;
  1859. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  1860. xmlRegTransPtr trans = &(state->trans[transnr]);
  1861. if ((trans->atom == NULL) &&
  1862. (trans->count < 0) &&
  1863. (trans->to >= 0)) {
  1864. trans->to = -1;
  1865. }
  1866. }
  1867. }
  1868. }
  1869. /*
  1870. * Use this pass to detect unreachable states too
  1871. */
  1872. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1873. state = ctxt->states[statenr];
  1874. if (state != NULL)
  1875. state->reached = XML_REGEXP_MARK_NORMAL;
  1876. }
  1877. state = ctxt->states[0];
  1878. if (state != NULL)
  1879. state->reached = XML_REGEXP_MARK_START;
  1880. while (state != NULL) {
  1881. xmlRegStatePtr target = NULL;
  1882. state->reached = XML_REGEXP_MARK_VISITED;
  1883. /*
  1884. * Mark all states reachable from the current reachable state
  1885. */
  1886. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  1887. if ((state->trans[transnr].to >= 0) &&
  1888. ((state->trans[transnr].atom != NULL) ||
  1889. (state->trans[transnr].count >= 0))) {
  1890. int newto = state->trans[transnr].to;
  1891. if (ctxt->states[newto] == NULL)
  1892. continue;
  1893. if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
  1894. ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
  1895. target = ctxt->states[newto];
  1896. }
  1897. }
  1898. }
  1899. /*
  1900. * find the next accessible state not explored
  1901. */
  1902. if (target == NULL) {
  1903. for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
  1904. state = ctxt->states[statenr];
  1905. if ((state != NULL) && (state->reached ==
  1906. XML_REGEXP_MARK_START)) {
  1907. target = state;
  1908. break;
  1909. }
  1910. }
  1911. }
  1912. state = target;
  1913. }
  1914. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1915. state = ctxt->states[statenr];
  1916. if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
  1917. #ifdef DEBUG_REGEXP_GRAPH
  1918. printf("Removed unreachable state %d\n", statenr);
  1919. #endif
  1920. xmlRegFreeState(state);
  1921. ctxt->states[statenr] = NULL;
  1922. }
  1923. }
  1924. }
  1925. static int
  1926. xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
  1927. int ret = 0;
  1928. if ((range1->type == XML_REGEXP_RANGES) ||
  1929. (range2->type == XML_REGEXP_RANGES) ||
  1930. (range2->type == XML_REGEXP_SUBREG) ||
  1931. (range1->type == XML_REGEXP_SUBREG) ||
  1932. (range1->type == XML_REGEXP_STRING) ||
  1933. (range2->type == XML_REGEXP_STRING))
  1934. return(-1);
  1935. /* put them in order */
  1936. if (range1->type > range2->type) {
  1937. xmlRegRangePtr tmp;
  1938. tmp = range1;
  1939. range1 = range2;
  1940. range2 = tmp;
  1941. }
  1942. if ((range1->type == XML_REGEXP_ANYCHAR) ||
  1943. (range2->type == XML_REGEXP_ANYCHAR)) {
  1944. ret = 1;
  1945. } else if ((range1->type == XML_REGEXP_EPSILON) ||
  1946. (range2->type == XML_REGEXP_EPSILON)) {
  1947. return(0);
  1948. } else if (range1->type == range2->type) {
  1949. if (range1->type != XML_REGEXP_CHARVAL)
  1950. ret = 1;
  1951. else if ((range1->end < range2->start) ||
  1952. (range2->end < range1->start))
  1953. ret = 0;
  1954. else
  1955. ret = 1;
  1956. } else if (range1->type == XML_REGEXP_CHARVAL) {
  1957. int codepoint;
  1958. int neg = 0;
  1959. /*
  1960. * just check all codepoints in the range for acceptance,
  1961. * this is usually way cheaper since done only once at
  1962. * compilation than testing over and over at runtime or
  1963. * pushing too many states when evaluating.
  1964. */
  1965. if (((range1->neg == 0) && (range2->neg != 0)) ||
  1966. ((range1->neg != 0) && (range2->neg == 0)))
  1967. neg = 1;
  1968. for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
  1969. ret = xmlRegCheckCharacterRange(range2->type, codepoint,
  1970. 0, range2->start, range2->end,
  1971. range2->blockName);
  1972. if (ret < 0)
  1973. return(-1);
  1974. if (((neg == 1) && (ret == 0)) ||
  1975. ((neg == 0) && (ret == 1)))
  1976. return(1);
  1977. }
  1978. return(0);
  1979. } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
  1980. (range2->type == XML_REGEXP_BLOCK_NAME)) {
  1981. if (range1->type == range2->type) {
  1982. ret = xmlStrEqual(range1->blockName, range2->blockName);
  1983. } else {
  1984. /*
  1985. * comparing a block range with anything else is way
  1986. * too costly, and maintining the table is like too much
  1987. * memory too, so let's force the automata to save state
  1988. * here.
  1989. */
  1990. return(1);
  1991. }
  1992. } else if ((range1->type < XML_REGEXP_LETTER) ||
  1993. (range2->type < XML_REGEXP_LETTER)) {
  1994. if ((range1->type == XML_REGEXP_ANYSPACE) &&
  1995. (range2->type == XML_REGEXP_NOTSPACE))
  1996. ret = 0;
  1997. else if ((range1->type == XML_REGEXP_INITNAME) &&
  1998. (range2->type == XML_REGEXP_NOTINITNAME))
  1999. ret = 0;
  2000. else if ((range1->type == XML_REGEXP_NAMECHAR) &&
  2001. (range2->type == XML_REGEXP_NOTNAMECHAR))
  2002. ret = 0;
  2003. else if ((range1->type == XML_REGEXP_DECIMAL) &&
  2004. (range2->type == XML_REGEXP_NOTDECIMAL))
  2005. ret = 0;
  2006. else if ((range1->type == XML_REGEXP_REALCHAR) &&
  2007. (range2->type == XML_REGEXP_NOTREALCHAR))
  2008. ret = 0;
  2009. else {
  2010. /* same thing to limit complexity */
  2011. return(1);
  2012. }
  2013. } else {
  2014. ret = 0;
  2015. /* range1->type < range2->type here */
  2016. switch (range1->type) {
  2017. case XML_REGEXP_LETTER:
  2018. /* all disjoint except in the subgroups */
  2019. if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
  2020. (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
  2021. (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
  2022. (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
  2023. (range2->type == XML_REGEXP_LETTER_OTHERS))
  2024. ret = 1;
  2025. break;
  2026. case XML_REGEXP_MARK:
  2027. if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
  2028. (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
  2029. (range2->type == XML_REGEXP_MARK_ENCLOSING))
  2030. ret = 1;
  2031. break;
  2032. case XML_REGEXP_NUMBER:
  2033. if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
  2034. (range2->type == XML_REGEXP_NUMBER_LETTER) ||
  2035. (range2->type == XML_REGEXP_NUMBER_OTHERS))
  2036. ret = 1;
  2037. break;
  2038. case XML_REGEXP_PUNCT:
  2039. if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
  2040. (range2->type == XML_REGEXP_PUNCT_DASH) ||
  2041. (range2->type == XML_REGEXP_PUNCT_OPEN) ||
  2042. (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
  2043. (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
  2044. (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
  2045. (range2->type == XML_REGEXP_PUNCT_OTHERS))
  2046. ret = 1;
  2047. break;
  2048. case XML_REGEXP_SEPAR:
  2049. if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
  2050. (range2->type == XML_REGEXP_SEPAR_LINE) ||
  2051. (range2->type == XML_REGEXP_SEPAR_PARA))
  2052. ret = 1;
  2053. break;
  2054. case XML_REGEXP_SYMBOL:
  2055. if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
  2056. (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
  2057. (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
  2058. (range2->type == XML_REGEXP_SYMBOL_OTHERS))
  2059. ret = 1;
  2060. break;
  2061. case XML_REGEXP_OTHER:
  2062. if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
  2063. (range2->type == XML_REGEXP_OTHER_FORMAT) ||
  2064. (range2->type == XML_REGEXP_OTHER_PRIVATE))
  2065. ret = 1;
  2066. break;
  2067. default:
  2068. if ((range2->type >= XML_REGEXP_LETTER) &&
  2069. (range2->type < XML_REGEXP_BLOCK_NAME))
  2070. ret = 0;
  2071. else {
  2072. /* safety net ! */
  2073. return(1);
  2074. }
  2075. }
  2076. }
  2077. if (((range1->neg == 0) && (range2->neg != 0)) ||
  2078. ((range1->neg != 0) && (range2->neg == 0)))
  2079. ret = !ret;
  2080. return(ret);
  2081. }
  2082. /**
  2083. * xmlFACompareAtomTypes:
  2084. * @type1: an atom type
  2085. * @type2: an atom type
  2086. *
  2087. * Compares two atoms type to check whether they intersect in some ways,
  2088. * this is used by xmlFACompareAtoms only
  2089. *
  2090. * Returns 1 if they may intersect and 0 otherwise
  2091. */
  2092. static int
  2093. xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
  2094. if ((type1 == XML_REGEXP_EPSILON) ||
  2095. (type1 == XML_REGEXP_CHARVAL) ||
  2096. (type1 == XML_REGEXP_RANGES) ||
  2097. (type1 == XML_REGEXP_SUBREG) ||
  2098. (type1 == XML_REGEXP_STRING) ||
  2099. (type1 == XML_REGEXP_ANYCHAR))
  2100. return(1);
  2101. if ((type2 == XML_REGEXP_EPSILON) ||
  2102. (type2 == XML_REGEXP_CHARVAL) ||
  2103. (type2 == XML_REGEXP_RANGES) ||
  2104. (type2 == XML_REGEXP_SUBREG) ||
  2105. (type2 == XML_REGEXP_STRING) ||
  2106. (type2 == XML_REGEXP_ANYCHAR))
  2107. return(1);
  2108. if (type1 == type2) return(1);
  2109. /* simplify subsequent compares by making sure type1 < type2 */
  2110. if (type1 > type2) {
  2111. xmlRegAtomType tmp = type1;
  2112. type1 = type2;
  2113. type2 = tmp;
  2114. }
  2115. switch (type1) {
  2116. case XML_REGEXP_ANYSPACE: /* \s */
  2117. /* can't be a letter, number, mark, pontuation, symbol */
  2118. if ((type2 == XML_REGEXP_NOTSPACE) ||
  2119. ((type2 >= XML_REGEXP_LETTER) &&
  2120. (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
  2121. ((type2 >= XML_REGEXP_NUMBER) &&
  2122. (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
  2123. ((type2 >= XML_REGEXP_MARK) &&
  2124. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2125. ((type2 >= XML_REGEXP_PUNCT) &&
  2126. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2127. ((type2 >= XML_REGEXP_SYMBOL) &&
  2128. (type2 <= XML_REGEXP_SYMBOL_OTHERS))
  2129. ) return(0);
  2130. break;
  2131. case XML_REGEXP_NOTSPACE: /* \S */
  2132. break;
  2133. case XML_REGEXP_INITNAME: /* \l */
  2134. /* can't be a number, mark, separator, pontuation, symbol or other */
  2135. if ((type2 == XML_REGEXP_NOTINITNAME) ||
  2136. ((type2 >= XML_REGEXP_NUMBER) &&
  2137. (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
  2138. ((type2 >= XML_REGEXP_MARK) &&
  2139. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2140. ((type2 >= XML_REGEXP_SEPAR) &&
  2141. (type2 <= XML_REGEXP_SEPAR_PARA)) ||
  2142. ((type2 >= XML_REGEXP_PUNCT) &&
  2143. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2144. ((type2 >= XML_REGEXP_SYMBOL) &&
  2145. (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
  2146. ((type2 >= XML_REGEXP_OTHER) &&
  2147. (type2 <= XML_REGEXP_OTHER_NA))
  2148. ) return(0);
  2149. break;
  2150. case XML_REGEXP_NOTINITNAME: /* \L */
  2151. break;
  2152. case XML_REGEXP_NAMECHAR: /* \c */
  2153. /* can't be a mark, separator, pontuation, symbol or other */
  2154. if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
  2155. ((type2 >= XML_REGEXP_MARK) &&
  2156. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2157. ((type2 >= XML_REGEXP_PUNCT) &&
  2158. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2159. ((type2 >= XML_REGEXP_SEPAR) &&
  2160. (type2 <= XML_REGEXP_SEPAR_PARA)) ||
  2161. ((type2 >= XML_REGEXP_SYMBOL) &&
  2162. (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
  2163. ((type2 >= XML_REGEXP_OTHER) &&
  2164. (type2 <= XML_REGEXP_OTHER_NA))
  2165. ) return(0);
  2166. break;
  2167. case XML_REGEXP_NOTNAMECHAR: /* \C */
  2168. break;
  2169. case XML_REGEXP_DECIMAL: /* \d */
  2170. /* can't be a letter, mark, separator, pontuation, symbol or other */
  2171. if ((type2 == XML_REGEXP_NOTDECIMAL) ||
  2172. (type2 == XML_REGEXP_REALCHAR) ||
  2173. ((type2 >= XML_REGEXP_LETTER) &&
  2174. (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
  2175. ((type2 >= XML_REGEXP_MARK) &&
  2176. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2177. ((type2 >= XML_REGEXP_PUNCT) &&
  2178. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2179. ((type2 >= XML_REGEXP_SEPAR) &&
  2180. (type2 <= XML_REGEXP_SEPAR_PARA)) ||
  2181. ((type2 >= XML_REGEXP_SYMBOL) &&
  2182. (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
  2183. ((type2 >= XML_REGEXP_OTHER) &&
  2184. (type2 <= XML_REGEXP_OTHER_NA))
  2185. )return(0);
  2186. break;
  2187. case XML_REGEXP_NOTDECIMAL: /* \D */
  2188. break;
  2189. case XML_REGEXP_REALCHAR: /* \w */
  2190. /* can't be a mark, separator, pontuation, symbol or other */
  2191. if ((type2 == XML_REGEXP_NOTDECIMAL) ||
  2192. ((type2 >= XML_REGEXP_MARK) &&
  2193. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2194. ((type2 >= XML_REGEXP_PUNCT) &&
  2195. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2196. ((type2 >= XML_REGEXP_SEPAR) &&
  2197. (type2 <= XML_REGEXP_SEPAR_PARA)) ||
  2198. ((type2 >= XML_REGEXP_SYMBOL) &&
  2199. (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
  2200. ((type2 >= XML_REGEXP_OTHER) &&
  2201. (type2 <= XML_REGEXP_OTHER_NA))
  2202. )return(0);
  2203. break;
  2204. case XML_REGEXP_NOTREALCHAR: /* \W */
  2205. break;
  2206. /*
  2207. * at that point we know both type 1 and type2 are from
  2208. * character categories are ordered and are different,
  2209. * it becomes simple because this is a partition
  2210. */
  2211. case XML_REGEXP_LETTER:
  2212. if (type2 <= XML_REGEXP_LETTER_OTHERS)
  2213. return(1);
  2214. return(0);
  2215. case XML_REGEXP_LETTER_UPPERCASE:
  2216. case XML_REGEXP_LETTER_LOWERCASE:
  2217. case XML_REGEXP_LETTER_TITLECASE:
  2218. case XML_REGEXP_LETTER_MODIFIER:
  2219. case XML_REGEXP_LETTER_OTHERS:
  2220. return(0);
  2221. case XML_REGEXP_MARK:
  2222. if (type2 <= XML_REGEXP_MARK_ENCLOSING)
  2223. return(1);
  2224. return(0);
  2225. case XML_REGEXP_MARK_NONSPACING:
  2226. case XML_REGEXP_MARK_SPACECOMBINING:
  2227. case XML_REGEXP_MARK_ENCLOSING:
  2228. return(0);
  2229. case XML_REGEXP_NUMBER:
  2230. if (type2 <= XML_REGEXP_NUMBER_OTHERS)
  2231. return(1);
  2232. return(0);
  2233. case XML_REGEXP_NUMBER_DECIMAL:
  2234. case XML_REGEXP_NUMBER_LETTER:
  2235. case XML_REGEXP_NUMBER_OTHERS:
  2236. return(0);
  2237. case XML_REGEXP_PUNCT:
  2238. if (type2 <= XML_REGEXP_PUNCT_OTHERS)
  2239. return(1);
  2240. return(0);
  2241. case XML_REGEXP_PUNCT_CONNECTOR:
  2242. case XML_REGEXP_PUNCT_DASH:
  2243. case XML_REGEXP_PUNCT_OPEN:
  2244. case XML_REGEXP_PUNCT_CLOSE:
  2245. case XML_REGEXP_PUNCT_INITQUOTE:
  2246. case XML_REGEXP_PUNCT_FINQUOTE:
  2247. case XML_REGEXP_PUNCT_OTHERS:
  2248. return(0);
  2249. case XML_REGEXP_SEPAR:
  2250. if (type2 <= XML_REGEXP_SEPAR_PARA)
  2251. return(1);
  2252. return(0);
  2253. case XML_REGEXP_SEPAR_SPACE:
  2254. case XML_REGEXP_SEPAR_LINE:
  2255. case XML_REGEXP_SEPAR_PARA:
  2256. return(0);
  2257. case XML_REGEXP_SYMBOL:
  2258. if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
  2259. return(1);
  2260. return(0);
  2261. case XML_REGEXP_SYMBOL_MATH:
  2262. case XML_REGEXP_SYMBOL_CURRENCY:
  2263. case XML_REGEXP_SYMBOL_MODIFIER:
  2264. case XML_REGEXP_SYMBOL_OTHERS:
  2265. return(0);
  2266. case XML_REGEXP_OTHER:
  2267. if (type2 <= XML_REGEXP_OTHER_NA)
  2268. return(1);
  2269. return(0);
  2270. case XML_REGEXP_OTHER_CONTROL:
  2271. case XML_REGEXP_OTHER_FORMAT:
  2272. case XML_REGEXP_OTHER_PRIVATE:
  2273. case XML_REGEXP_OTHER_NA:
  2274. return(0);
  2275. default:
  2276. break;
  2277. }
  2278. return(1);
  2279. }
  2280. /**
  2281. * xmlFAEqualAtoms:
  2282. * @atom1: an atom
  2283. * @atom2: an atom
  2284. * @deep: if not set only compare string pointers
  2285. *
  2286. * Compares two atoms to check whether they are the same exactly
  2287. * this is used to remove equivalent transitions
  2288. *
  2289. * Returns 1 if same and 0 otherwise
  2290. */
  2291. static int
  2292. xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
  2293. int ret = 0;
  2294. if (atom1 == atom2)
  2295. return(1);
  2296. if ((atom1 == NULL) || (atom2 == NULL))
  2297. return(0);
  2298. if (atom1->type != atom2->type)
  2299. return(0);
  2300. switch (atom1->type) {
  2301. case XML_REGEXP_EPSILON:
  2302. ret = 0;
  2303. break;
  2304. case XML_REGEXP_STRING:
  2305. if (!deep)
  2306. ret = (atom1->valuep == atom2->valuep);
  2307. else
  2308. ret = xmlStrEqual((xmlChar *)atom1->valuep,
  2309. (xmlChar *)atom2->valuep);
  2310. break;
  2311. case XML_REGEXP_CHARVAL:
  2312. ret = (atom1->codepoint == atom2->codepoint);
  2313. break;
  2314. case XML_REGEXP_RANGES:
  2315. /* too hard to do in the general case */
  2316. ret = 0;
  2317. default:
  2318. break;
  2319. }
  2320. return(ret);
  2321. }
  2322. /**
  2323. * xmlFACompareAtoms:
  2324. * @atom1: an atom
  2325. * @atom2: an atom
  2326. * @deep: if not set only compare string pointers
  2327. *
  2328. * Compares two atoms to check whether they intersect in some ways,
  2329. * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
  2330. *
  2331. * Returns 1 if yes and 0 otherwise
  2332. */
  2333. static int
  2334. xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
  2335. int ret = 1;
  2336. if (atom1 == atom2)
  2337. return(1);
  2338. if ((atom1 == NULL) || (atom2 == NULL))
  2339. return(0);
  2340. if ((atom1->type == XML_REGEXP_ANYCHAR) ||
  2341. (atom2->type == XML_REGEXP_ANYCHAR))
  2342. return(1);
  2343. if (atom1->type > atom2->type) {
  2344. xmlRegAtomPtr tmp;
  2345. tmp = atom1;
  2346. atom1 = atom2;
  2347. atom2 = tmp;
  2348. }
  2349. if (atom1->type != atom2->type) {
  2350. ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
  2351. /* if they can't intersect at the type level break now */
  2352. if (ret == 0)
  2353. return(0);
  2354. }
  2355. switch (atom1->type) {
  2356. case XML_REGEXP_STRING:
  2357. if (!deep)
  2358. ret = (atom1->valuep != atom2->valuep);
  2359. else
  2360. ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
  2361. (xmlChar *)atom2->valuep);
  2362. break;
  2363. case XML_REGEXP_EPSILON:
  2364. goto not_determinist;
  2365. case XML_REGEXP_CHARVAL:
  2366. if (atom2->type == XML_REGEXP_CHARVAL) {
  2367. ret = (atom1->codepoint == atom2->codepoint);
  2368. } else {
  2369. ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
  2370. if (ret < 0)
  2371. ret = 1;
  2372. }
  2373. break;
  2374. case XML_REGEXP_RANGES:
  2375. if (atom2->type == XML_REGEXP_RANGES) {
  2376. int i, j, res;
  2377. xmlRegRangePtr r1, r2;
  2378. /*
  2379. * need to check that none of the ranges eventually matches
  2380. */
  2381. for (i = 0;i < atom1->nbRanges;i++) {
  2382. for (j = 0;j < atom2->nbRanges;j++) {
  2383. r1 = atom1->ranges[i];
  2384. r2 = atom2->ranges[j];
  2385. res = xmlFACompareRanges(r1, r2);
  2386. if (res == 1) {
  2387. ret = 1;
  2388. goto done;
  2389. }
  2390. }
  2391. }
  2392. ret = 0;
  2393. }
  2394. break;
  2395. default:
  2396. goto not_determinist;
  2397. }
  2398. done:
  2399. if (atom1->neg != atom2->neg) {
  2400. ret = !ret;
  2401. }
  2402. if (ret == 0)
  2403. return(0);
  2404. not_determinist:
  2405. return(1);
  2406. }
  2407. /**
  2408. * xmlFARecurseDeterminism:
  2409. * @ctxt: a regexp parser context
  2410. *
  2411. * Check whether the associated regexp is determinist,
  2412. * should be called after xmlFAEliminateEpsilonTransitions()
  2413. *
  2414. */
  2415. static int
  2416. xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
  2417. int to, xmlRegAtomPtr atom) {
  2418. int ret = 1;
  2419. int res;
  2420. int transnr, nbTrans;
  2421. xmlRegTransPtr t1;
  2422. int deep = 1;
  2423. if (state == NULL)
  2424. return(ret);
  2425. if (ctxt->flags & AM_AUTOMATA_RNG)
  2426. deep = 0;
  2427. /*
  2428. * don't recurse on transitions potentially added in the course of
  2429. * the elimination.
  2430. */
  2431. nbTrans = state->nbTrans;
  2432. for (transnr = 0;transnr < nbTrans;transnr++) {
  2433. t1 = &(state->trans[transnr]);
  2434. /*
  2435. * check transitions conflicting with the one looked at
  2436. */
  2437. if (t1->atom == NULL) {
  2438. if (t1->to < 0)
  2439. continue;
  2440. res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
  2441. to, atom);
  2442. if (res == 0) {
  2443. ret = 0;
  2444. /* t1->nd = 1; */
  2445. }
  2446. continue;
  2447. }
  2448. if (t1->to != to)
  2449. continue;
  2450. if (xmlFACompareAtoms(t1->atom, atom, deep)) {
  2451. ret = 0;
  2452. /* mark the transition as non-deterministic */
  2453. t1->nd = 1;
  2454. }
  2455. }
  2456. return(ret);
  2457. }
  2458. /**
  2459. * xmlFAComputesDeterminism:
  2460. * @ctxt: a regexp parser context
  2461. *
  2462. * Check whether the associated regexp is determinist,
  2463. * should be called after xmlFAEliminateEpsilonTransitions()
  2464. *
  2465. */
  2466. static int
  2467. xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
  2468. int statenr, transnr;
  2469. xmlRegStatePtr state;
  2470. xmlRegTransPtr t1, t2, last;
  2471. int i;
  2472. int ret = 1;
  2473. int deep = 1;
  2474. #ifdef DEBUG_REGEXP_GRAPH
  2475. printf("xmlFAComputesDeterminism\n");
  2476. xmlRegPrintCtxt(stdout, ctxt);
  2477. #endif
  2478. if (ctxt->determinist != -1)
  2479. return(ctxt->determinist);
  2480. if (ctxt->flags & AM_AUTOMATA_RNG)
  2481. deep = 0;
  2482. /*
  2483. * First cleanup the automata removing cancelled transitions
  2484. */
  2485. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  2486. state = ctxt->states[statenr];
  2487. if (state == NULL)
  2488. continue;
  2489. if (state->nbTrans < 2)
  2490. continue;
  2491. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  2492. t1 = &(state->trans[transnr]);
  2493. /*
  2494. * Determinism checks in case of counted or all transitions
  2495. * will have to be handled separately
  2496. */
  2497. if (t1->atom == NULL) {
  2498. /* t1->nd = 1; */
  2499. continue;
  2500. }
  2501. if (t1->to == -1) /* eliminated */
  2502. continue;
  2503. for (i = 0;i < transnr;i++) {
  2504. t2 = &(state->trans[i]);
  2505. if (t2->to == -1) /* eliminated */
  2506. continue;
  2507. if (t2->atom != NULL) {
  2508. if (t1->to == t2->to) {
  2509. /*
  2510. * Here we use deep because we want to keep the
  2511. * transitions which indicate a conflict
  2512. */
  2513. if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
  2514. (t1->counter == t2->counter) &&
  2515. (t1->count == t2->count))
  2516. t2->to = -1; /* eliminated */
  2517. }
  2518. }
  2519. }
  2520. }
  2521. }
  2522. /*
  2523. * Check for all states that there aren't 2 transitions
  2524. * with the same atom and a different target.
  2525. */
  2526. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  2527. state = ctxt->states[statenr];
  2528. if (state == NULL)
  2529. continue;
  2530. if (state->nbTrans < 2)
  2531. continue;
  2532. last = NULL;
  2533. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  2534. t1 = &(state->trans[transnr]);
  2535. /*
  2536. * Determinism checks in case of counted or all transitions
  2537. * will have to be handled separately
  2538. */
  2539. if (t1->atom == NULL) {
  2540. continue;
  2541. }
  2542. if (t1->to == -1) /* eliminated */
  2543. continue;
  2544. for (i = 0;i < transnr;i++) {
  2545. t2 = &(state->trans[i]);
  2546. if (t2->to == -1) /* eliminated */
  2547. continue;
  2548. if (t2->atom != NULL) {
  2549. /*
  2550. * But here we don't use deep because we want to
  2551. * find transitions which indicate a conflict
  2552. */
  2553. if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
  2554. ret = 0;
  2555. /* mark the transitions as non-deterministic ones */
  2556. t1->nd = 1;
  2557. t2->nd = 1;
  2558. last = t1;
  2559. }
  2560. } else if (t1->to != -1) {
  2561. /*
  2562. * do the closure in case of remaining specific
  2563. * epsilon transitions like choices or all
  2564. */
  2565. ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
  2566. t2->to, t2->atom);
  2567. /* don't shortcut the computation so all non deterministic
  2568. transition get marked down
  2569. if (ret == 0)
  2570. return(0);
  2571. */
  2572. if (ret == 0) {
  2573. t1->nd = 1;
  2574. /* t2->nd = 1; */
  2575. last = t1;
  2576. }
  2577. }
  2578. }
  2579. /* don't shortcut the computation so all non deterministic
  2580. transition get marked down
  2581. if (ret == 0)
  2582. break; */
  2583. }
  2584. /*
  2585. * mark specifically the last non-deterministic transition
  2586. * from a state since there is no need to set-up rollback
  2587. * from it
  2588. */
  2589. if (last != NULL) {
  2590. last->nd = 2;
  2591. }
  2592. /* don't shortcut the computation so all non deterministic
  2593. transition get marked down
  2594. if (ret == 0)
  2595. break; */
  2596. }
  2597. ctxt->determinist = ret;
  2598. return(ret);
  2599. }
  2600. /************************************************************************
  2601. * *
  2602. * Routines to check input against transition atoms *
  2603. * *
  2604. ************************************************************************/
  2605. static int
  2606. xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
  2607. int start, int end, const xmlChar *blockName) {
  2608. int ret = 0;
  2609. switch (type) {
  2610. case XML_REGEXP_STRING:
  2611. case XML_REGEXP_SUBREG:
  2612. case XML_REGEXP_RANGES:
  2613. case XML_REGEXP_EPSILON:
  2614. return(-1);
  2615. case XML_REGEXP_ANYCHAR:
  2616. ret = ((codepoint != '\n') && (codepoint != '\r'));
  2617. break;
  2618. case XML_REGEXP_CHARVAL:
  2619. ret = ((codepoint >= start) && (codepoint <= end));
  2620. break;
  2621. case XML_REGEXP_NOTSPACE:
  2622. neg = !neg;
  2623. case XML_REGEXP_ANYSPACE:
  2624. ret = ((codepoint == '\n') || (codepoint == '\r') ||
  2625. (codepoint == '\t') || (codepoint == ' '));
  2626. break;
  2627. case XML_REGEXP_NOTINITNAME:
  2628. neg = !neg;
  2629. case XML_REGEXP_INITNAME:
  2630. ret = (IS_LETTER(codepoint) ||
  2631. (codepoint == '_') || (codepoint == ':'));
  2632. break;
  2633. case XML_REGEXP_NOTNAMECHAR:
  2634. neg = !neg;
  2635. case XML_REGEXP_NAMECHAR:
  2636. ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
  2637. (codepoint == '.') || (codepoint == '-') ||
  2638. (codepoint == '_') || (codepoint == ':') ||
  2639. IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
  2640. break;
  2641. case XML_REGEXP_NOTDECIMAL:
  2642. neg = !neg;
  2643. case XML_REGEXP_DECIMAL:
  2644. ret = xmlUCSIsCatNd(codepoint);
  2645. break;
  2646. case XML_REGEXP_REALCHAR:
  2647. neg = !neg;
  2648. case XML_REGEXP_NOTREALCHAR:
  2649. ret = xmlUCSIsCatP(codepoint);
  2650. if (ret == 0)
  2651. ret = xmlUCSIsCatZ(codepoint);
  2652. if (ret == 0)
  2653. ret = xmlUCSIsCatC(codepoint);
  2654. break;
  2655. case XML_REGEXP_LETTER:
  2656. ret = xmlUCSIsCatL(codepoint);
  2657. break;
  2658. case XML_REGEXP_LETTER_UPPERCASE:
  2659. ret = xmlUCSIsCatLu(codepoint);
  2660. break;
  2661. case XML_REGEXP_LETTER_LOWERCASE:
  2662. ret = xmlUCSIsCatLl(codepoint);
  2663. break;
  2664. case XML_REGEXP_LETTER_TITLECASE:
  2665. ret = xmlUCSIsCatLt(codepoint);
  2666. break;
  2667. case XML_REGEXP_LETTER_MODIFIER:
  2668. ret = xmlUCSIsCatLm(codepoint);
  2669. break;
  2670. case XML_REGEXP_LETTER_OTHERS:
  2671. ret = xmlUCSIsCatLo(codepoint);
  2672. break;
  2673. case XML_REGEXP_MARK:
  2674. ret = xmlUCSIsCatM(codepoint);
  2675. break;
  2676. case XML_REGEXP_MARK_NONSPACING:
  2677. ret = xmlUCSIsCatMn(codepoint);
  2678. break;
  2679. case XML_REGEXP_MARK_SPACECOMBINING:
  2680. ret = xmlUCSIsCatMc(codepoint);
  2681. break;
  2682. case XML_REGEXP_MARK_ENCLOSING:
  2683. ret = xmlUCSIsCatMe(codepoint);
  2684. break;
  2685. case XML_REGEXP_NUMBER:
  2686. ret = xmlUCSIsCatN(codepoint);
  2687. break;
  2688. case XML_REGEXP_NUMBER_DECIMAL:
  2689. ret = xmlUCSIsCatNd(codepoint);
  2690. break;
  2691. case XML_REGEXP_NUMBER_LETTER:
  2692. ret = xmlUCSIsCatNl(codepoint);
  2693. break;
  2694. case XML_REGEXP_NUMBER_OTHERS:
  2695. ret = xmlUCSIsCatNo(codepoint);
  2696. break;
  2697. case XML_REGEXP_PUNCT:
  2698. ret = xmlUCSIsCatP(codepoint);
  2699. break;
  2700. case XML_REGEXP_PUNCT_CONNECTOR:
  2701. ret = xmlUCSIsCatPc(codepoint);
  2702. break;
  2703. case XML_REGEXP_PUNCT_DASH:
  2704. ret = xmlUCSIsCatPd(codepoint);
  2705. break;
  2706. case XML_REGEXP_PUNCT_OPEN:
  2707. ret = xmlUCSIsCatPs(codepoint);
  2708. break;
  2709. case XML_REGEXP_PUNCT_CLOSE:
  2710. ret = xmlUCSIsCatPe(codepoint);
  2711. break;
  2712. case XML_REGEXP_PUNCT_INITQUOTE:
  2713. ret = xmlUCSIsCatPi(codepoint);
  2714. break;
  2715. case XML_REGEXP_PUNCT_FINQUOTE:
  2716. ret = xmlUCSIsCatPf(codepoint);
  2717. break;
  2718. case XML_REGEXP_PUNCT_OTHERS:
  2719. ret = xmlUCSIsCatPo(codepoint);
  2720. break;
  2721. case XML_REGEXP_SEPAR:
  2722. ret = xmlUCSIsCatZ(codepoint);
  2723. break;
  2724. case XML_REGEXP_SEPAR_SPACE:
  2725. ret = xmlUCSIsCatZs(codepoint);
  2726. break;
  2727. case XML_REGEXP_SEPAR_LINE:
  2728. ret = xmlUCSIsCatZl(codepoint);
  2729. break;
  2730. case XML_REGEXP_SEPAR_PARA:
  2731. ret = xmlUCSIsCatZp(codepoint);
  2732. break;
  2733. case XML_REGEXP_SYMBOL:
  2734. ret = xmlUCSIsCatS(codepoint);
  2735. break;
  2736. case XML_REGEXP_SYMBOL_MATH:
  2737. ret = xmlUCSIsCatSm(codepoint);
  2738. break;
  2739. case XML_REGEXP_SYMBOL_CURRENCY:
  2740. ret = xmlUCSIsCatSc(codepoint);
  2741. break;
  2742. case XML_REGEXP_SYMBOL_MODIFIER:
  2743. ret = xmlUCSIsCatSk(codepoint);
  2744. break;
  2745. case XML_REGEXP_SYMBOL_OTHERS:
  2746. ret = xmlUCSIsCatSo(codepoint);
  2747. break;
  2748. case XML_REGEXP_OTHER:
  2749. ret = xmlUCSIsCatC(codepoint);
  2750. break;
  2751. case XML_REGEXP_OTHER_CONTROL:
  2752. ret = xmlUCSIsCatCc(codepoint);
  2753. break;
  2754. case XML_REGEXP_OTHER_FORMAT:
  2755. ret = xmlUCSIsCatCf(codepoint);
  2756. break;
  2757. case XML_REGEXP_OTHER_PRIVATE:
  2758. ret = xmlUCSIsCatCo(codepoint);
  2759. break;
  2760. case XML_REGEXP_OTHER_NA:
  2761. /* ret = xmlUCSIsCatCn(codepoint); */
  2762. /* Seems it doesn't exist anymore in recent Unicode releases */
  2763. ret = 0;
  2764. break;
  2765. case XML_REGEXP_BLOCK_NAME:
  2766. ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
  2767. break;
  2768. }
  2769. if (neg)
  2770. return(!ret);
  2771. return(ret);
  2772. }
  2773. static int
  2774. xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
  2775. int i, ret = 0;
  2776. xmlRegRangePtr range;
  2777. if ((atom == NULL) || (!IS_CHAR(codepoint)))
  2778. return(-1);
  2779. switch (atom->type) {
  2780. case XML_REGEXP_SUBREG:
  2781. case XML_REGEXP_EPSILON:
  2782. return(-1);
  2783. case XML_REGEXP_CHARVAL:
  2784. return(codepoint == atom->codepoint);
  2785. case XML_REGEXP_RANGES: {
  2786. int accept = 0;
  2787. for (i = 0;i < atom->nbRanges;i++) {
  2788. range = atom->ranges[i];
  2789. if (range->neg == 2) {
  2790. ret = xmlRegCheckCharacterRange(range->type, codepoint,
  2791. 0, range->start, range->end,
  2792. range->blockName);
  2793. if (ret != 0)
  2794. return(0); /* excluded char */
  2795. } else if (range->neg) {
  2796. ret = xmlRegCheckCharacterRange(range->type, codepoint,
  2797. 0, range->start, range->end,
  2798. range->blockName);
  2799. if (ret == 0)
  2800. accept = 1;
  2801. else
  2802. return(0);
  2803. } else {
  2804. ret = xmlRegCheckCharacterRange(range->type, codepoint,
  2805. 0, range->start, range->end,
  2806. range->blockName);
  2807. if (ret != 0)
  2808. accept = 1; /* might still be excluded */
  2809. }
  2810. }
  2811. return(accept);
  2812. }
  2813. case XML_REGEXP_STRING:
  2814. printf("TODO: XML_REGEXP_STRING\n");
  2815. return(-1);
  2816. case XML_REGEXP_ANYCHAR:
  2817. case XML_REGEXP_ANYSPACE:
  2818. case XML_REGEXP_NOTSPACE:
  2819. case XML_REGEXP_INITNAME:
  2820. case XML_REGEXP_NOTINITNAME:
  2821. case XML_REGEXP_NAMECHAR:
  2822. case XML_REGEXP_NOTNAMECHAR:
  2823. case XML_REGEXP_DECIMAL:
  2824. case XML_REGEXP_NOTDECIMAL:
  2825. case XML_REGEXP_REALCHAR:
  2826. case XML_REGEXP_NOTREALCHAR:
  2827. case XML_REGEXP_LETTER:
  2828. case XML_REGEXP_LETTER_UPPERCASE:
  2829. case XML_REGEXP_LETTER_LOWERCASE:
  2830. case XML_REGEXP_LETTER_TITLECASE:
  2831. case XML_REGEXP_LETTER_MODIFIER:
  2832. case XML_REGEXP_LETTER_OTHERS:
  2833. case XML_REGEXP_MARK:
  2834. case XML_REGEXP_MARK_NONSPACING:
  2835. case XML_REGEXP_MARK_SPACECOMBINING:
  2836. case XML_REGEXP_MARK_ENCLOSING:
  2837. case XML_REGEXP_NUMBER:
  2838. case XML_REGEXP_NUMBER_DECIMAL:
  2839. case XML_REGEXP_NUMBER_LETTER:
  2840. case XML_REGEXP_NUMBER_OTHERS:
  2841. case XML_REGEXP_PUNCT:
  2842. case XML_REGEXP_PUNCT_CONNECTOR:
  2843. case XML_REGEXP_PUNCT_DASH:
  2844. case XML_REGEXP_PUNCT_OPEN:
  2845. case XML_REGEXP_PUNCT_CLOSE:
  2846. case XML_REGEXP_PUNCT_INITQUOTE:
  2847. case XML_REGEXP_PUNCT_FINQUOTE:
  2848. case XML_REGEXP_PUNCT_OTHERS:
  2849. case XML_REGEXP_SEPAR:
  2850. case XML_REGEXP_SEPAR_SPACE:
  2851. case XML_REGEXP_SEPAR_LINE:
  2852. case XML_REGEXP_SEPAR_PARA:
  2853. case XML_REGEXP_SYMBOL:
  2854. case XML_REGEXP_SYMBOL_MATH:
  2855. case XML_REGEXP_SYMBOL_CURRENCY:
  2856. case XML_REGEXP_SYMBOL_MODIFIER:
  2857. case XML_REGEXP_SYMBOL_OTHERS:
  2858. case XML_REGEXP_OTHER:
  2859. case XML_REGEXP_OTHER_CONTROL:
  2860. case XML_REGEXP_OTHER_FORMAT:
  2861. case XML_REGEXP_OTHER_PRIVATE:
  2862. case XML_REGEXP_OTHER_NA:
  2863. case XML_REGEXP_BLOCK_NAME:
  2864. ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
  2865. (const xmlChar *)atom->valuep);
  2866. if (atom->neg)
  2867. ret = !ret;
  2868. break;
  2869. }
  2870. return(ret);
  2871. }
  2872. /************************************************************************
  2873. * *
  2874. * Saving and restoring state of an execution context *
  2875. * *
  2876. ************************************************************************/
  2877. #ifdef DEBUG_REGEXP_EXEC
  2878. static void
  2879. xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
  2880. printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
  2881. if (exec->inputStack != NULL) {
  2882. int i;
  2883. printf(": ");
  2884. for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
  2885. printf("%s ", (const char *)
  2886. exec->inputStack[exec->inputStackNr - (i + 1)].value);
  2887. } else {
  2888. printf(": %s", &(exec->inputString[exec->index]));
  2889. }
  2890. printf("\n");
  2891. }
  2892. #endif
  2893. static void
  2894. xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
  2895. #ifdef DEBUG_REGEXP_EXEC
  2896. printf("saving ");
  2897. exec->transno++;
  2898. xmlFARegDebugExec(exec);
  2899. exec->transno--;
  2900. #endif
  2901. #ifdef MAX_PUSH
  2902. if (exec->nbPush > MAX_PUSH) {
  2903. return;
  2904. }
  2905. exec->nbPush++;
  2906. #endif
  2907. if (exec->maxRollbacks == 0) {
  2908. exec->maxRollbacks = 4;
  2909. exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
  2910. sizeof(xmlRegExecRollback));
  2911. if (exec->rollbacks == NULL) {
  2912. xmlRegexpErrMemory(NULL, "saving regexp");
  2913. exec->maxRollbacks = 0;
  2914. return;
  2915. }
  2916. memset(exec->rollbacks, 0,
  2917. exec->maxRollbacks * sizeof(xmlRegExecRollback));
  2918. } else if (exec->nbRollbacks >= exec->maxRollbacks) {
  2919. xmlRegExecRollback *tmp;
  2920. int len = exec->maxRollbacks;
  2921. exec->maxRollbacks *= 2;
  2922. tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
  2923. exec->maxRollbacks * sizeof(xmlRegExecRollback));
  2924. if (tmp == NULL) {
  2925. xmlRegexpErrMemory(NULL, "saving regexp");
  2926. exec->maxRollbacks /= 2;
  2927. return;
  2928. }
  2929. exec->rollbacks = tmp;
  2930. tmp = &exec->rollbacks[len];
  2931. memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
  2932. }
  2933. exec->rollbacks[exec->nbRollbacks].state = exec->state;
  2934. exec->rollbacks[exec->nbRollbacks].index = exec->index;
  2935. exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
  2936. if (exec->comp->nbCounters > 0) {
  2937. if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
  2938. exec->rollbacks[exec->nbRollbacks].counts = (int *)
  2939. xmlMalloc(exec->comp->nbCounters * sizeof(int));
  2940. if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
  2941. xmlRegexpErrMemory(NULL, "saving regexp");
  2942. exec->status = -5;
  2943. return;
  2944. }
  2945. }
  2946. memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
  2947. exec->comp->nbCounters * sizeof(int));
  2948. }
  2949. exec->nbRollbacks++;
  2950. }
  2951. static void
  2952. xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
  2953. if (exec->nbRollbacks <= 0) {
  2954. exec->status = -1;
  2955. #ifdef DEBUG_REGEXP_EXEC
  2956. printf("rollback failed on empty stack\n");
  2957. #endif
  2958. return;
  2959. }
  2960. exec->nbRollbacks--;
  2961. exec->state = exec->rollbacks[exec->nbRollbacks].state;
  2962. exec->index = exec->rollbacks[exec->nbRollbacks].index;
  2963. exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
  2964. if (exec->comp->nbCounters > 0) {
  2965. if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
  2966. fprintf(stderr, "exec save: allocation failed");
  2967. exec->status = -6;
  2968. return;
  2969. }
  2970. memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
  2971. exec->comp->nbCounters * sizeof(int));
  2972. }
  2973. #ifdef DEBUG_REGEXP_EXEC
  2974. printf("restored ");
  2975. xmlFARegDebugExec(exec);
  2976. #endif
  2977. }
  2978. /************************************************************************
  2979. * *
  2980. * Verifier, running an input against a compiled regexp *
  2981. * *
  2982. ************************************************************************/
  2983. static int
  2984. xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
  2985. xmlRegExecCtxt execval;
  2986. xmlRegExecCtxtPtr exec = &execval;
  2987. int ret, codepoint = 0, len, deter;
  2988. exec->inputString = content;
  2989. exec->index = 0;
  2990. exec->nbPush = 0;
  2991. exec->determinist = 1;
  2992. exec->maxRollbacks = 0;
  2993. exec->nbRollbacks = 0;
  2994. exec->rollbacks = NULL;
  2995. exec->status = 0;
  2996. exec->comp = comp;
  2997. exec->state = comp->states[0];
  2998. exec->transno = 0;
  2999. exec->transcount = 0;
  3000. exec->inputStack = NULL;
  3001. exec->inputStackMax = 0;
  3002. if (comp->nbCounters > 0) {
  3003. exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
  3004. if (exec->counts == NULL) {
  3005. xmlRegexpErrMemory(NULL, "running regexp");
  3006. return(-1);
  3007. }
  3008. memset(exec->counts, 0, comp->nbCounters * sizeof(int));
  3009. } else
  3010. exec->counts = NULL;
  3011. while ((exec->status == 0) &&
  3012. ((exec->inputString[exec->index] != 0) ||
  3013. ((exec->state != NULL) &&
  3014. (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
  3015. xmlRegTransPtr trans;
  3016. xmlRegAtomPtr atom;
  3017. /*
  3018. * If end of input on non-terminal state, rollback, however we may
  3019. * still have epsilon like transition for counted transitions
  3020. * on counters, in that case don't break too early. Additionally,
  3021. * if we are working on a range like "AB{0,2}", where B is not present,
  3022. * we don't want to break.
  3023. */
  3024. len = 1;
  3025. if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
  3026. /*
  3027. * if there is a transition, we must check if
  3028. * atom allows minOccurs of 0
  3029. */
  3030. if (exec->transno < exec->state->nbTrans) {
  3031. trans = &exec->state->trans[exec->transno];
  3032. if (trans->to >=0) {
  3033. atom = trans->atom;
  3034. if (!((atom->min == 0) && (atom->max > 0)))
  3035. goto rollback;
  3036. }
  3037. } else
  3038. goto rollback;
  3039. }
  3040. exec->transcount = 0;
  3041. for (;exec->transno < exec->state->nbTrans;exec->transno++) {
  3042. trans = &exec->state->trans[exec->transno];
  3043. if (trans->to < 0)
  3044. continue;
  3045. atom = trans->atom;
  3046. ret = 0;
  3047. deter = 1;
  3048. if (trans->count >= 0) {
  3049. int count;
  3050. xmlRegCounterPtr counter;
  3051. if (exec->counts == NULL) {
  3052. exec->status = -1;
  3053. goto error;
  3054. }
  3055. /*
  3056. * A counted transition.
  3057. */
  3058. count = exec->counts[trans->count];
  3059. counter = &exec->comp->counters[trans->count];
  3060. #ifdef DEBUG_REGEXP_EXEC
  3061. printf("testing count %d: val %d, min %d, max %d\n",
  3062. trans->count, count, counter->min, counter->max);
  3063. #endif
  3064. ret = ((count >= counter->min) && (count <= counter->max));
  3065. if ((ret) && (counter->min != counter->max))
  3066. deter = 0;
  3067. } else if (atom == NULL) {
  3068. fprintf(stderr, "epsilon transition left at runtime\n");
  3069. exec->status = -2;
  3070. break;
  3071. } else if (exec->inputString[exec->index] != 0) {
  3072. codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
  3073. ret = xmlRegCheckCharacter(atom, codepoint);
  3074. if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
  3075. xmlRegStatePtr to = comp->states[trans->to];
  3076. /*
  3077. * this is a multiple input sequence
  3078. * If there is a counter associated increment it now.
  3079. * before potentially saving and rollback
  3080. * do not increment if the counter is already over the
  3081. * maximum limit in which case get to next transition
  3082. */
  3083. if (trans->counter >= 0) {
  3084. xmlRegCounterPtr counter;
  3085. if ((exec->counts == NULL) ||
  3086. (exec->comp == NULL) ||
  3087. (exec->comp->counters == NULL)) {
  3088. exec->status = -1;
  3089. goto error;
  3090. }
  3091. counter = &exec->comp->counters[trans->counter];
  3092. if (exec->counts[trans->counter] >= counter->max)
  3093. continue; /* for loop on transitions */
  3094. #ifdef DEBUG_REGEXP_EXEC
  3095. printf("Increasing count %d\n", trans->counter);
  3096. #endif
  3097. exec->counts[trans->counter]++;
  3098. }
  3099. if (exec->state->nbTrans > exec->transno + 1) {
  3100. xmlFARegExecSave(exec);
  3101. }
  3102. exec->transcount = 1;
  3103. do {
  3104. /*
  3105. * Try to progress as much as possible on the input
  3106. */
  3107. if (exec->transcount == atom->max) {
  3108. break;
  3109. }
  3110. exec->index += len;
  3111. /*
  3112. * End of input: stop here
  3113. */
  3114. if (exec->inputString[exec->index] == 0) {
  3115. exec->index -= len;
  3116. break;
  3117. }
  3118. if (exec->transcount >= atom->min) {
  3119. int transno = exec->transno;
  3120. xmlRegStatePtr state = exec->state;
  3121. /*
  3122. * The transition is acceptable save it
  3123. */
  3124. exec->transno = -1; /* trick */
  3125. exec->state = to;
  3126. xmlFARegExecSave(exec);
  3127. exec->transno = transno;
  3128. exec->state = state;
  3129. }
  3130. codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
  3131. len);
  3132. ret = xmlRegCheckCharacter(atom, codepoint);
  3133. exec->transcount++;
  3134. } while (ret == 1);
  3135. if (exec->transcount < atom->min)
  3136. ret = 0;
  3137. /*
  3138. * If the last check failed but one transition was found
  3139. * possible, rollback
  3140. */
  3141. if (ret < 0)
  3142. ret = 0;
  3143. if (ret == 0) {
  3144. goto rollback;
  3145. }
  3146. if (trans->counter >= 0) {
  3147. if (exec->counts == NULL) {
  3148. exec->status = -1;
  3149. goto error;
  3150. }
  3151. #ifdef DEBUG_REGEXP_EXEC
  3152. printf("Decreasing count %d\n", trans->counter);
  3153. #endif
  3154. exec->counts[trans->counter]--;
  3155. }
  3156. } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
  3157. /*
  3158. * we don't match on the codepoint, but minOccurs of 0
  3159. * says that's ok. Setting len to 0 inhibits stepping
  3160. * over the codepoint.
  3161. */
  3162. exec->transcount = 1;
  3163. len = 0;
  3164. ret = 1;
  3165. }
  3166. } else if ((atom->min == 0) && (atom->max > 0)) {
  3167. /* another spot to match when minOccurs is 0 */
  3168. exec->transcount = 1;
  3169. len = 0;
  3170. ret = 1;
  3171. }
  3172. if (ret == 1) {
  3173. if ((trans->nd == 1) ||
  3174. ((trans->count >= 0) && (deter == 0) &&
  3175. (exec->state->nbTrans > exec->transno + 1))) {
  3176. #ifdef DEBUG_REGEXP_EXEC
  3177. if (trans->nd == 1)
  3178. printf("Saving on nd transition atom %d for %c at %d\n",
  3179. trans->atom->no, codepoint, exec->index);
  3180. else
  3181. printf("Saving on counted transition count %d for %c at %d\n",
  3182. trans->count, codepoint, exec->index);
  3183. #endif
  3184. xmlFARegExecSave(exec);
  3185. }
  3186. if (trans->counter >= 0) {
  3187. xmlRegCounterPtr counter;
  3188. /* make sure we don't go over the counter maximum value */
  3189. if ((exec->counts == NULL) ||
  3190. (exec->comp == NULL) ||
  3191. (exec->comp->counters == NULL)) {
  3192. exec->status = -1;
  3193. goto error;
  3194. }
  3195. counter = &exec->comp->counters[trans->counter];
  3196. if (exec->counts[trans->counter] >= counter->max)
  3197. continue; /* for loop on transitions */
  3198. #ifdef DEBUG_REGEXP_EXEC
  3199. printf("Increasing count %d\n", trans->counter);
  3200. #endif
  3201. exec->counts[trans->counter]++;
  3202. }
  3203. if ((trans->count >= 0) &&
  3204. (trans->count < REGEXP_ALL_COUNTER)) {
  3205. if (exec->counts == NULL) {
  3206. exec->status = -1;
  3207. goto error;
  3208. }
  3209. #ifdef DEBUG_REGEXP_EXEC
  3210. printf("resetting count %d on transition\n",
  3211. trans->count);
  3212. #endif
  3213. exec->counts[trans->count] = 0;
  3214. }
  3215. #ifdef DEBUG_REGEXP_EXEC
  3216. printf("entering state %d\n", trans->to);
  3217. #endif
  3218. exec->state = comp->states[trans->to];
  3219. exec->transno = 0;
  3220. if (trans->atom != NULL) {
  3221. exec->index += len;
  3222. }
  3223. goto progress;
  3224. } else if (ret < 0) {
  3225. exec->status = -4;
  3226. break;
  3227. }
  3228. }
  3229. if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
  3230. rollback:
  3231. /*
  3232. * Failed to find a way out
  3233. */
  3234. exec->determinist = 0;
  3235. #ifdef DEBUG_REGEXP_EXEC
  3236. printf("rollback from state %d on %d:%c\n", exec->state->no,
  3237. codepoint,codepoint);
  3238. #endif
  3239. xmlFARegExecRollBack(exec);
  3240. }
  3241. progress:
  3242. continue;
  3243. }
  3244. error:
  3245. if (exec->rollbacks != NULL) {
  3246. if (exec->counts != NULL) {
  3247. int i;
  3248. for (i = 0;i < exec->maxRollbacks;i++)
  3249. if (exec->rollbacks[i].counts != NULL)
  3250. xmlFree(exec->rollbacks[i].counts);
  3251. }
  3252. xmlFree(exec->rollbacks);
  3253. }
  3254. if (exec->counts != NULL)
  3255. xmlFree(exec->counts);
  3256. if (exec->status == 0)
  3257. return(1);
  3258. if (exec->status == -1) {
  3259. if (exec->nbPush > MAX_PUSH)
  3260. return(-1);
  3261. return(0);
  3262. }
  3263. return(exec->status);
  3264. }
  3265. /************************************************************************
  3266. * *
  3267. * Progressive interface to the verifier one atom at a time *
  3268. * *
  3269. ************************************************************************/
  3270. #ifdef DEBUG_ERR
  3271. static void testerr(xmlRegExecCtxtPtr exec);
  3272. #endif
  3273. /**
  3274. * xmlRegNewExecCtxt:
  3275. * @comp: a precompiled regular expression
  3276. * @callback: a callback function used for handling progresses in the
  3277. * automata matching phase
  3278. * @data: the context data associated to the callback in this context
  3279. *
  3280. * Build a context used for progressive evaluation of a regexp.
  3281. *
  3282. * Returns the new context
  3283. */
  3284. xmlRegExecCtxtPtr
  3285. xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
  3286. xmlRegExecCtxtPtr exec;
  3287. if (comp == NULL)
  3288. return(NULL);
  3289. if ((comp->compact == NULL) && (comp->states == NULL))
  3290. return(NULL);
  3291. exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
  3292. if (exec == NULL) {
  3293. xmlRegexpErrMemory(NULL, "creating execution context");
  3294. return(NULL);
  3295. }
  3296. memset(exec, 0, sizeof(xmlRegExecCtxt));
  3297. exec->inputString = NULL;
  3298. exec->index = 0;
  3299. exec->determinist = 1;
  3300. exec->maxRollbacks = 0;
  3301. exec->nbRollbacks = 0;
  3302. exec->rollbacks = NULL;
  3303. exec->status = 0;
  3304. exec->comp = comp;
  3305. if (comp->compact == NULL)
  3306. exec->state = comp->states[0];
  3307. exec->transno = 0;
  3308. exec->transcount = 0;
  3309. exec->callback = callback;
  3310. exec->data = data;
  3311. if (comp->nbCounters > 0) {
  3312. /*
  3313. * For error handling, exec->counts is allocated twice the size
  3314. * the second half is used to store the data in case of rollback
  3315. */
  3316. exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
  3317. * 2);
  3318. if (exec->counts == NULL) {
  3319. xmlRegexpErrMemory(NULL, "creating execution context");
  3320. xmlFree(exec);
  3321. return(NULL);
  3322. }
  3323. memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
  3324. exec->errCounts = &exec->counts[comp->nbCounters];
  3325. } else {
  3326. exec->counts = NULL;
  3327. exec->errCounts = NULL;
  3328. }
  3329. exec->inputStackMax = 0;
  3330. exec->inputStackNr = 0;
  3331. exec->inputStack = NULL;
  3332. exec->errStateNo = -1;
  3333. exec->errString = NULL;
  3334. exec->nbPush = 0;
  3335. return(exec);
  3336. }
  3337. /**
  3338. * xmlRegFreeExecCtxt:
  3339. * @exec: a regular expression evaulation context
  3340. *
  3341. * Free the structures associated to a regular expression evaulation context.
  3342. */
  3343. void
  3344. xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
  3345. if (exec == NULL)
  3346. return;
  3347. if (exec->rollbacks != NULL) {
  3348. if (exec->counts != NULL) {
  3349. int i;
  3350. for (i = 0;i < exec->maxRollbacks;i++)
  3351. if (exec->rollbacks[i].counts != NULL)
  3352. xmlFree(exec->rollbacks[i].counts);
  3353. }
  3354. xmlFree(exec->rollbacks);
  3355. }
  3356. if (exec->counts != NULL)
  3357. xmlFree(exec->counts);
  3358. if (exec->inputStack != NULL) {
  3359. int i;
  3360. for (i = 0;i < exec->inputStackNr;i++) {
  3361. if (exec->inputStack[i].value != NULL)
  3362. xmlFree(exec->inputStack[i].value);
  3363. }
  3364. xmlFree(exec->inputStack);
  3365. }
  3366. if (exec->errString != NULL)
  3367. xmlFree(exec->errString);
  3368. xmlFree(exec);
  3369. }
  3370. static void
  3371. xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
  3372. void *data) {
  3373. #ifdef DEBUG_PUSH
  3374. printf("saving value: %d:%s\n", exec->inputStackNr, value);
  3375. #endif
  3376. if (exec->inputStackMax == 0) {
  3377. exec->inputStackMax = 4;
  3378. exec->inputStack = (xmlRegInputTokenPtr)
  3379. xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
  3380. if (exec->inputStack == NULL) {
  3381. xmlRegexpErrMemory(NULL, "pushing input string");
  3382. exec->inputStackMax = 0;
  3383. return;
  3384. }
  3385. } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
  3386. xmlRegInputTokenPtr tmp;
  3387. exec->inputStackMax *= 2;
  3388. tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
  3389. exec->inputStackMax * sizeof(xmlRegInputToken));
  3390. if (tmp == NULL) {
  3391. xmlRegexpErrMemory(NULL, "pushing input string");
  3392. exec->inputStackMax /= 2;
  3393. return;
  3394. }
  3395. exec->inputStack = tmp;
  3396. }
  3397. exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
  3398. exec->inputStack[exec->inputStackNr].data = data;
  3399. exec->inputStackNr++;
  3400. exec->inputStack[exec->inputStackNr].value = NULL;
  3401. exec->inputStack[exec->inputStackNr].data = NULL;
  3402. }
  3403. /**
  3404. * xmlRegStrEqualWildcard:
  3405. * @expStr: the string to be evaluated
  3406. * @valStr: the validation string
  3407. *
  3408. * Checks if both strings are equal or have the same content. "*"
  3409. * can be used as a wildcard in @valStr; "|" is used as a seperator of
  3410. * substrings in both @expStr and @valStr.
  3411. *
  3412. * Returns 1 if the comparison is satisfied and the number of substrings
  3413. * is equal, 0 otherwise.
  3414. */
  3415. static int
  3416. xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
  3417. if (expStr == valStr) return(1);
  3418. if (expStr == NULL) return(0);
  3419. if (valStr == NULL) return(0);
  3420. do {
  3421. /*
  3422. * Eval if we have a wildcard for the current item.
  3423. */
  3424. if (*expStr != *valStr) {
  3425. /* if one of them starts with a wildcard make valStr be it */
  3426. if (*valStr == '*') {
  3427. const xmlChar *tmp;
  3428. tmp = valStr;
  3429. valStr = expStr;
  3430. expStr = tmp;
  3431. }
  3432. if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
  3433. do {
  3434. if (*valStr == XML_REG_STRING_SEPARATOR)
  3435. break;
  3436. valStr++;
  3437. } while (*valStr != 0);
  3438. continue;
  3439. } else
  3440. return(0);
  3441. }
  3442. expStr++;
  3443. valStr++;
  3444. } while (*valStr != 0);
  3445. if (*expStr != 0)
  3446. return (0);
  3447. else
  3448. return (1);
  3449. }
  3450. /**
  3451. * xmlRegCompactPushString:
  3452. * @exec: a regexp execution context
  3453. * @comp: the precompiled exec with a compact table
  3454. * @value: a string token input
  3455. * @data: data associated to the token to reuse in callbacks
  3456. *
  3457. * Push one input token in the execution context
  3458. *
  3459. * Returns: 1 if the regexp reached a final state, 0 if non-final, and
  3460. * a negative value in case of error.
  3461. */
  3462. static int
  3463. xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
  3464. xmlRegexpPtr comp,
  3465. const xmlChar *value,
  3466. void *data) {
  3467. int state = exec->index;
  3468. int i, target;
  3469. if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
  3470. return(-1);
  3471. if (value == NULL) {
  3472. /*
  3473. * are we at a final state ?
  3474. */
  3475. if (comp->compact[state * (comp->nbstrings + 1)] ==
  3476. XML_REGEXP_FINAL_STATE)
  3477. return(1);
  3478. return(0);
  3479. }
  3480. #ifdef DEBUG_PUSH
  3481. printf("value pushed: %s\n", value);
  3482. #endif
  3483. /*
  3484. * Examine all outside transitions from current state
  3485. */
  3486. for (i = 0;i < comp->nbstrings;i++) {
  3487. target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
  3488. if ((target > 0) && (target <= comp->nbstates)) {
  3489. target--; /* to avoid 0 */
  3490. if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
  3491. exec->index = target;
  3492. if ((exec->callback != NULL) && (comp->transdata != NULL)) {
  3493. exec->callback(exec->data, value,
  3494. comp->transdata[state * comp->nbstrings + i], data);
  3495. }
  3496. #ifdef DEBUG_PUSH
  3497. printf("entering state %d\n", target);
  3498. #endif
  3499. if (comp->compact[target * (comp->nbstrings + 1)] ==
  3500. XML_REGEXP_SINK_STATE)
  3501. goto error;
  3502. if (comp->compact[target * (comp->nbstrings + 1)] ==
  3503. XML_REGEXP_FINAL_STATE)
  3504. return(1);
  3505. return(0);
  3506. }
  3507. }
  3508. }
  3509. /*
  3510. * Failed to find an exit transition out from current state for the
  3511. * current token
  3512. */
  3513. #ifdef DEBUG_PUSH
  3514. printf("failed to find a transition for %s on state %d\n", value, state);
  3515. #endif
  3516. error:
  3517. if (exec->errString != NULL)
  3518. xmlFree(exec->errString);
  3519. exec->errString = xmlStrdup(value);
  3520. exec->errStateNo = state;
  3521. exec->status = -1;
  3522. #ifdef DEBUG_ERR
  3523. testerr(exec);
  3524. #endif
  3525. return(-1);
  3526. }
  3527. /**
  3528. * xmlRegExecPushStringInternal:
  3529. * @exec: a regexp execution context or NULL to indicate the end
  3530. * @value: a string token input
  3531. * @data: data associated to the token to reuse in callbacks
  3532. * @compound: value was assembled from 2 strings
  3533. *
  3534. * Push one input token in the execution context
  3535. *
  3536. * Returns: 1 if the regexp reached a final state, 0 if non-final, and
  3537. * a negative value in case of error.
  3538. */
  3539. static int
  3540. xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
  3541. void *data, int compound) {
  3542. xmlRegTransPtr trans;
  3543. xmlRegAtomPtr atom;
  3544. int ret;
  3545. int final = 0;
  3546. int progress = 1;
  3547. if (exec == NULL)
  3548. return(-1);
  3549. if (exec->comp == NULL)
  3550. return(-1);
  3551. if (exec->status != 0)
  3552. return(exec->status);
  3553. if (exec->comp->compact != NULL)
  3554. return(xmlRegCompactPushString(exec, exec->comp, value, data));
  3555. if (value == NULL) {
  3556. if (exec->state->type == XML_REGEXP_FINAL_STATE)
  3557. return(1);
  3558. final = 1;
  3559. }
  3560. #ifdef DEBUG_PUSH
  3561. printf("value pushed: %s\n", value);
  3562. #endif
  3563. /*
  3564. * If we have an active rollback stack push the new value there
  3565. * and get back to where we were left
  3566. */
  3567. if ((value != NULL) && (exec->inputStackNr > 0)) {
  3568. xmlFARegExecSaveInputString(exec, value, data);
  3569. value = exec->inputStack[exec->index].value;
  3570. data = exec->inputStack[exec->index].data;
  3571. #ifdef DEBUG_PUSH
  3572. printf("value loaded: %s\n", value);
  3573. #endif
  3574. }
  3575. while ((exec->status == 0) &&
  3576. ((value != NULL) ||
  3577. ((final == 1) &&
  3578. (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
  3579. /*
  3580. * End of input on non-terminal state, rollback, however we may
  3581. * still have epsilon like transition for counted transitions
  3582. * on counters, in that case don't break too early.
  3583. */
  3584. if ((value == NULL) && (exec->counts == NULL))
  3585. goto rollback;
  3586. exec->transcount = 0;
  3587. for (;exec->transno < exec->state->nbTrans;exec->transno++) {
  3588. trans = &exec->state->trans[exec->transno];
  3589. if (trans->to < 0)
  3590. continue;
  3591. atom = trans->atom;
  3592. ret = 0;
  3593. if (trans->count == REGEXP_ALL_LAX_COUNTER) {
  3594. int i;
  3595. int count;
  3596. xmlRegTransPtr t;
  3597. xmlRegCounterPtr counter;
  3598. ret = 0;
  3599. #ifdef DEBUG_PUSH
  3600. printf("testing all lax %d\n", trans->count);
  3601. #endif
  3602. /*
  3603. * Check all counted transitions from the current state
  3604. */
  3605. if ((value == NULL) && (final)) {
  3606. ret = 1;
  3607. } else if (value != NULL) {
  3608. for (i = 0;i < exec->state->nbTrans;i++) {
  3609. t = &exec->state->trans[i];
  3610. if ((t->counter < 0) || (t == trans))
  3611. continue;
  3612. counter = &exec->comp->counters[t->counter];
  3613. count = exec->counts[t->counter];
  3614. if ((count < counter->max) &&
  3615. (t->atom != NULL) &&
  3616. (xmlStrEqual(value, t->atom->valuep))) {
  3617. ret = 0;
  3618. break;
  3619. }
  3620. if ((count >= counter->min) &&
  3621. (count < counter->max) &&
  3622. (t->atom != NULL) &&
  3623. (xmlStrEqual(value, t->atom->valuep))) {
  3624. ret = 1;
  3625. break;
  3626. }
  3627. }
  3628. }
  3629. } else if (trans->count == REGEXP_ALL_COUNTER) {
  3630. int i;
  3631. int count;
  3632. xmlRegTransPtr t;
  3633. xmlRegCounterPtr counter;
  3634. ret = 1;
  3635. #ifdef DEBUG_PUSH
  3636. printf("testing all %d\n", trans->count);
  3637. #endif
  3638. /*
  3639. * Check all counted transitions from the current state
  3640. */
  3641. for (i = 0;i < exec->state->nbTrans;i++) {
  3642. t = &exec->state->trans[i];
  3643. if ((t->counter < 0) || (t == trans))
  3644. continue;
  3645. counter = &exec->comp->counters[t->counter];
  3646. count = exec->counts[t->counter];
  3647. if ((count < counter->min) || (count > counter->max)) {
  3648. ret = 0;
  3649. break;
  3650. }
  3651. }
  3652. } else if (trans->count >= 0) {
  3653. int count;
  3654. xmlRegCounterPtr counter;
  3655. /*
  3656. * A counted transition.
  3657. */
  3658. count = exec->counts[trans->count];
  3659. counter = &exec->comp->counters[trans->count];
  3660. #ifdef DEBUG_PUSH
  3661. printf("testing count %d: val %d, min %d, max %d\n",
  3662. trans->count, count, counter->min, counter->max);
  3663. #endif
  3664. ret = ((count >= counter->min) && (count <= counter->max));
  3665. } else if (atom == NULL) {
  3666. fprintf(stderr, "epsilon transition left at runtime\n");
  3667. exec->status = -2;
  3668. break;
  3669. } else if (value != NULL) {
  3670. ret = xmlRegStrEqualWildcard(atom->valuep, value);
  3671. if (atom->neg) {
  3672. ret = !ret;
  3673. if (!compound)
  3674. ret = 0;
  3675. }
  3676. if ((ret == 1) && (trans->counter >= 0)) {
  3677. xmlRegCounterPtr counter;
  3678. int count;
  3679. count = exec->counts[trans->counter];
  3680. counter = &exec->comp->counters[trans->counter];
  3681. if (count >= counter->max)
  3682. ret = 0;
  3683. }
  3684. if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
  3685. xmlRegStatePtr to = exec->comp->states[trans->to];
  3686. /*
  3687. * this is a multiple input sequence
  3688. */
  3689. if (exec->state->nbTrans > exec->transno + 1) {
  3690. if (exec->inputStackNr <= 0) {
  3691. xmlFARegExecSaveInputString(exec, value, data);
  3692. }
  3693. xmlFARegExecSave(exec);
  3694. }
  3695. exec->transcount = 1;
  3696. do {
  3697. /*
  3698. * Try to progress as much as possible on the input
  3699. */
  3700. if (exec->transcount == atom->max) {
  3701. break;
  3702. }
  3703. exec->index++;
  3704. value = exec->inputStack[exec->index].value;
  3705. data = exec->inputStack[exec->index].data;
  3706. #ifdef DEBUG_PUSH
  3707. printf("value loaded: %s\n", value);
  3708. #endif
  3709. /*
  3710. * End of input: stop here
  3711. */
  3712. if (value == NULL) {
  3713. exec->index --;
  3714. break;
  3715. }
  3716. if (exec->transcount >= atom->min) {
  3717. int transno = exec->transno;
  3718. xmlRegStatePtr state = exec->state;
  3719. /*
  3720. * The transition is acceptable save it
  3721. */
  3722. exec->transno = -1; /* trick */
  3723. exec->state = to;
  3724. if (exec->inputStackNr <= 0) {
  3725. xmlFARegExecSaveInputString(exec, value, data);
  3726. }
  3727. xmlFARegExecSave(exec);
  3728. exec->transno = transno;
  3729. exec->state = state;
  3730. }
  3731. ret = xmlStrEqual(value, atom->valuep);
  3732. exec->transcount++;
  3733. } while (ret == 1);
  3734. if (exec->transcount < atom->min)
  3735. ret = 0;
  3736. /*
  3737. * If the last check failed but one transition was found
  3738. * possible, rollback
  3739. */
  3740. if (ret < 0)
  3741. ret = 0;
  3742. if (ret == 0) {
  3743. goto rollback;
  3744. }
  3745. }
  3746. }
  3747. if (ret == 1) {
  3748. if ((exec->callback != NULL) && (atom != NULL) &&
  3749. (data != NULL)) {
  3750. exec->callback(exec->data, atom->valuep,
  3751. atom->data, data);
  3752. }
  3753. if (exec->state->nbTrans > exec->transno + 1) {
  3754. if (exec->inputStackNr <= 0) {
  3755. xmlFARegExecSaveInputString(exec, value, data);
  3756. }
  3757. xmlFARegExecSave(exec);
  3758. }
  3759. if (trans->counter >= 0) {
  3760. #ifdef DEBUG_PUSH
  3761. printf("Increasing count %d\n", trans->counter);
  3762. #endif
  3763. exec->counts[trans->counter]++;
  3764. }
  3765. if ((trans->count >= 0) &&
  3766. (trans->count < REGEXP_ALL_COUNTER)) {
  3767. #ifdef DEBUG_REGEXP_EXEC
  3768. printf("resetting count %d on transition\n",
  3769. trans->count);
  3770. #endif
  3771. exec->counts[trans->count] = 0;
  3772. }
  3773. #ifdef DEBUG_PUSH
  3774. printf("entering state %d\n", trans->to);
  3775. #endif
  3776. if ((exec->comp->states[trans->to] != NULL) &&
  3777. (exec->comp->states[trans->to]->type ==
  3778. XML_REGEXP_SINK_STATE)) {
  3779. /*
  3780. * entering a sink state, save the current state as error
  3781. * state.
  3782. */
  3783. if (exec->errString != NULL)
  3784. xmlFree(exec->errString);
  3785. exec->errString = xmlStrdup(value);
  3786. exec->errState = exec->state;
  3787. memcpy(exec->errCounts, exec->counts,
  3788. exec->comp->nbCounters * sizeof(int));
  3789. }
  3790. exec->state = exec->comp->states[trans->to];
  3791. exec->transno = 0;
  3792. if (trans->atom != NULL) {
  3793. if (exec->inputStack != NULL) {
  3794. exec->index++;
  3795. if (exec->index < exec->inputStackNr) {
  3796. value = exec->inputStack[exec->index].value;
  3797. data = exec->inputStack[exec->index].data;
  3798. #ifdef DEBUG_PUSH
  3799. printf("value loaded: %s\n", value);
  3800. #endif
  3801. } else {
  3802. value = NULL;
  3803. data = NULL;
  3804. #ifdef DEBUG_PUSH
  3805. printf("end of input\n");
  3806. #endif
  3807. }
  3808. } else {
  3809. value = NULL;
  3810. data = NULL;
  3811. #ifdef DEBUG_PUSH
  3812. printf("end of input\n");
  3813. #endif
  3814. }
  3815. }
  3816. goto progress;
  3817. } else if (ret < 0) {
  3818. exec->status = -4;
  3819. break;
  3820. }
  3821. }
  3822. if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
  3823. rollback:
  3824. /*
  3825. * if we didn't yet rollback on the current input
  3826. * store the current state as the error state.
  3827. */
  3828. if ((progress) && (exec->state != NULL) &&
  3829. (exec->state->type != XML_REGEXP_SINK_STATE)) {
  3830. progress = 0;
  3831. if (exec->errString != NULL)
  3832. xmlFree(exec->errString);
  3833. exec->errString = xmlStrdup(value);
  3834. exec->errState = exec->state;
  3835. memcpy(exec->errCounts, exec->counts,
  3836. exec->comp->nbCounters * sizeof(int));
  3837. }
  3838. /*
  3839. * Failed to find a way out
  3840. */
  3841. exec->determinist = 0;
  3842. xmlFARegExecRollBack(exec);
  3843. if (exec->status == 0) {
  3844. value = exec->inputStack[exec->index].value;
  3845. data = exec->inputStack[exec->index].data;
  3846. #ifdef DEBUG_PUSH
  3847. printf("value loaded: %s\n", value);
  3848. #endif
  3849. }
  3850. }
  3851. continue;
  3852. progress:
  3853. progress = 1;
  3854. continue;
  3855. }
  3856. if (exec->status == 0) {
  3857. return(exec->state->type == XML_REGEXP_FINAL_STATE);
  3858. }
  3859. #ifdef DEBUG_ERR
  3860. if (exec->status < 0) {
  3861. testerr(exec);
  3862. }
  3863. #endif
  3864. return(exec->status);
  3865. }
  3866. /**
  3867. * xmlRegExecPushString:
  3868. * @exec: a regexp execution context or NULL to indicate the end
  3869. * @value: a string token input
  3870. * @data: data associated to the token to reuse in callbacks
  3871. *
  3872. * Push one input token in the execution context
  3873. *
  3874. * Returns: 1 if the regexp reached a final state, 0 if non-final, and
  3875. * a negative value in case of error.
  3876. */
  3877. int
  3878. xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
  3879. void *data) {
  3880. return(xmlRegExecPushStringInternal(exec, value, data, 0));
  3881. }
  3882. /**
  3883. * xmlRegExecPushString2:
  3884. * @exec: a regexp execution context or NULL to indicate the end
  3885. * @value: the first string token input
  3886. * @value2: the second string token input
  3887. * @data: data associated to the token to reuse in callbacks
  3888. *
  3889. * Push one input token in the execution context
  3890. *
  3891. * Returns: 1 if the regexp reached a final state, 0 if non-final, and
  3892. * a negative value in case of error.
  3893. */
  3894. int
  3895. xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
  3896. const xmlChar *value2, void *data) {
  3897. xmlChar buf[150];
  3898. int lenn, lenp, ret;
  3899. xmlChar *str;
  3900. if (exec == NULL)
  3901. return(-1);
  3902. if (exec->comp == NULL)
  3903. return(-1);
  3904. if (exec->status != 0)
  3905. return(exec->status);
  3906. if (value2 == NULL)
  3907. return(xmlRegExecPushString(exec, value, data));
  3908. lenn = strlen((char *) value2);
  3909. lenp = strlen((char *) value);
  3910. if (150 < lenn + lenp + 2) {
  3911. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  3912. if (str == NULL) {
  3913. exec->status = -1;
  3914. return(-1);
  3915. }
  3916. } else {
  3917. str = buf;
  3918. }
  3919. memcpy(&str[0], value, lenp);
  3920. str[lenp] = XML_REG_STRING_SEPARATOR;
  3921. memcpy(&str[lenp + 1], value2, lenn);
  3922. str[lenn + lenp + 1] = 0;
  3923. if (exec->comp->compact != NULL)
  3924. ret = xmlRegCompactPushString(exec, exec->comp, str, data);
  3925. else
  3926. ret = xmlRegExecPushStringInternal(exec, str, data, 1);
  3927. if (str != buf)
  3928. xmlFree(str);
  3929. return(ret);
  3930. }
  3931. /**
  3932. * xmlRegExecGetValues:
  3933. * @exec: a regexp execution context
  3934. * @err: error extraction or normal one
  3935. * @nbval: pointer to the number of accepted values IN/OUT
  3936. * @nbneg: return number of negative transitions
  3937. * @values: pointer to the array of acceptable values
  3938. * @terminal: return value if this was a terminal state
  3939. *
  3940. * Extract informations from the regexp execution, internal routine to
  3941. * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
  3942. *
  3943. * Returns: 0 in case of success or -1 in case of error.
  3944. */
  3945. static int
  3946. xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
  3947. int *nbval, int *nbneg,
  3948. xmlChar **values, int *terminal) {
  3949. int maxval;
  3950. int nb = 0;
  3951. if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
  3952. (values == NULL) || (*nbval <= 0))
  3953. return(-1);
  3954. maxval = *nbval;
  3955. *nbval = 0;
  3956. *nbneg = 0;
  3957. if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
  3958. xmlRegexpPtr comp;
  3959. int target, i, state;
  3960. comp = exec->comp;
  3961. if (err) {
  3962. if (exec->errStateNo == -1) return(-1);
  3963. state = exec->errStateNo;
  3964. } else {
  3965. state = exec->index;
  3966. }
  3967. if (terminal != NULL) {
  3968. if (comp->compact[state * (comp->nbstrings + 1)] ==
  3969. XML_REGEXP_FINAL_STATE)
  3970. *terminal = 1;
  3971. else
  3972. *terminal = 0;
  3973. }
  3974. for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
  3975. target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
  3976. if ((target > 0) && (target <= comp->nbstates) &&
  3977. (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
  3978. XML_REGEXP_SINK_STATE)) {
  3979. values[nb++] = comp->stringMap[i];
  3980. (*nbval)++;
  3981. }
  3982. }
  3983. for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
  3984. target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
  3985. if ((target > 0) && (target <= comp->nbstates) &&
  3986. (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
  3987. XML_REGEXP_SINK_STATE)) {
  3988. values[nb++] = comp->stringMap[i];
  3989. (*nbneg)++;
  3990. }
  3991. }
  3992. } else {
  3993. int transno;
  3994. xmlRegTransPtr trans;
  3995. xmlRegAtomPtr atom;
  3996. xmlRegStatePtr state;
  3997. if (terminal != NULL) {
  3998. if (exec->state->type == XML_REGEXP_FINAL_STATE)
  3999. *terminal = 1;
  4000. else
  4001. *terminal = 0;
  4002. }
  4003. if (err) {
  4004. if (exec->errState == NULL) return(-1);
  4005. state = exec->errState;
  4006. } else {
  4007. if (exec->state == NULL) return(-1);
  4008. state = exec->state;
  4009. }
  4010. for (transno = 0;
  4011. (transno < state->nbTrans) && (nb < maxval);
  4012. transno++) {
  4013. trans = &state->trans[transno];
  4014. if (trans->to < 0)
  4015. continue;
  4016. atom = trans->atom;
  4017. if ((atom == NULL) || (atom->valuep == NULL))
  4018. continue;
  4019. if (trans->count == REGEXP_ALL_LAX_COUNTER) {
  4020. /* this should not be reached but ... */
  4021. TODO;
  4022. } else if (trans->count == REGEXP_ALL_COUNTER) {
  4023. /* this should not be reached but ... */
  4024. TODO;
  4025. } else if (trans->counter >= 0) {
  4026. xmlRegCounterPtr counter = NULL;
  4027. int count;
  4028. if (err)
  4029. count = exec->errCounts[trans->counter];
  4030. else
  4031. count = exec->counts[trans->counter];
  4032. if (exec->comp != NULL)
  4033. counter = &exec->comp->counters[trans->counter];
  4034. if ((counter == NULL) || (count < counter->max)) {
  4035. if (atom->neg)
  4036. values[nb++] = (xmlChar *) atom->valuep2;
  4037. else
  4038. values[nb++] = (xmlChar *) atom->valuep;
  4039. (*nbval)++;
  4040. }
  4041. } else {
  4042. if ((exec->comp->states[trans->to] != NULL) &&
  4043. (exec->comp->states[trans->to]->type !=
  4044. XML_REGEXP_SINK_STATE)) {
  4045. if (atom->neg)
  4046. values[nb++] = (xmlChar *) atom->valuep2;
  4047. else
  4048. values[nb++] = (xmlChar *) atom->valuep;
  4049. (*nbval)++;
  4050. }
  4051. }
  4052. }
  4053. for (transno = 0;
  4054. (transno < state->nbTrans) && (nb < maxval);
  4055. transno++) {
  4056. trans = &state->trans[transno];
  4057. if (trans->to < 0)
  4058. continue;
  4059. atom = trans->atom;
  4060. if ((atom == NULL) || (atom->valuep == NULL))
  4061. continue;
  4062. if (trans->count == REGEXP_ALL_LAX_COUNTER) {
  4063. continue;
  4064. } else if (trans->count == REGEXP_ALL_COUNTER) {
  4065. continue;
  4066. } else if (trans->counter >= 0) {
  4067. continue;
  4068. } else {
  4069. if ((exec->comp->states[trans->to] != NULL) &&
  4070. (exec->comp->states[trans->to]->type ==
  4071. XML_REGEXP_SINK_STATE)) {
  4072. if (atom->neg)
  4073. values[nb++] = (xmlChar *) atom->valuep2;
  4074. else
  4075. values[nb++] = (xmlChar *) atom->valuep;
  4076. (*nbneg)++;
  4077. }
  4078. }
  4079. }
  4080. }
  4081. return(0);
  4082. }
  4083. /**
  4084. * xmlRegExecNextValues:
  4085. * @exec: a regexp execution context
  4086. * @nbval: pointer to the number of accepted values IN/OUT
  4087. * @nbneg: return number of negative transitions
  4088. * @values: pointer to the array of acceptable values
  4089. * @terminal: return value if this was a terminal state
  4090. *
  4091. * Extract informations from the regexp execution,
  4092. * the parameter @values must point to an array of @nbval string pointers
  4093. * on return nbval will contain the number of possible strings in that
  4094. * state and the @values array will be updated with them. The string values
  4095. * returned will be freed with the @exec context and don't need to be
  4096. * deallocated.
  4097. *
  4098. * Returns: 0 in case of success or -1 in case of error.
  4099. */
  4100. int
  4101. xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
  4102. xmlChar **values, int *terminal) {
  4103. return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
  4104. }
  4105. /**
  4106. * xmlRegExecErrInfo:
  4107. * @exec: a regexp execution context generating an error
  4108. * @string: return value for the error string
  4109. * @nbval: pointer to the number of accepted values IN/OUT
  4110. * @nbneg: return number of negative transitions
  4111. * @values: pointer to the array of acceptable values
  4112. * @terminal: return value if this was a terminal state
  4113. *
  4114. * Extract error informations from the regexp execution, the parameter
  4115. * @string will be updated with the value pushed and not accepted,
  4116. * the parameter @values must point to an array of @nbval string pointers
  4117. * on return nbval will contain the number of possible strings in that
  4118. * state and the @values array will be updated with them. The string values
  4119. * returned will be freed with the @exec context and don't need to be
  4120. * deallocated.
  4121. *
  4122. * Returns: 0 in case of success or -1 in case of error.
  4123. */
  4124. int
  4125. xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
  4126. int *nbval, int *nbneg, xmlChar **values, int *terminal) {
  4127. if (exec == NULL)
  4128. return(-1);
  4129. if (string != NULL) {
  4130. if (exec->status != 0)
  4131. *string = exec->errString;
  4132. else
  4133. *string = NULL;
  4134. }
  4135. return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
  4136. }
  4137. #ifdef DEBUG_ERR
  4138. static void testerr(xmlRegExecCtxtPtr exec) {
  4139. const xmlChar *string;
  4140. xmlChar *values[5];
  4141. int nb = 5;
  4142. int nbneg;
  4143. int terminal;
  4144. xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
  4145. }
  4146. #endif
  4147. #if 0
  4148. static int
  4149. xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
  4150. xmlRegTransPtr trans;
  4151. xmlRegAtomPtr atom;
  4152. int ret;
  4153. int codepoint, len;
  4154. if (exec == NULL)
  4155. return(-1);
  4156. if (exec->status != 0)
  4157. return(exec->status);
  4158. while ((exec->status == 0) &&
  4159. ((exec->inputString[exec->index] != 0) ||
  4160. (exec->state->type != XML_REGEXP_FINAL_STATE))) {
  4161. /*
  4162. * End of input on non-terminal state, rollback, however we may
  4163. * still have epsilon like transition for counted transitions
  4164. * on counters, in that case don't break too early.
  4165. */
  4166. if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
  4167. goto rollback;
  4168. exec->transcount = 0;
  4169. for (;exec->transno < exec->state->nbTrans;exec->transno++) {
  4170. trans = &exec->state->trans[exec->transno];
  4171. if (trans->to < 0)
  4172. continue;
  4173. atom = trans->atom;
  4174. ret = 0;
  4175. if (trans->count >= 0) {
  4176. int count;
  4177. xmlRegCounterPtr counter;
  4178. /*
  4179. * A counted transition.
  4180. */
  4181. count = exec->counts[trans->count];
  4182. counter = &exec->comp->counters[trans->count];
  4183. #ifdef DEBUG_REGEXP_EXEC
  4184. printf("testing count %d: val %d, min %d, max %d\n",
  4185. trans->count, count, counter->min, counter->max);
  4186. #endif
  4187. ret = ((count >= counter->min) && (count <= counter->max));
  4188. } else if (atom == NULL) {
  4189. fprintf(stderr, "epsilon transition left at runtime\n");
  4190. exec->status = -2;
  4191. break;
  4192. } else if (exec->inputString[exec->index] != 0) {
  4193. codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
  4194. ret = xmlRegCheckCharacter(atom, codepoint);
  4195. if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
  4196. xmlRegStatePtr to = exec->comp->states[trans->to];
  4197. /*
  4198. * this is a multiple input sequence
  4199. */
  4200. if (exec->state->nbTrans > exec->transno + 1) {
  4201. xmlFARegExecSave(exec);
  4202. }
  4203. exec->transcount = 1;
  4204. do {
  4205. /*
  4206. * Try to progress as much as possible on the input
  4207. */
  4208. if (exec->transcount == atom->max) {
  4209. break;
  4210. }
  4211. exec->index += len;
  4212. /*
  4213. * End of input: stop here
  4214. */
  4215. if (exec->inputString[exec->index] == 0) {
  4216. exec->index -= len;
  4217. break;
  4218. }
  4219. if (exec->transcount >= atom->min) {
  4220. int transno = exec->transno;
  4221. xmlRegStatePtr state = exec->state;
  4222. /*
  4223. * The transition is acceptable save it
  4224. */
  4225. exec->transno = -1; /* trick */
  4226. exec->state = to;
  4227. xmlFARegExecSave(exec);
  4228. exec->transno = transno;
  4229. exec->state = state;
  4230. }
  4231. codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
  4232. len);
  4233. ret = xmlRegCheckCharacter(atom, codepoint);
  4234. exec->transcount++;
  4235. } while (ret == 1);
  4236. if (exec->transcount < atom->min)
  4237. ret = 0;
  4238. /*
  4239. * If the last check failed but one transition was found
  4240. * possible, rollback
  4241. */
  4242. if (ret < 0)
  4243. ret = 0;
  4244. if (ret == 0) {
  4245. goto rollback;
  4246. }
  4247. }
  4248. }
  4249. if (ret == 1) {
  4250. if (exec->state->nbTrans > exec->transno + 1) {
  4251. xmlFARegExecSave(exec);
  4252. }
  4253. /*
  4254. * restart count for expressions like this ((abc){2})*
  4255. */
  4256. if (trans->count >= 0) {
  4257. #ifdef DEBUG_REGEXP_EXEC
  4258. printf("Reset count %d\n", trans->count);
  4259. #endif
  4260. exec->counts[trans->count] = 0;
  4261. }
  4262. if (trans->counter >= 0) {
  4263. #ifdef DEBUG_REGEXP_EXEC
  4264. printf("Increasing count %d\n", trans->counter);
  4265. #endif
  4266. exec->counts[trans->counter]++;
  4267. }
  4268. #ifdef DEBUG_REGEXP_EXEC
  4269. printf("entering state %d\n", trans->to);
  4270. #endif
  4271. exec->state = exec->comp->states[trans->to];
  4272. exec->transno = 0;
  4273. if (trans->atom != NULL) {
  4274. exec->index += len;
  4275. }
  4276. goto progress;
  4277. } else if (ret < 0) {
  4278. exec->status = -4;
  4279. break;
  4280. }
  4281. }
  4282. if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
  4283. rollback:
  4284. /*
  4285. * Failed to find a way out
  4286. */
  4287. exec->determinist = 0;
  4288. xmlFARegExecRollBack(exec);
  4289. }
  4290. progress:
  4291. continue;
  4292. }
  4293. }
  4294. #endif
  4295. /************************************************************************
  4296. * *
  4297. * Parser for the Schemas Datatype Regular Expressions *
  4298. * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
  4299. * *
  4300. ************************************************************************/
  4301. /**
  4302. * xmlFAIsChar:
  4303. * @ctxt: a regexp parser context
  4304. *
  4305. * [10] Char ::= [^.\?*+()|#x5B#x5D]
  4306. */
  4307. static int
  4308. xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
  4309. int cur;
  4310. int len;
  4311. cur = CUR_SCHAR(ctxt->cur, len);
  4312. if ((cur == '.') || (cur == '\\') || (cur == '?') ||
  4313. (cur == '*') || (cur == '+') || (cur == '(') ||
  4314. (cur == ')') || (cur == '|') || (cur == 0x5B) ||
  4315. (cur == 0x5D) || (cur == 0))
  4316. return(-1);
  4317. return(cur);
  4318. }
  4319. /**
  4320. * xmlFAParseCharProp:
  4321. * @ctxt: a regexp parser context
  4322. *
  4323. * [27] charProp ::= IsCategory | IsBlock
  4324. * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
  4325. * Separators | Symbols | Others
  4326. * [29] Letters ::= 'L' [ultmo]?
  4327. * [30] Marks ::= 'M' [nce]?
  4328. * [31] Numbers ::= 'N' [dlo]?
  4329. * [32] Punctuation ::= 'P' [cdseifo]?
  4330. * [33] Separators ::= 'Z' [slp]?
  4331. * [34] Symbols ::= 'S' [mcko]?
  4332. * [35] Others ::= 'C' [cfon]?
  4333. * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
  4334. */
  4335. static void
  4336. xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
  4337. int cur;
  4338. xmlRegAtomType type = (xmlRegAtomType) 0;
  4339. xmlChar *blockName = NULL;
  4340. cur = CUR;
  4341. if (cur == 'L') {
  4342. NEXT;
  4343. cur = CUR;
  4344. if (cur == 'u') {
  4345. NEXT;
  4346. type = XML_REGEXP_LETTER_UPPERCASE;
  4347. } else if (cur == 'l') {
  4348. NEXT;
  4349. type = XML_REGEXP_LETTER_LOWERCASE;
  4350. } else if (cur == 't') {
  4351. NEXT;
  4352. type = XML_REGEXP_LETTER_TITLECASE;
  4353. } else if (cur == 'm') {
  4354. NEXT;
  4355. type = XML_REGEXP_LETTER_MODIFIER;
  4356. } else if (cur == 'o') {
  4357. NEXT;
  4358. type = XML_REGEXP_LETTER_OTHERS;
  4359. } else {
  4360. type = XML_REGEXP_LETTER;
  4361. }
  4362. } else if (cur == 'M') {
  4363. NEXT;
  4364. cur = CUR;
  4365. if (cur == 'n') {
  4366. NEXT;
  4367. /* nonspacing */
  4368. type = XML_REGEXP_MARK_NONSPACING;
  4369. } else if (cur == 'c') {
  4370. NEXT;
  4371. /* spacing combining */
  4372. type = XML_REGEXP_MARK_SPACECOMBINING;
  4373. } else if (cur == 'e') {
  4374. NEXT;
  4375. /* enclosing */
  4376. type = XML_REGEXP_MARK_ENCLOSING;
  4377. } else {
  4378. /* all marks */
  4379. type = XML_REGEXP_MARK;
  4380. }
  4381. } else if (cur == 'N') {
  4382. NEXT;
  4383. cur = CUR;
  4384. if (cur == 'd') {
  4385. NEXT;
  4386. /* digital */
  4387. type = XML_REGEXP_NUMBER_DECIMAL;
  4388. } else if (cur == 'l') {
  4389. NEXT;
  4390. /* letter */
  4391. type = XML_REGEXP_NUMBER_LETTER;
  4392. } else if (cur == 'o') {
  4393. NEXT;
  4394. /* other */
  4395. type = XML_REGEXP_NUMBER_OTHERS;
  4396. } else {
  4397. /* all numbers */
  4398. type = XML_REGEXP_NUMBER;
  4399. }
  4400. } else if (cur == 'P') {
  4401. NEXT;
  4402. cur = CUR;
  4403. if (cur == 'c') {
  4404. NEXT;
  4405. /* connector */
  4406. type = XML_REGEXP_PUNCT_CONNECTOR;
  4407. } else if (cur == 'd') {
  4408. NEXT;
  4409. /* dash */
  4410. type = XML_REGEXP_PUNCT_DASH;
  4411. } else if (cur == 's') {
  4412. NEXT;
  4413. /* open */
  4414. type = XML_REGEXP_PUNCT_OPEN;
  4415. } else if (cur == 'e') {
  4416. NEXT;
  4417. /* close */
  4418. type = XML_REGEXP_PUNCT_CLOSE;
  4419. } else if (cur == 'i') {
  4420. NEXT;
  4421. /* initial quote */
  4422. type = XML_REGEXP_PUNCT_INITQUOTE;
  4423. } else if (cur == 'f') {
  4424. NEXT;
  4425. /* final quote */
  4426. type = XML_REGEXP_PUNCT_FINQUOTE;
  4427. } else if (cur == 'o') {
  4428. NEXT;
  4429. /* other */
  4430. type = XML_REGEXP_PUNCT_OTHERS;
  4431. } else {
  4432. /* all punctuation */
  4433. type = XML_REGEXP_PUNCT;
  4434. }
  4435. } else if (cur == 'Z') {
  4436. NEXT;
  4437. cur = CUR;
  4438. if (cur == 's') {
  4439. NEXT;
  4440. /* space */
  4441. type = XML_REGEXP_SEPAR_SPACE;
  4442. } else if (cur == 'l') {
  4443. NEXT;
  4444. /* line */
  4445. type = XML_REGEXP_SEPAR_LINE;
  4446. } else if (cur == 'p') {
  4447. NEXT;
  4448. /* paragraph */
  4449. type = XML_REGEXP_SEPAR_PARA;
  4450. } else {
  4451. /* all separators */
  4452. type = XML_REGEXP_SEPAR;
  4453. }
  4454. } else if (cur == 'S') {
  4455. NEXT;
  4456. cur = CUR;
  4457. if (cur == 'm') {
  4458. NEXT;
  4459. type = XML_REGEXP_SYMBOL_MATH;
  4460. /* math */
  4461. } else if (cur == 'c') {
  4462. NEXT;
  4463. type = XML_REGEXP_SYMBOL_CURRENCY;
  4464. /* currency */
  4465. } else if (cur == 'k') {
  4466. NEXT;
  4467. type = XML_REGEXP_SYMBOL_MODIFIER;
  4468. /* modifiers */
  4469. } else if (cur == 'o') {
  4470. NEXT;
  4471. type = XML_REGEXP_SYMBOL_OTHERS;
  4472. /* other */
  4473. } else {
  4474. /* all symbols */
  4475. type = XML_REGEXP_SYMBOL;
  4476. }
  4477. } else if (cur == 'C') {
  4478. NEXT;
  4479. cur = CUR;
  4480. if (cur == 'c') {
  4481. NEXT;
  4482. /* control */
  4483. type = XML_REGEXP_OTHER_CONTROL;
  4484. } else if (cur == 'f') {
  4485. NEXT;
  4486. /* format */
  4487. type = XML_REGEXP_OTHER_FORMAT;
  4488. } else if (cur == 'o') {
  4489. NEXT;
  4490. /* private use */
  4491. type = XML_REGEXP_OTHER_PRIVATE;
  4492. } else if (cur == 'n') {
  4493. NEXT;
  4494. /* not assigned */
  4495. type = XML_REGEXP_OTHER_NA;
  4496. } else {
  4497. /* all others */
  4498. type = XML_REGEXP_OTHER;
  4499. }
  4500. } else if (cur == 'I') {
  4501. const xmlChar *start;
  4502. NEXT;
  4503. cur = CUR;
  4504. if (cur != 's') {
  4505. ERROR("IsXXXX expected");
  4506. return;
  4507. }
  4508. NEXT;
  4509. start = ctxt->cur;
  4510. cur = CUR;
  4511. if (((cur >= 'a') && (cur <= 'z')) ||
  4512. ((cur >= 'A') && (cur <= 'Z')) ||
  4513. ((cur >= '0') && (cur <= '9')) ||
  4514. (cur == 0x2D)) {
  4515. NEXT;
  4516. cur = CUR;
  4517. while (((cur >= 'a') && (cur <= 'z')) ||
  4518. ((cur >= 'A') && (cur <= 'Z')) ||
  4519. ((cur >= '0') && (cur <= '9')) ||
  4520. (cur == 0x2D)) {
  4521. NEXT;
  4522. cur = CUR;
  4523. }
  4524. }
  4525. type = XML_REGEXP_BLOCK_NAME;
  4526. blockName = xmlStrndup(start, ctxt->cur - start);
  4527. } else {
  4528. ERROR("Unknown char property");
  4529. return;
  4530. }
  4531. if (ctxt->atom == NULL) {
  4532. ctxt->atom = xmlRegNewAtom(ctxt, type);
  4533. if (ctxt->atom != NULL)
  4534. ctxt->atom->valuep = blockName;
  4535. } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
  4536. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4537. type, 0, 0, blockName);
  4538. }
  4539. }
  4540. /**
  4541. * xmlFAParseCharClassEsc:
  4542. * @ctxt: a regexp parser context
  4543. *
  4544. * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
  4545. * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
  4546. * [25] catEsc ::= '\p{' charProp '}'
  4547. * [26] complEsc ::= '\P{' charProp '}'
  4548. * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
  4549. */
  4550. static void
  4551. xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
  4552. int cur;
  4553. if (CUR == '.') {
  4554. if (ctxt->atom == NULL) {
  4555. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
  4556. } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
  4557. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4558. XML_REGEXP_ANYCHAR, 0, 0, NULL);
  4559. }
  4560. NEXT;
  4561. return;
  4562. }
  4563. if (CUR != '\\') {
  4564. ERROR("Escaped sequence: expecting \\");
  4565. return;
  4566. }
  4567. NEXT;
  4568. cur = CUR;
  4569. if (cur == 'p') {
  4570. NEXT;
  4571. if (CUR != '{') {
  4572. ERROR("Expecting '{'");
  4573. return;
  4574. }
  4575. NEXT;
  4576. xmlFAParseCharProp(ctxt);
  4577. if (CUR != '}') {
  4578. ERROR("Expecting '}'");
  4579. return;
  4580. }
  4581. NEXT;
  4582. } else if (cur == 'P') {
  4583. NEXT;
  4584. if (CUR != '{') {
  4585. ERROR("Expecting '{'");
  4586. return;
  4587. }
  4588. NEXT;
  4589. xmlFAParseCharProp(ctxt);
  4590. ctxt->atom->neg = 1;
  4591. if (CUR != '}') {
  4592. ERROR("Expecting '}'");
  4593. return;
  4594. }
  4595. NEXT;
  4596. } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
  4597. (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
  4598. (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
  4599. (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
  4600. (cur == 0x5E)) {
  4601. if (ctxt->atom == NULL) {
  4602. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
  4603. if (ctxt->atom != NULL) {
  4604. switch (cur) {
  4605. case 'n':
  4606. ctxt->atom->codepoint = '\n';
  4607. break;
  4608. case 'r':
  4609. ctxt->atom->codepoint = '\r';
  4610. break;
  4611. case 't':
  4612. ctxt->atom->codepoint = '\t';
  4613. break;
  4614. default:
  4615. ctxt->atom->codepoint = cur;
  4616. }
  4617. }
  4618. } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
  4619. switch (cur) {
  4620. case 'n':
  4621. cur = '\n';
  4622. break;
  4623. case 'r':
  4624. cur = '\r';
  4625. break;
  4626. case 't':
  4627. cur = '\t';
  4628. break;
  4629. }
  4630. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4631. XML_REGEXP_CHARVAL, cur, cur, NULL);
  4632. }
  4633. NEXT;
  4634. } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
  4635. (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
  4636. (cur == 'w') || (cur == 'W')) {
  4637. xmlRegAtomType type = XML_REGEXP_ANYSPACE;
  4638. switch (cur) {
  4639. case 's':
  4640. type = XML_REGEXP_ANYSPACE;
  4641. break;
  4642. case 'S':
  4643. type = XML_REGEXP_NOTSPACE;
  4644. break;
  4645. case 'i':
  4646. type = XML_REGEXP_INITNAME;
  4647. break;
  4648. case 'I':
  4649. type = XML_REGEXP_NOTINITNAME;
  4650. break;
  4651. case 'c':
  4652. type = XML_REGEXP_NAMECHAR;
  4653. break;
  4654. case 'C':
  4655. type = XML_REGEXP_NOTNAMECHAR;
  4656. break;
  4657. case 'd':
  4658. type = XML_REGEXP_DECIMAL;
  4659. break;
  4660. case 'D':
  4661. type = XML_REGEXP_NOTDECIMAL;
  4662. break;
  4663. case 'w':
  4664. type = XML_REGEXP_REALCHAR;
  4665. break;
  4666. case 'W':
  4667. type = XML_REGEXP_NOTREALCHAR;
  4668. break;
  4669. }
  4670. NEXT;
  4671. if (ctxt->atom == NULL) {
  4672. ctxt->atom = xmlRegNewAtom(ctxt, type);
  4673. } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
  4674. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4675. type, 0, 0, NULL);
  4676. }
  4677. } else {
  4678. ERROR("Wrong escape sequence, misuse of character '\\'");
  4679. }
  4680. }
  4681. /**
  4682. * xmlFAParseCharRange:
  4683. * @ctxt: a regexp parser context
  4684. *
  4685. * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
  4686. * [18] seRange ::= charOrEsc '-' charOrEsc
  4687. * [20] charOrEsc ::= XmlChar | SingleCharEsc
  4688. * [21] XmlChar ::= [^\#x2D#x5B#x5D]
  4689. * [22] XmlCharIncDash ::= [^\#x5B#x5D]
  4690. */
  4691. static void
  4692. xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
  4693. int cur, len;
  4694. int start = -1;
  4695. int end = -1;
  4696. if (CUR == '\0') {
  4697. ERROR("Expecting ']'");
  4698. return;
  4699. }
  4700. cur = CUR;
  4701. if (cur == '\\') {
  4702. NEXT;
  4703. cur = CUR;
  4704. switch (cur) {
  4705. case 'n': start = 0xA; break;
  4706. case 'r': start = 0xD; break;
  4707. case 't': start = 0x9; break;
  4708. case '\\': case '|': case '.': case '-': case '^': case '?':
  4709. case '*': case '+': case '{': case '}': case '(': case ')':
  4710. case '[': case ']':
  4711. start = cur; break;
  4712. default:
  4713. ERROR("Invalid escape value");
  4714. return;
  4715. }
  4716. end = start;
  4717. len = 1;
  4718. } else if ((cur != 0x5B) && (cur != 0x5D)) {
  4719. end = start = CUR_SCHAR(ctxt->cur, len);
  4720. } else {
  4721. ERROR("Expecting a char range");
  4722. return;
  4723. }
  4724. /*
  4725. * Since we are "inside" a range, we can assume ctxt->cur is past
  4726. * the start of ctxt->string, and PREV should be safe
  4727. */
  4728. if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
  4729. NEXTL(len);
  4730. return;
  4731. }
  4732. NEXTL(len);
  4733. cur = CUR;
  4734. if ((cur != '-') || (NXT(1) == ']')) {
  4735. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4736. XML_REGEXP_CHARVAL, start, end, NULL);
  4737. return;
  4738. }
  4739. NEXT;
  4740. cur = CUR;
  4741. if (cur == '\\') {
  4742. NEXT;
  4743. cur = CUR;
  4744. switch (cur) {
  4745. case 'n': end = 0xA; break;
  4746. case 'r': end = 0xD; break;
  4747. case 't': end = 0x9; break;
  4748. case '\\': case '|': case '.': case '-': case '^': case '?':
  4749. case '*': case '+': case '{': case '}': case '(': case ')':
  4750. case '[': case ']':
  4751. end = cur; break;
  4752. default:
  4753. ERROR("Invalid escape value");
  4754. return;
  4755. }
  4756. len = 1;
  4757. } else if ((cur != 0x5B) && (cur != 0x5D)) {
  4758. end = CUR_SCHAR(ctxt->cur, len);
  4759. } else {
  4760. ERROR("Expecting the end of a char range");
  4761. return;
  4762. }
  4763. NEXTL(len);
  4764. /* TODO check that the values are acceptable character ranges for XML */
  4765. if (end < start) {
  4766. ERROR("End of range is before start of range");
  4767. } else {
  4768. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4769. XML_REGEXP_CHARVAL, start, end, NULL);
  4770. }
  4771. return;
  4772. }
  4773. /**
  4774. * xmlFAParsePosCharGroup:
  4775. * @ctxt: a regexp parser context
  4776. *
  4777. * [14] posCharGroup ::= ( charRange | charClassEsc )+
  4778. */
  4779. static void
  4780. xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
  4781. do {
  4782. if (CUR == '\\') {
  4783. xmlFAParseCharClassEsc(ctxt);
  4784. } else {
  4785. xmlFAParseCharRange(ctxt);
  4786. }
  4787. } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
  4788. (CUR != 0) && (ctxt->error == 0));
  4789. }
  4790. /**
  4791. * xmlFAParseCharGroup:
  4792. * @ctxt: a regexp parser context
  4793. *
  4794. * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
  4795. * [15] negCharGroup ::= '^' posCharGroup
  4796. * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
  4797. * [12] charClassExpr ::= '[' charGroup ']'
  4798. */
  4799. static void
  4800. xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
  4801. int n = ctxt->neg;
  4802. while ((CUR != ']') && (ctxt->error == 0)) {
  4803. if (CUR == '^') {
  4804. int neg = ctxt->neg;
  4805. NEXT;
  4806. ctxt->neg = !ctxt->neg;
  4807. xmlFAParsePosCharGroup(ctxt);
  4808. ctxt->neg = neg;
  4809. } else if ((CUR == '-') && (NXT(1) == '[')) {
  4810. int neg = ctxt->neg;
  4811. ctxt->neg = 2;
  4812. NEXT; /* eat the '-' */
  4813. NEXT; /* eat the '[' */
  4814. xmlFAParseCharGroup(ctxt);
  4815. if (CUR == ']') {
  4816. NEXT;
  4817. } else {
  4818. ERROR("charClassExpr: ']' expected");
  4819. break;
  4820. }
  4821. ctxt->neg = neg;
  4822. break;
  4823. } else if (CUR != ']') {
  4824. xmlFAParsePosCharGroup(ctxt);
  4825. }
  4826. }
  4827. ctxt->neg = n;
  4828. }
  4829. /**
  4830. * xmlFAParseCharClass:
  4831. * @ctxt: a regexp parser context
  4832. *
  4833. * [11] charClass ::= charClassEsc | charClassExpr
  4834. * [12] charClassExpr ::= '[' charGroup ']'
  4835. */
  4836. static void
  4837. xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
  4838. if (CUR == '[') {
  4839. NEXT;
  4840. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
  4841. if (ctxt->atom == NULL)
  4842. return;
  4843. xmlFAParseCharGroup(ctxt);
  4844. if (CUR == ']') {
  4845. NEXT;
  4846. } else {
  4847. ERROR("xmlFAParseCharClass: ']' expected");
  4848. }
  4849. } else {
  4850. xmlFAParseCharClassEsc(ctxt);
  4851. }
  4852. }
  4853. /**
  4854. * xmlFAParseQuantExact:
  4855. * @ctxt: a regexp parser context
  4856. *
  4857. * [8] QuantExact ::= [0-9]+
  4858. *
  4859. * Returns 0 if success or -1 in case of error
  4860. */
  4861. static int
  4862. xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
  4863. int ret = 0;
  4864. int ok = 0;
  4865. while ((CUR >= '0') && (CUR <= '9')) {
  4866. ret = ret * 10 + (CUR - '0');
  4867. ok = 1;
  4868. NEXT;
  4869. }
  4870. if (ok != 1) {
  4871. return(-1);
  4872. }
  4873. return(ret);
  4874. }
  4875. /**
  4876. * xmlFAParseQuantifier:
  4877. * @ctxt: a regexp parser context
  4878. *
  4879. * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
  4880. * [5] quantity ::= quantRange | quantMin | QuantExact
  4881. * [6] quantRange ::= QuantExact ',' QuantExact
  4882. * [7] quantMin ::= QuantExact ','
  4883. * [8] QuantExact ::= [0-9]+
  4884. */
  4885. static int
  4886. xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
  4887. int cur;
  4888. cur = CUR;
  4889. if ((cur == '?') || (cur == '*') || (cur == '+')) {
  4890. if (ctxt->atom != NULL) {
  4891. if (cur == '?')
  4892. ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
  4893. else if (cur == '*')
  4894. ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
  4895. else if (cur == '+')
  4896. ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
  4897. }
  4898. NEXT;
  4899. return(1);
  4900. }
  4901. if (cur == '{') {
  4902. int min = 0, max = 0;
  4903. NEXT;
  4904. cur = xmlFAParseQuantExact(ctxt);
  4905. if (cur >= 0)
  4906. min = cur;
  4907. if (CUR == ',') {
  4908. NEXT;
  4909. if (CUR == '}')
  4910. max = INT_MAX;
  4911. else {
  4912. cur = xmlFAParseQuantExact(ctxt);
  4913. if (cur >= 0)
  4914. max = cur;
  4915. else {
  4916. ERROR("Improper quantifier");
  4917. }
  4918. }
  4919. }
  4920. if (CUR == '}') {
  4921. NEXT;
  4922. } else {
  4923. ERROR("Unterminated quantifier");
  4924. }
  4925. if (max == 0)
  4926. max = min;
  4927. if (ctxt->atom != NULL) {
  4928. ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
  4929. ctxt->atom->min = min;
  4930. ctxt->atom->max = max;
  4931. }
  4932. return(1);
  4933. }
  4934. return(0);
  4935. }
  4936. /**
  4937. * xmlFAParseAtom:
  4938. * @ctxt: a regexp parser context
  4939. *
  4940. * [9] atom ::= Char | charClass | ( '(' regExp ')' )
  4941. */
  4942. static int
  4943. xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
  4944. int codepoint, len;
  4945. codepoint = xmlFAIsChar(ctxt);
  4946. if (codepoint > 0) {
  4947. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
  4948. if (ctxt->atom == NULL)
  4949. return(-1);
  4950. codepoint = CUR_SCHAR(ctxt->cur, len);
  4951. ctxt->atom->codepoint = codepoint;
  4952. NEXTL(len);
  4953. return(1);
  4954. } else if (CUR == '|') {
  4955. return(0);
  4956. } else if (CUR == 0) {
  4957. return(0);
  4958. } else if (CUR == ')') {
  4959. return(0);
  4960. } else if (CUR == '(') {
  4961. xmlRegStatePtr start, oldend, start0;
  4962. NEXT;
  4963. /*
  4964. * this extra Epsilon transition is needed if we count with 0 allowed
  4965. * unfortunately this can't be known at that point
  4966. */
  4967. xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
  4968. start0 = ctxt->state;
  4969. xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
  4970. start = ctxt->state;
  4971. oldend = ctxt->end;
  4972. ctxt->end = NULL;
  4973. ctxt->atom = NULL;
  4974. xmlFAParseRegExp(ctxt, 0);
  4975. if (CUR == ')') {
  4976. NEXT;
  4977. } else {
  4978. ERROR("xmlFAParseAtom: expecting ')'");
  4979. }
  4980. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
  4981. if (ctxt->atom == NULL)
  4982. return(-1);
  4983. ctxt->atom->start = start;
  4984. ctxt->atom->start0 = start0;
  4985. ctxt->atom->stop = ctxt->state;
  4986. ctxt->end = oldend;
  4987. return(1);
  4988. } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
  4989. xmlFAParseCharClass(ctxt);
  4990. return(1);
  4991. }
  4992. return(0);
  4993. }
  4994. /**
  4995. * xmlFAParsePiece:
  4996. * @ctxt: a regexp parser context
  4997. *
  4998. * [3] piece ::= atom quantifier?
  4999. */
  5000. static int
  5001. xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
  5002. int ret;
  5003. ctxt->atom = NULL;
  5004. ret = xmlFAParseAtom(ctxt);
  5005. if (ret == 0)
  5006. return(0);
  5007. if (ctxt->atom == NULL) {
  5008. ERROR("internal: no atom generated");
  5009. }
  5010. xmlFAParseQuantifier(ctxt);
  5011. return(1);
  5012. }
  5013. /**
  5014. * xmlFAParseBranch:
  5015. * @ctxt: a regexp parser context
  5016. * @to: optional target to the end of the branch
  5017. *
  5018. * @to is used to optimize by removing duplicate path in automata
  5019. * in expressions like (a|b)(c|d)
  5020. *
  5021. * [2] branch ::= piece*
  5022. */
  5023. static int
  5024. xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
  5025. xmlRegStatePtr previous;
  5026. int ret;
  5027. previous = ctxt->state;
  5028. ret = xmlFAParsePiece(ctxt);
  5029. if (ret != 0) {
  5030. if (xmlFAGenerateTransitions(ctxt, previous,
  5031. (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
  5032. return(-1);
  5033. previous = ctxt->state;
  5034. ctxt->atom = NULL;
  5035. }
  5036. while ((ret != 0) && (ctxt->error == 0)) {
  5037. ret = xmlFAParsePiece(ctxt);
  5038. if (ret != 0) {
  5039. if (xmlFAGenerateTransitions(ctxt, previous,
  5040. (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
  5041. return(-1);
  5042. previous = ctxt->state;
  5043. ctxt->atom = NULL;
  5044. }
  5045. }
  5046. return(0);
  5047. }
  5048. /**
  5049. * xmlFAParseRegExp:
  5050. * @ctxt: a regexp parser context
  5051. * @top: is this the top-level expression ?
  5052. *
  5053. * [1] regExp ::= branch ( '|' branch )*
  5054. */
  5055. static void
  5056. xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
  5057. xmlRegStatePtr start, end;
  5058. /* if not top start should have been generated by an epsilon trans */
  5059. start = ctxt->state;
  5060. ctxt->end = NULL;
  5061. xmlFAParseBranch(ctxt, NULL);
  5062. if (top) {
  5063. #ifdef DEBUG_REGEXP_GRAPH
  5064. printf("State %d is final\n", ctxt->state->no);
  5065. #endif
  5066. ctxt->state->type = XML_REGEXP_FINAL_STATE;
  5067. }
  5068. if (CUR != '|') {
  5069. ctxt->end = ctxt->state;
  5070. return;
  5071. }
  5072. end = ctxt->state;
  5073. while ((CUR == '|') && (ctxt->error == 0)) {
  5074. NEXT;
  5075. ctxt->state = start;
  5076. ctxt->end = NULL;
  5077. xmlFAParseBranch(ctxt, end);
  5078. }
  5079. if (!top) {
  5080. ctxt->state = end;
  5081. ctxt->end = end;
  5082. }
  5083. }
  5084. /************************************************************************
  5085. * *
  5086. * The basic API *
  5087. * *
  5088. ************************************************************************/
  5089. /**
  5090. * xmlRegexpPrint:
  5091. * @output: the file for the output debug
  5092. * @regexp: the compiled regexp
  5093. *
  5094. * Print the content of the compiled regular expression
  5095. */
  5096. void
  5097. xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
  5098. int i;
  5099. if (output == NULL)
  5100. return;
  5101. fprintf(output, " regexp: ");
  5102. if (regexp == NULL) {
  5103. fprintf(output, "NULL\n");
  5104. return;
  5105. }
  5106. fprintf(output, "'%s' ", regexp->string);
  5107. fprintf(output, "\n");
  5108. fprintf(output, "%d atoms:\n", regexp->nbAtoms);
  5109. for (i = 0;i < regexp->nbAtoms; i++) {
  5110. fprintf(output, " %02d ", i);
  5111. xmlRegPrintAtom(output, regexp->atoms[i]);
  5112. }
  5113. fprintf(output, "%d states:", regexp->nbStates);
  5114. fprintf(output, "\n");
  5115. for (i = 0;i < regexp->nbStates; i++) {
  5116. xmlRegPrintState(output, regexp->states[i]);
  5117. }
  5118. fprintf(output, "%d counters:\n", regexp->nbCounters);
  5119. for (i = 0;i < regexp->nbCounters; i++) {
  5120. fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
  5121. regexp->counters[i].max);
  5122. }
  5123. }
  5124. /**
  5125. * xmlRegexpCompile:
  5126. * @regexp: a regular expression string
  5127. *
  5128. * Parses a regular expression conforming to XML Schemas Part 2 Datatype
  5129. * Appendix F and builds an automata suitable for testing strings against
  5130. * that regular expression
  5131. *
  5132. * Returns the compiled expression or NULL in case of error
  5133. */
  5134. xmlRegexpPtr
  5135. xmlRegexpCompile(const xmlChar *regexp) {
  5136. xmlRegexpPtr ret;
  5137. xmlRegParserCtxtPtr ctxt;
  5138. ctxt = xmlRegNewParserCtxt(regexp);
  5139. if (ctxt == NULL)
  5140. return(NULL);
  5141. /* initialize the parser */
  5142. ctxt->end = NULL;
  5143. ctxt->start = ctxt->state = xmlRegNewState(ctxt);
  5144. xmlRegStatePush(ctxt, ctxt->start);
  5145. /* parse the expression building an automata */
  5146. xmlFAParseRegExp(ctxt, 1);
  5147. if (CUR != 0) {
  5148. ERROR("xmlFAParseRegExp: extra characters");
  5149. }
  5150. if (ctxt->error != 0) {
  5151. xmlRegFreeParserCtxt(ctxt);
  5152. return(NULL);
  5153. }
  5154. ctxt->end = ctxt->state;
  5155. ctxt->start->type = XML_REGEXP_START_STATE;
  5156. ctxt->end->type = XML_REGEXP_FINAL_STATE;
  5157. /* remove the Epsilon except for counted transitions */
  5158. xmlFAEliminateEpsilonTransitions(ctxt);
  5159. if (ctxt->error != 0) {
  5160. xmlRegFreeParserCtxt(ctxt);
  5161. return(NULL);
  5162. }
  5163. ret = xmlRegEpxFromParse(ctxt);
  5164. xmlRegFreeParserCtxt(ctxt);
  5165. return(ret);
  5166. }
  5167. /**
  5168. * xmlRegexpExec:
  5169. * @comp: the compiled regular expression
  5170. * @content: the value to check against the regular expression
  5171. *
  5172. * Check if the regular expression generates the value
  5173. *
  5174. * Returns 1 if it matches, 0 if not and a negative value in case of error
  5175. */
  5176. int
  5177. xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
  5178. if ((comp == NULL) || (content == NULL))
  5179. return(-1);
  5180. return(xmlFARegExec(comp, content));
  5181. }
  5182. /**
  5183. * xmlRegexpIsDeterminist:
  5184. * @comp: the compiled regular expression
  5185. *
  5186. * Check if the regular expression is determinist
  5187. *
  5188. * Returns 1 if it yes, 0 if not and a negative value in case of error
  5189. */
  5190. int
  5191. xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
  5192. xmlAutomataPtr am;
  5193. int ret;
  5194. if (comp == NULL)
  5195. return(-1);
  5196. if (comp->determinist != -1)
  5197. return(comp->determinist);
  5198. am = xmlNewAutomata();
  5199. if (am->states != NULL) {
  5200. int i;
  5201. for (i = 0;i < am->nbStates;i++)
  5202. xmlRegFreeState(am->states[i]);
  5203. xmlFree(am->states);
  5204. }
  5205. am->nbAtoms = comp->nbAtoms;
  5206. am->atoms = comp->atoms;
  5207. am->nbStates = comp->nbStates;
  5208. am->states = comp->states;
  5209. am->determinist = -1;
  5210. am->flags = comp->flags;
  5211. ret = xmlFAComputesDeterminism(am);
  5212. am->atoms = NULL;
  5213. am->states = NULL;
  5214. xmlFreeAutomata(am);
  5215. comp->determinist = ret;
  5216. return(ret);
  5217. }
  5218. /**
  5219. * xmlRegFreeRegexp:
  5220. * @regexp: the regexp
  5221. *
  5222. * Free a regexp
  5223. */
  5224. void
  5225. xmlRegFreeRegexp(xmlRegexpPtr regexp) {
  5226. int i;
  5227. if (regexp == NULL)
  5228. return;
  5229. if (regexp->string != NULL)
  5230. xmlFree(regexp->string);
  5231. if (regexp->states != NULL) {
  5232. for (i = 0;i < regexp->nbStates;i++)
  5233. xmlRegFreeState(regexp->states[i]);
  5234. xmlFree(regexp->states);
  5235. }
  5236. if (regexp->atoms != NULL) {
  5237. for (i = 0;i < regexp->nbAtoms;i++)
  5238. xmlRegFreeAtom(regexp->atoms[i]);
  5239. xmlFree(regexp->atoms);
  5240. }
  5241. if (regexp->counters != NULL)
  5242. xmlFree(regexp->counters);
  5243. if (regexp->compact != NULL)
  5244. xmlFree(regexp->compact);
  5245. if (regexp->transdata != NULL)
  5246. xmlFree(regexp->transdata);
  5247. if (regexp->stringMap != NULL) {
  5248. for (i = 0; i < regexp->nbstrings;i++)
  5249. xmlFree(regexp->stringMap[i]);
  5250. xmlFree(regexp->stringMap);
  5251. }
  5252. xmlFree(regexp);
  5253. }
  5254. #ifdef LIBXML_AUTOMATA_ENABLED
  5255. /************************************************************************
  5256. * *
  5257. * The Automata interface *
  5258. * *
  5259. ************************************************************************/
  5260. /**
  5261. * xmlNewAutomata:
  5262. *
  5263. * Create a new automata
  5264. *
  5265. * Returns the new object or NULL in case of failure
  5266. */
  5267. xmlAutomataPtr
  5268. xmlNewAutomata(void) {
  5269. xmlAutomataPtr ctxt;
  5270. ctxt = xmlRegNewParserCtxt(NULL);
  5271. if (ctxt == NULL)
  5272. return(NULL);
  5273. /* initialize the parser */
  5274. ctxt->end = NULL;
  5275. ctxt->start = ctxt->state = xmlRegNewState(ctxt);
  5276. if (ctxt->start == NULL) {
  5277. xmlFreeAutomata(ctxt);
  5278. return(NULL);
  5279. }
  5280. ctxt->start->type = XML_REGEXP_START_STATE;
  5281. if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
  5282. xmlRegFreeState(ctxt->start);
  5283. xmlFreeAutomata(ctxt);
  5284. return(NULL);
  5285. }
  5286. ctxt->flags = 0;
  5287. return(ctxt);
  5288. }
  5289. /**
  5290. * xmlFreeAutomata:
  5291. * @am: an automata
  5292. *
  5293. * Free an automata
  5294. */
  5295. void
  5296. xmlFreeAutomata(xmlAutomataPtr am) {
  5297. if (am == NULL)
  5298. return;
  5299. xmlRegFreeParserCtxt(am);
  5300. }
  5301. /**
  5302. * xmlAutomataSetFlags:
  5303. * @am: an automata
  5304. * @flags: a set of internal flags
  5305. *
  5306. * Set some flags on the automata
  5307. */
  5308. void
  5309. xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
  5310. if (am == NULL)
  5311. return;
  5312. am->flags |= flags;
  5313. }
  5314. /**
  5315. * xmlAutomataGetInitState:
  5316. * @am: an automata
  5317. *
  5318. * Initial state lookup
  5319. *
  5320. * Returns the initial state of the automata
  5321. */
  5322. xmlAutomataStatePtr
  5323. xmlAutomataGetInitState(xmlAutomataPtr am) {
  5324. if (am == NULL)
  5325. return(NULL);
  5326. return(am->start);
  5327. }
  5328. /**
  5329. * xmlAutomataSetFinalState:
  5330. * @am: an automata
  5331. * @state: a state in this automata
  5332. *
  5333. * Makes that state a final state
  5334. *
  5335. * Returns 0 or -1 in case of error
  5336. */
  5337. int
  5338. xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
  5339. if ((am == NULL) || (state == NULL))
  5340. return(-1);
  5341. state->type = XML_REGEXP_FINAL_STATE;
  5342. return(0);
  5343. }
  5344. /**
  5345. * xmlAutomataNewTransition:
  5346. * @am: an automata
  5347. * @from: the starting point of the transition
  5348. * @to: the target point of the transition or NULL
  5349. * @token: the input string associated to that transition
  5350. * @data: data passed to the callback function if the transition is activated
  5351. *
  5352. * If @to is NULL, this creates first a new target state in the automata
  5353. * and then adds a transition from the @from state to the target state
  5354. * activated by the value of @token
  5355. *
  5356. * Returns the target state or NULL in case of error
  5357. */
  5358. xmlAutomataStatePtr
  5359. xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5360. xmlAutomataStatePtr to, const xmlChar *token,
  5361. void *data) {
  5362. xmlRegAtomPtr atom;
  5363. if ((am == NULL) || (from == NULL) || (token == NULL))
  5364. return(NULL);
  5365. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5366. if (atom == NULL)
  5367. return(NULL);
  5368. atom->data = data;
  5369. if (atom == NULL)
  5370. return(NULL);
  5371. atom->valuep = xmlStrdup(token);
  5372. if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
  5373. xmlRegFreeAtom(atom);
  5374. return(NULL);
  5375. }
  5376. if (to == NULL)
  5377. return(am->state);
  5378. return(to);
  5379. }
  5380. /**
  5381. * xmlAutomataNewTransition2:
  5382. * @am: an automata
  5383. * @from: the starting point of the transition
  5384. * @to: the target point of the transition or NULL
  5385. * @token: the first input string associated to that transition
  5386. * @token2: the second input string associated to that transition
  5387. * @data: data passed to the callback function if the transition is activated
  5388. *
  5389. * If @to is NULL, this creates first a new target state in the automata
  5390. * and then adds a transition from the @from state to the target state
  5391. * activated by the value of @token
  5392. *
  5393. * Returns the target state or NULL in case of error
  5394. */
  5395. xmlAutomataStatePtr
  5396. xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5397. xmlAutomataStatePtr to, const xmlChar *token,
  5398. const xmlChar *token2, void *data) {
  5399. xmlRegAtomPtr atom;
  5400. if ((am == NULL) || (from == NULL) || (token == NULL))
  5401. return(NULL);
  5402. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5403. if (atom == NULL)
  5404. return(NULL);
  5405. atom->data = data;
  5406. if ((token2 == NULL) || (*token2 == 0)) {
  5407. atom->valuep = xmlStrdup(token);
  5408. } else {
  5409. int lenn, lenp;
  5410. xmlChar *str;
  5411. lenn = strlen((char *) token2);
  5412. lenp = strlen((char *) token);
  5413. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  5414. if (str == NULL) {
  5415. xmlRegFreeAtom(atom);
  5416. return(NULL);
  5417. }
  5418. memcpy(&str[0], token, lenp);
  5419. str[lenp] = '|';
  5420. memcpy(&str[lenp + 1], token2, lenn);
  5421. str[lenn + lenp + 1] = 0;
  5422. atom->valuep = str;
  5423. }
  5424. if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
  5425. xmlRegFreeAtom(atom);
  5426. return(NULL);
  5427. }
  5428. if (to == NULL)
  5429. return(am->state);
  5430. return(to);
  5431. }
  5432. /**
  5433. * xmlAutomataNewNegTrans:
  5434. * @am: an automata
  5435. * @from: the starting point of the transition
  5436. * @to: the target point of the transition or NULL
  5437. * @token: the first input string associated to that transition
  5438. * @token2: the second input string associated to that transition
  5439. * @data: data passed to the callback function if the transition is activated
  5440. *
  5441. * If @to is NULL, this creates first a new target state in the automata
  5442. * and then adds a transition from the @from state to the target state
  5443. * activated by any value except (@token,@token2)
  5444. * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
  5445. # the semantic of XSD ##other
  5446. *
  5447. * Returns the target state or NULL in case of error
  5448. */
  5449. xmlAutomataStatePtr
  5450. xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5451. xmlAutomataStatePtr to, const xmlChar *token,
  5452. const xmlChar *token2, void *data) {
  5453. xmlRegAtomPtr atom;
  5454. xmlChar err_msg[200];
  5455. if ((am == NULL) || (from == NULL) || (token == NULL))
  5456. return(NULL);
  5457. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5458. if (atom == NULL)
  5459. return(NULL);
  5460. atom->data = data;
  5461. atom->neg = 1;
  5462. if ((token2 == NULL) || (*token2 == 0)) {
  5463. atom->valuep = xmlStrdup(token);
  5464. } else {
  5465. int lenn, lenp;
  5466. xmlChar *str;
  5467. lenn = strlen((char *) token2);
  5468. lenp = strlen((char *) token);
  5469. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  5470. if (str == NULL) {
  5471. xmlRegFreeAtom(atom);
  5472. return(NULL);
  5473. }
  5474. memcpy(&str[0], token, lenp);
  5475. str[lenp] = '|';
  5476. memcpy(&str[lenp + 1], token2, lenn);
  5477. str[lenn + lenp + 1] = 0;
  5478. atom->valuep = str;
  5479. }
  5480. snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
  5481. err_msg[199] = 0;
  5482. atom->valuep2 = xmlStrdup(err_msg);
  5483. if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
  5484. xmlRegFreeAtom(atom);
  5485. return(NULL);
  5486. }
  5487. am->negs++;
  5488. if (to == NULL)
  5489. return(am->state);
  5490. return(to);
  5491. }
  5492. /**
  5493. * xmlAutomataNewCountTrans2:
  5494. * @am: an automata
  5495. * @from: the starting point of the transition
  5496. * @to: the target point of the transition or NULL
  5497. * @token: the input string associated to that transition
  5498. * @token2: the second input string associated to that transition
  5499. * @min: the minimum successive occurences of token
  5500. * @max: the maximum successive occurences of token
  5501. * @data: data associated to the transition
  5502. *
  5503. * If @to is NULL, this creates first a new target state in the automata
  5504. * and then adds a transition from the @from state to the target state
  5505. * activated by a succession of input of value @token and @token2 and
  5506. * whose number is between @min and @max
  5507. *
  5508. * Returns the target state or NULL in case of error
  5509. */
  5510. xmlAutomataStatePtr
  5511. xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5512. xmlAutomataStatePtr to, const xmlChar *token,
  5513. const xmlChar *token2,
  5514. int min, int max, void *data) {
  5515. xmlRegAtomPtr atom;
  5516. int counter;
  5517. if ((am == NULL) || (from == NULL) || (token == NULL))
  5518. return(NULL);
  5519. if (min < 0)
  5520. return(NULL);
  5521. if ((max < min) || (max < 1))
  5522. return(NULL);
  5523. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5524. if (atom == NULL)
  5525. return(NULL);
  5526. if ((token2 == NULL) || (*token2 == 0)) {
  5527. atom->valuep = xmlStrdup(token);
  5528. } else {
  5529. int lenn, lenp;
  5530. xmlChar *str;
  5531. lenn = strlen((char *) token2);
  5532. lenp = strlen((char *) token);
  5533. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  5534. if (str == NULL) {
  5535. xmlRegFreeAtom(atom);
  5536. return(NULL);
  5537. }
  5538. memcpy(&str[0], token, lenp);
  5539. str[lenp] = '|';
  5540. memcpy(&str[lenp + 1], token2, lenn);
  5541. str[lenn + lenp + 1] = 0;
  5542. atom->valuep = str;
  5543. }
  5544. atom->data = data;
  5545. if (min == 0)
  5546. atom->min = 1;
  5547. else
  5548. atom->min = min;
  5549. atom->max = max;
  5550. /*
  5551. * associate a counter to the transition.
  5552. */
  5553. counter = xmlRegGetCounter(am);
  5554. am->counters[counter].min = min;
  5555. am->counters[counter].max = max;
  5556. /* xmlFAGenerateTransitions(am, from, to, atom); */
  5557. if (to == NULL) {
  5558. to = xmlRegNewState(am);
  5559. xmlRegStatePush(am, to);
  5560. }
  5561. xmlRegStateAddTrans(am, from, atom, to, counter, -1);
  5562. xmlRegAtomPush(am, atom);
  5563. am->state = to;
  5564. if (to == NULL)
  5565. to = am->state;
  5566. if (to == NULL)
  5567. return(NULL);
  5568. if (min == 0)
  5569. xmlFAGenerateEpsilonTransition(am, from, to);
  5570. return(to);
  5571. }
  5572. /**
  5573. * xmlAutomataNewCountTrans:
  5574. * @am: an automata
  5575. * @from: the starting point of the transition
  5576. * @to: the target point of the transition or NULL
  5577. * @token: the input string associated to that transition
  5578. * @min: the minimum successive occurences of token
  5579. * @max: the maximum successive occurences of token
  5580. * @data: data associated to the transition
  5581. *
  5582. * If @to is NULL, this creates first a new target state in the automata
  5583. * and then adds a transition from the @from state to the target state
  5584. * activated by a succession of input of value @token and whose number
  5585. * is between @min and @max
  5586. *
  5587. * Returns the target state or NULL in case of error
  5588. */
  5589. xmlAutomataStatePtr
  5590. xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5591. xmlAutomataStatePtr to, const xmlChar *token,
  5592. int min, int max, void *data) {
  5593. xmlRegAtomPtr atom;
  5594. int counter;
  5595. if ((am == NULL) || (from == NULL) || (token == NULL))
  5596. return(NULL);
  5597. if (min < 0)
  5598. return(NULL);
  5599. if ((max < min) || (max < 1))
  5600. return(NULL);
  5601. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5602. if (atom == NULL)
  5603. return(NULL);
  5604. atom->valuep = xmlStrdup(token);
  5605. atom->data = data;
  5606. if (min == 0)
  5607. atom->min = 1;
  5608. else
  5609. atom->min = min;
  5610. atom->max = max;
  5611. /*
  5612. * associate a counter to the transition.
  5613. */
  5614. counter = xmlRegGetCounter(am);
  5615. am->counters[counter].min = min;
  5616. am->counters[counter].max = max;
  5617. /* xmlFAGenerateTransitions(am, from, to, atom); */
  5618. if (to == NULL) {
  5619. to = xmlRegNewState(am);
  5620. xmlRegStatePush(am, to);
  5621. }
  5622. xmlRegStateAddTrans(am, from, atom, to, counter, -1);
  5623. xmlRegAtomPush(am, atom);
  5624. am->state = to;
  5625. if (to == NULL)
  5626. to = am->state;
  5627. if (to == NULL)
  5628. return(NULL);
  5629. if (min == 0)
  5630. xmlFAGenerateEpsilonTransition(am, from, to);
  5631. return(to);
  5632. }
  5633. /**
  5634. * xmlAutomataNewOnceTrans2:
  5635. * @am: an automata
  5636. * @from: the starting point of the transition
  5637. * @to: the target point of the transition or NULL
  5638. * @token: the input string associated to that transition
  5639. * @token2: the second input string associated to that transition
  5640. * @min: the minimum successive occurences of token
  5641. * @max: the maximum successive occurences of token
  5642. * @data: data associated to the transition
  5643. *
  5644. * If @to is NULL, this creates first a new target state in the automata
  5645. * and then adds a transition from the @from state to the target state
  5646. * activated by a succession of input of value @token and @token2 and whose
  5647. * number is between @min and @max, moreover that transition can only be
  5648. * crossed once.
  5649. *
  5650. * Returns the target state or NULL in case of error
  5651. */
  5652. xmlAutomataStatePtr
  5653. xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5654. xmlAutomataStatePtr to, const xmlChar *token,
  5655. const xmlChar *token2,
  5656. int min, int max, void *data) {
  5657. xmlRegAtomPtr atom;
  5658. int counter;
  5659. if ((am == NULL) || (from == NULL) || (token == NULL))
  5660. return(NULL);
  5661. if (min < 1)
  5662. return(NULL);
  5663. if ((max < min) || (max < 1))
  5664. return(NULL);
  5665. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5666. if (atom == NULL)
  5667. return(NULL);
  5668. if ((token2 == NULL) || (*token2 == 0)) {
  5669. atom->valuep = xmlStrdup(token);
  5670. } else {
  5671. int lenn, lenp;
  5672. xmlChar *str;
  5673. lenn = strlen((char *) token2);
  5674. lenp = strlen((char *) token);
  5675. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  5676. if (str == NULL) {
  5677. xmlRegFreeAtom(atom);
  5678. return(NULL);
  5679. }
  5680. memcpy(&str[0], token, lenp);
  5681. str[lenp] = '|';
  5682. memcpy(&str[lenp + 1], token2, lenn);
  5683. str[lenn + lenp + 1] = 0;
  5684. atom->valuep = str;
  5685. }
  5686. atom->data = data;
  5687. atom->quant = XML_REGEXP_QUANT_ONCEONLY;
  5688. atom->min = min;
  5689. atom->max = max;
  5690. /*
  5691. * associate a counter to the transition.
  5692. */
  5693. counter = xmlRegGetCounter(am);
  5694. am->counters[counter].min = 1;
  5695. am->counters[counter].max = 1;
  5696. /* xmlFAGenerateTransitions(am, from, to, atom); */
  5697. if (to == NULL) {
  5698. to = xmlRegNewState(am);
  5699. xmlRegStatePush(am, to);
  5700. }
  5701. xmlRegStateAddTrans(am, from, atom, to, counter, -1);
  5702. xmlRegAtomPush(am, atom);
  5703. am->state = to;
  5704. return(to);
  5705. }
  5706. /**
  5707. * xmlAutomataNewOnceTrans:
  5708. * @am: an automata
  5709. * @from: the starting point of the transition
  5710. * @to: the target point of the transition or NULL
  5711. * @token: the input string associated to that transition
  5712. * @min: the minimum successive occurences of token
  5713. * @max: the maximum successive occurences of token
  5714. * @data: data associated to the transition
  5715. *
  5716. * If @to is NULL, this creates first a new target state in the automata
  5717. * and then adds a transition from the @from state to the target state
  5718. * activated by a succession of input of value @token and whose number
  5719. * is between @min and @max, moreover that transition can only be crossed
  5720. * once.
  5721. *
  5722. * Returns the target state or NULL in case of error
  5723. */
  5724. xmlAutomataStatePtr
  5725. xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5726. xmlAutomataStatePtr to, const xmlChar *token,
  5727. int min, int max, void *data) {
  5728. xmlRegAtomPtr atom;
  5729. int counter;
  5730. if ((am == NULL) || (from == NULL) || (token == NULL))
  5731. return(NULL);
  5732. if (min < 1)
  5733. return(NULL);
  5734. if ((max < min) || (max < 1))
  5735. return(NULL);
  5736. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5737. if (atom == NULL)
  5738. return(NULL);
  5739. atom->valuep = xmlStrdup(token);
  5740. atom->data = data;
  5741. atom->quant = XML_REGEXP_QUANT_ONCEONLY;
  5742. atom->min = min;
  5743. atom->max = max;
  5744. /*
  5745. * associate a counter to the transition.
  5746. */
  5747. counter = xmlRegGetCounter(am);
  5748. am->counters[counter].min = 1;
  5749. am->counters[counter].max = 1;
  5750. /* xmlFAGenerateTransitions(am, from, to, atom); */
  5751. if (to == NULL) {
  5752. to = xmlRegNewState(am);
  5753. xmlRegStatePush(am, to);
  5754. }
  5755. xmlRegStateAddTrans(am, from, atom, to, counter, -1);
  5756. xmlRegAtomPush(am, atom);
  5757. am->state = to;
  5758. return(to);
  5759. }
  5760. /**
  5761. * xmlAutomataNewState:
  5762. * @am: an automata
  5763. *
  5764. * Create a new disconnected state in the automata
  5765. *
  5766. * Returns the new state or NULL in case of error
  5767. */
  5768. xmlAutomataStatePtr
  5769. xmlAutomataNewState(xmlAutomataPtr am) {
  5770. xmlAutomataStatePtr to;
  5771. if (am == NULL)
  5772. return(NULL);
  5773. to = xmlRegNewState(am);
  5774. xmlRegStatePush(am, to);
  5775. return(to);
  5776. }
  5777. /**
  5778. * xmlAutomataNewEpsilon:
  5779. * @am: an automata
  5780. * @from: the starting point of the transition
  5781. * @to: the target point of the transition or NULL
  5782. *
  5783. * If @to is NULL, this creates first a new target state in the automata
  5784. * and then adds an epsilon transition from the @from state to the
  5785. * target state
  5786. *
  5787. * Returns the target state or NULL in case of error
  5788. */
  5789. xmlAutomataStatePtr
  5790. xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5791. xmlAutomataStatePtr to) {
  5792. if ((am == NULL) || (from == NULL))
  5793. return(NULL);
  5794. xmlFAGenerateEpsilonTransition(am, from, to);
  5795. if (to == NULL)
  5796. return(am->state);
  5797. return(to);
  5798. }
  5799. /**
  5800. * xmlAutomataNewAllTrans:
  5801. * @am: an automata
  5802. * @from: the starting point of the transition
  5803. * @to: the target point of the transition or NULL
  5804. * @lax: allow to transition if not all all transitions have been activated
  5805. *
  5806. * If @to is NULL, this creates first a new target state in the automata
  5807. * and then adds a an ALL transition from the @from state to the
  5808. * target state. That transition is an epsilon transition allowed only when
  5809. * all transitions from the @from node have been activated.
  5810. *
  5811. * Returns the target state or NULL in case of error
  5812. */
  5813. xmlAutomataStatePtr
  5814. xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5815. xmlAutomataStatePtr to, int lax) {
  5816. if ((am == NULL) || (from == NULL))
  5817. return(NULL);
  5818. xmlFAGenerateAllTransition(am, from, to, lax);
  5819. if (to == NULL)
  5820. return(am->state);
  5821. return(to);
  5822. }
  5823. /**
  5824. * xmlAutomataNewCounter:
  5825. * @am: an automata
  5826. * @min: the minimal value on the counter
  5827. * @max: the maximal value on the counter
  5828. *
  5829. * Create a new counter
  5830. *
  5831. * Returns the counter number or -1 in case of error
  5832. */
  5833. int
  5834. xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
  5835. int ret;
  5836. if (am == NULL)
  5837. return(-1);
  5838. ret = xmlRegGetCounter(am);
  5839. if (ret < 0)
  5840. return(-1);
  5841. am->counters[ret].min = min;
  5842. am->counters[ret].max = max;
  5843. return(ret);
  5844. }
  5845. /**
  5846. * xmlAutomataNewCountedTrans:
  5847. * @am: an automata
  5848. * @from: the starting point of the transition
  5849. * @to: the target point of the transition or NULL
  5850. * @counter: the counter associated to that transition
  5851. *
  5852. * If @to is NULL, this creates first a new target state in the automata
  5853. * and then adds an epsilon transition from the @from state to the target state
  5854. * which will increment the counter provided
  5855. *
  5856. * Returns the target state or NULL in case of error
  5857. */
  5858. xmlAutomataStatePtr
  5859. xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5860. xmlAutomataStatePtr to, int counter) {
  5861. if ((am == NULL) || (from == NULL) || (counter < 0))
  5862. return(NULL);
  5863. xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
  5864. if (to == NULL)
  5865. return(am->state);
  5866. return(to);
  5867. }
  5868. /**
  5869. * xmlAutomataNewCounterTrans:
  5870. * @am: an automata
  5871. * @from: the starting point of the transition
  5872. * @to: the target point of the transition or NULL
  5873. * @counter: the counter associated to that transition
  5874. *
  5875. * If @to is NULL, this creates first a new target state in the automata
  5876. * and then adds an epsilon transition from the @from state to the target state
  5877. * which will be allowed only if the counter is within the right range.
  5878. *
  5879. * Returns the target state or NULL in case of error
  5880. */
  5881. xmlAutomataStatePtr
  5882. xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5883. xmlAutomataStatePtr to, int counter) {
  5884. if ((am == NULL) || (from == NULL) || (counter < 0))
  5885. return(NULL);
  5886. xmlFAGenerateCountedTransition(am, from, to, counter);
  5887. if (to == NULL)
  5888. return(am->state);
  5889. return(to);
  5890. }
  5891. /**
  5892. * xmlAutomataCompile:
  5893. * @am: an automata
  5894. *
  5895. * Compile the automata into a Reg Exp ready for being executed.
  5896. * The automata should be free after this point.
  5897. *
  5898. * Returns the compiled regexp or NULL in case of error
  5899. */
  5900. xmlRegexpPtr
  5901. xmlAutomataCompile(xmlAutomataPtr am) {
  5902. xmlRegexpPtr ret;
  5903. if ((am == NULL) || (am->error != 0)) return(NULL);
  5904. xmlFAEliminateEpsilonTransitions(am);
  5905. /* xmlFAComputesDeterminism(am); */
  5906. ret = xmlRegEpxFromParse(am);
  5907. return(ret);
  5908. }
  5909. /**
  5910. * xmlAutomataIsDeterminist:
  5911. * @am: an automata
  5912. *
  5913. * Checks if an automata is determinist.
  5914. *
  5915. * Returns 1 if true, 0 if not, and -1 in case of error
  5916. */
  5917. int
  5918. xmlAutomataIsDeterminist(xmlAutomataPtr am) {
  5919. int ret;
  5920. if (am == NULL)
  5921. return(-1);
  5922. ret = xmlFAComputesDeterminism(am);
  5923. return(ret);
  5924. }
  5925. #endif /* LIBXML_AUTOMATA_ENABLED */
  5926. #ifdef LIBXML_EXPR_ENABLED
  5927. /************************************************************************
  5928. * *
  5929. * Formal Expression handling code *
  5930. * *
  5931. ************************************************************************/
  5932. /************************************************************************
  5933. * *
  5934. * Expression handling context *
  5935. * *
  5936. ************************************************************************/
  5937. struct _xmlExpCtxt {
  5938. xmlDictPtr dict;
  5939. xmlExpNodePtr *table;
  5940. int size;
  5941. int nbElems;
  5942. int nb_nodes;
  5943. int maxNodes;
  5944. const char *expr;
  5945. const char *cur;
  5946. int nb_cons;
  5947. int tabSize;
  5948. };
  5949. /**
  5950. * xmlExpNewCtxt:
  5951. * @maxNodes: the maximum number of nodes
  5952. * @dict: optional dictionnary to use internally
  5953. *
  5954. * Creates a new context for manipulating expressions
  5955. *
  5956. * Returns the context or NULL in case of error
  5957. */
  5958. xmlExpCtxtPtr
  5959. xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
  5960. xmlExpCtxtPtr ret;
  5961. int size = 256;
  5962. if (maxNodes <= 4096)
  5963. maxNodes = 4096;
  5964. ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
  5965. if (ret == NULL)
  5966. return(NULL);
  5967. memset(ret, 0, sizeof(xmlExpCtxt));
  5968. ret->size = size;
  5969. ret->nbElems = 0;
  5970. ret->maxNodes = maxNodes;
  5971. ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
  5972. if (ret->table == NULL) {
  5973. xmlFree(ret);
  5974. return(NULL);
  5975. }
  5976. memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
  5977. if (dict == NULL) {
  5978. ret->dict = xmlDictCreate();
  5979. if (ret->dict == NULL) {
  5980. xmlFree(ret->table);
  5981. xmlFree(ret);
  5982. return(NULL);
  5983. }
  5984. } else {
  5985. ret->dict = dict;
  5986. xmlDictReference(ret->dict);
  5987. }
  5988. return(ret);
  5989. }
  5990. /**
  5991. * xmlExpFreeCtxt:
  5992. * @ctxt: an expression context
  5993. *
  5994. * Free an expression context
  5995. */
  5996. void
  5997. xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
  5998. if (ctxt == NULL)
  5999. return;
  6000. xmlDictFree(ctxt->dict);
  6001. if (ctxt->table != NULL)
  6002. xmlFree(ctxt->table);
  6003. xmlFree(ctxt);
  6004. }
  6005. /************************************************************************
  6006. * *
  6007. * Structure associated to an expression node *
  6008. * *
  6009. ************************************************************************/
  6010. #define MAX_NODES 10000
  6011. /* #define DEBUG_DERIV */
  6012. /*
  6013. * TODO:
  6014. * - Wildcards
  6015. * - public API for creation
  6016. *
  6017. * Started
  6018. * - regression testing
  6019. *
  6020. * Done
  6021. * - split into module and test tool
  6022. * - memleaks
  6023. */
  6024. typedef enum {
  6025. XML_EXP_NILABLE = (1 << 0)
  6026. } xmlExpNodeInfo;
  6027. #define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
  6028. struct _xmlExpNode {
  6029. unsigned char type;/* xmlExpNodeType */
  6030. unsigned char info;/* OR of xmlExpNodeInfo */
  6031. unsigned short key; /* the hash key */
  6032. unsigned int ref; /* The number of references */
  6033. int c_max; /* the maximum length it can consume */
  6034. xmlExpNodePtr exp_left;
  6035. xmlExpNodePtr next;/* the next node in the hash table or free list */
  6036. union {
  6037. struct {
  6038. int f_min;
  6039. int f_max;
  6040. } count;
  6041. struct {
  6042. xmlExpNodePtr f_right;
  6043. } children;
  6044. const xmlChar *f_str;
  6045. } field;
  6046. };
  6047. #define exp_min field.count.f_min
  6048. #define exp_max field.count.f_max
  6049. /* #define exp_left field.children.f_left */
  6050. #define exp_right field.children.f_right
  6051. #define exp_str field.f_str
  6052. static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
  6053. static xmlExpNode forbiddenExpNode = {
  6054. XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
  6055. };
  6056. xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
  6057. static xmlExpNode emptyExpNode = {
  6058. XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
  6059. };
  6060. xmlExpNodePtr emptyExp = &emptyExpNode;
  6061. /************************************************************************
  6062. * *
  6063. * The custom hash table for unicity and canonicalization *
  6064. * of sub-expressions pointers *
  6065. * *
  6066. ************************************************************************/
  6067. /*
  6068. * xmlExpHashNameComputeKey:
  6069. * Calculate the hash key for a token
  6070. */
  6071. static unsigned short
  6072. xmlExpHashNameComputeKey(const xmlChar *name) {
  6073. unsigned short value = 0L;
  6074. char ch;
  6075. if (name != NULL) {
  6076. value += 30 * (*name);
  6077. while ((ch = *name++) != 0) {
  6078. value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
  6079. }
  6080. }
  6081. return (value);
  6082. }
  6083. /*
  6084. * xmlExpHashComputeKey:
  6085. * Calculate the hash key for a compound expression
  6086. */
  6087. static unsigned short
  6088. xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
  6089. xmlExpNodePtr right) {
  6090. unsigned long value;
  6091. unsigned short ret;
  6092. switch (type) {
  6093. case XML_EXP_SEQ:
  6094. value = left->key;
  6095. value += right->key;
  6096. value *= 3;
  6097. ret = (unsigned short) value;
  6098. break;
  6099. case XML_EXP_OR:
  6100. value = left->key;
  6101. value += right->key;
  6102. value *= 7;
  6103. ret = (unsigned short) value;
  6104. break;
  6105. case XML_EXP_COUNT:
  6106. value = left->key;
  6107. value += right->key;
  6108. ret = (unsigned short) value;
  6109. break;
  6110. default:
  6111. ret = 0;
  6112. }
  6113. return(ret);
  6114. }
  6115. static xmlExpNodePtr
  6116. xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
  6117. xmlExpNodePtr ret;
  6118. if (ctxt->nb_nodes >= MAX_NODES)
  6119. return(NULL);
  6120. ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
  6121. if (ret == NULL)
  6122. return(NULL);
  6123. memset(ret, 0, sizeof(xmlExpNode));
  6124. ret->type = type;
  6125. ret->next = NULL;
  6126. ctxt->nb_nodes++;
  6127. ctxt->nb_cons++;
  6128. return(ret);
  6129. }
  6130. /**
  6131. * xmlExpHashGetEntry:
  6132. * @table: the hash table
  6133. *
  6134. * Get the unique entry from the hash table. The entry is created if
  6135. * needed. @left and @right are consumed, i.e. their ref count will
  6136. * be decremented by the operation.
  6137. *
  6138. * Returns the pointer or NULL in case of error
  6139. */
  6140. static xmlExpNodePtr
  6141. xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
  6142. xmlExpNodePtr left, xmlExpNodePtr right,
  6143. const xmlChar *name, int min, int max) {
  6144. unsigned short kbase, key;
  6145. xmlExpNodePtr entry;
  6146. xmlExpNodePtr insert;
  6147. if (ctxt == NULL)
  6148. return(NULL);
  6149. /*
  6150. * Check for duplicate and insertion location.
  6151. */
  6152. if (type == XML_EXP_ATOM) {
  6153. kbase = xmlExpHashNameComputeKey(name);
  6154. } else if (type == XML_EXP_COUNT) {
  6155. /* COUNT reduction rule 1 */
  6156. /* a{1} -> a */
  6157. if (min == max) {
  6158. if (min == 1) {
  6159. return(left);
  6160. }
  6161. if (min == 0) {
  6162. xmlExpFree(ctxt, left);
  6163. return(emptyExp);
  6164. }
  6165. }
  6166. if (min < 0) {
  6167. xmlExpFree(ctxt, left);
  6168. return(forbiddenExp);
  6169. }
  6170. if (max == -1)
  6171. kbase = min + 79;
  6172. else
  6173. kbase = max - min;
  6174. kbase += left->key;
  6175. } else if (type == XML_EXP_OR) {
  6176. /* Forbid reduction rules */
  6177. if (left->type == XML_EXP_FORBID) {
  6178. xmlExpFree(ctxt, left);
  6179. return(right);
  6180. }
  6181. if (right->type == XML_EXP_FORBID) {
  6182. xmlExpFree(ctxt, right);
  6183. return(left);
  6184. }
  6185. /* OR reduction rule 1 */
  6186. /* a | a reduced to a */
  6187. if (left == right) {
  6188. left->ref--;
  6189. return(left);
  6190. }
  6191. /* OR canonicalization rule 1 */
  6192. /* linearize (a | b) | c into a | (b | c) */
  6193. if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
  6194. xmlExpNodePtr tmp = left;
  6195. left = right;
  6196. right = tmp;
  6197. }
  6198. /* OR reduction rule 2 */
  6199. /* a | (a | b) and b | (a | b) are reduced to a | b */
  6200. if (right->type == XML_EXP_OR) {
  6201. if ((left == right->exp_left) ||
  6202. (left == right->exp_right)) {
  6203. xmlExpFree(ctxt, left);
  6204. return(right);
  6205. }
  6206. }
  6207. /* OR canonicalization rule 2 */
  6208. /* linearize (a | b) | c into a | (b | c) */
  6209. if (left->type == XML_EXP_OR) {
  6210. xmlExpNodePtr tmp;
  6211. /* OR canonicalization rule 2 */
  6212. if ((left->exp_right->type != XML_EXP_OR) &&
  6213. (left->exp_right->key < left->exp_left->key)) {
  6214. tmp = left->exp_right;
  6215. left->exp_right = left->exp_left;
  6216. left->exp_left = tmp;
  6217. }
  6218. left->exp_right->ref++;
  6219. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
  6220. NULL, 0, 0);
  6221. left->exp_left->ref++;
  6222. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
  6223. NULL, 0, 0);
  6224. xmlExpFree(ctxt, left);
  6225. return(tmp);
  6226. }
  6227. if (right->type == XML_EXP_OR) {
  6228. /* Ordering in the tree */
  6229. /* C | (A | B) -> A | (B | C) */
  6230. if (left->key > right->exp_right->key) {
  6231. xmlExpNodePtr tmp;
  6232. right->exp_right->ref++;
  6233. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
  6234. left, NULL, 0, 0);
  6235. right->exp_left->ref++;
  6236. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
  6237. tmp, NULL, 0, 0);
  6238. xmlExpFree(ctxt, right);
  6239. return(tmp);
  6240. }
  6241. /* Ordering in the tree */
  6242. /* B | (A | C) -> A | (B | C) */
  6243. if (left->key > right->exp_left->key) {
  6244. xmlExpNodePtr tmp;
  6245. right->exp_right->ref++;
  6246. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
  6247. right->exp_right, NULL, 0, 0);
  6248. right->exp_left->ref++;
  6249. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
  6250. tmp, NULL, 0, 0);
  6251. xmlExpFree(ctxt, right);
  6252. return(tmp);
  6253. }
  6254. }
  6255. /* we know both types are != XML_EXP_OR here */
  6256. else if (left->key > right->key) {
  6257. xmlExpNodePtr tmp = left;
  6258. left = right;
  6259. right = tmp;
  6260. }
  6261. kbase = xmlExpHashComputeKey(type, left, right);
  6262. } else if (type == XML_EXP_SEQ) {
  6263. /* Forbid reduction rules */
  6264. if (left->type == XML_EXP_FORBID) {
  6265. xmlExpFree(ctxt, right);
  6266. return(left);
  6267. }
  6268. if (right->type == XML_EXP_FORBID) {
  6269. xmlExpFree(ctxt, left);
  6270. return(right);
  6271. }
  6272. /* Empty reduction rules */
  6273. if (right->type == XML_EXP_EMPTY) {
  6274. return(left);
  6275. }
  6276. if (left->type == XML_EXP_EMPTY) {
  6277. return(right);
  6278. }
  6279. kbase = xmlExpHashComputeKey(type, left, right);
  6280. } else
  6281. return(NULL);
  6282. key = kbase % ctxt->size;
  6283. if (ctxt->table[key] != NULL) {
  6284. for (insert = ctxt->table[key]; insert != NULL;
  6285. insert = insert->next) {
  6286. if ((insert->key == kbase) &&
  6287. (insert->type == type)) {
  6288. if (type == XML_EXP_ATOM) {
  6289. if (name == insert->exp_str) {
  6290. insert->ref++;
  6291. return(insert);
  6292. }
  6293. } else if (type == XML_EXP_COUNT) {
  6294. if ((insert->exp_min == min) && (insert->exp_max == max) &&
  6295. (insert->exp_left == left)) {
  6296. insert->ref++;
  6297. left->ref--;
  6298. return(insert);
  6299. }
  6300. } else if ((insert->exp_left == left) &&
  6301. (insert->exp_right == right)) {
  6302. insert->ref++;
  6303. left->ref--;
  6304. right->ref--;
  6305. return(insert);
  6306. }
  6307. }
  6308. }
  6309. }
  6310. entry = xmlExpNewNode(ctxt, type);
  6311. if (entry == NULL)
  6312. return(NULL);
  6313. entry->key = kbase;
  6314. if (type == XML_EXP_ATOM) {
  6315. entry->exp_str = name;
  6316. entry->c_max = 1;
  6317. } else if (type == XML_EXP_COUNT) {
  6318. entry->exp_min = min;
  6319. entry->exp_max = max;
  6320. entry->exp_left = left;
  6321. if ((min == 0) || (IS_NILLABLE(left)))
  6322. entry->info |= XML_EXP_NILABLE;
  6323. if (max < 0)
  6324. entry->c_max = -1;
  6325. else
  6326. entry->c_max = max * entry->exp_left->c_max;
  6327. } else {
  6328. entry->exp_left = left;
  6329. entry->exp_right = right;
  6330. if (type == XML_EXP_OR) {
  6331. if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
  6332. entry->info |= XML_EXP_NILABLE;
  6333. if ((entry->exp_left->c_max == -1) ||
  6334. (entry->exp_right->c_max == -1))
  6335. entry->c_max = -1;
  6336. else if (entry->exp_left->c_max > entry->exp_right->c_max)
  6337. entry->c_max = entry->exp_left->c_max;
  6338. else
  6339. entry->c_max = entry->exp_right->c_max;
  6340. } else {
  6341. if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
  6342. entry->info |= XML_EXP_NILABLE;
  6343. if ((entry->exp_left->c_max == -1) ||
  6344. (entry->exp_right->c_max == -1))
  6345. entry->c_max = -1;
  6346. else
  6347. entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
  6348. }
  6349. }
  6350. entry->ref = 1;
  6351. if (ctxt->table[key] != NULL)
  6352. entry->next = ctxt->table[key];
  6353. ctxt->table[key] = entry;
  6354. ctxt->nbElems++;
  6355. return(entry);
  6356. }
  6357. /**
  6358. * xmlExpFree:
  6359. * @ctxt: the expression context
  6360. * @exp: the expression
  6361. *
  6362. * Dereference the expression
  6363. */
  6364. void
  6365. xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
  6366. if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
  6367. return;
  6368. exp->ref--;
  6369. if (exp->ref == 0) {
  6370. unsigned short key;
  6371. /* Unlink it first from the hash table */
  6372. key = exp->key % ctxt->size;
  6373. if (ctxt->table[key] == exp) {
  6374. ctxt->table[key] = exp->next;
  6375. } else {
  6376. xmlExpNodePtr tmp;
  6377. tmp = ctxt->table[key];
  6378. while (tmp != NULL) {
  6379. if (tmp->next == exp) {
  6380. tmp->next = exp->next;
  6381. break;
  6382. }
  6383. tmp = tmp->next;
  6384. }
  6385. }
  6386. if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
  6387. xmlExpFree(ctxt, exp->exp_left);
  6388. xmlExpFree(ctxt, exp->exp_right);
  6389. } else if (exp->type == XML_EXP_COUNT) {
  6390. xmlExpFree(ctxt, exp->exp_left);
  6391. }
  6392. xmlFree(exp);
  6393. ctxt->nb_nodes--;
  6394. }
  6395. }
  6396. /**
  6397. * xmlExpRef:
  6398. * @exp: the expression
  6399. *
  6400. * Increase the reference count of the expression
  6401. */
  6402. void
  6403. xmlExpRef(xmlExpNodePtr exp) {
  6404. if (exp != NULL)
  6405. exp->ref++;
  6406. }
  6407. /**
  6408. * xmlExpNewAtom:
  6409. * @ctxt: the expression context
  6410. * @name: the atom name
  6411. * @len: the atom name lenght in byte (or -1);
  6412. *
  6413. * Get the atom associated to this name from that context
  6414. *
  6415. * Returns the node or NULL in case of error
  6416. */
  6417. xmlExpNodePtr
  6418. xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
  6419. if ((ctxt == NULL) || (name == NULL))
  6420. return(NULL);
  6421. name = xmlDictLookup(ctxt->dict, name, len);
  6422. if (name == NULL)
  6423. return(NULL);
  6424. return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
  6425. }
  6426. /**
  6427. * xmlExpNewOr:
  6428. * @ctxt: the expression context
  6429. * @left: left expression
  6430. * @right: right expression
  6431. *
  6432. * Get the atom associated to the choice @left | @right
  6433. * Note that @left and @right are consumed in the operation, to keep
  6434. * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
  6435. * this is true even in case of failure (unless ctxt == NULL).
  6436. *
  6437. * Returns the node or NULL in case of error
  6438. */
  6439. xmlExpNodePtr
  6440. xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
  6441. if (ctxt == NULL)
  6442. return(NULL);
  6443. if ((left == NULL) || (right == NULL)) {
  6444. xmlExpFree(ctxt, left);
  6445. xmlExpFree(ctxt, right);
  6446. return(NULL);
  6447. }
  6448. return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
  6449. }
  6450. /**
  6451. * xmlExpNewSeq:
  6452. * @ctxt: the expression context
  6453. * @left: left expression
  6454. * @right: right expression
  6455. *
  6456. * Get the atom associated to the sequence @left , @right
  6457. * Note that @left and @right are consumed in the operation, to keep
  6458. * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
  6459. * this is true even in case of failure (unless ctxt == NULL).
  6460. *
  6461. * Returns the node or NULL in case of error
  6462. */
  6463. xmlExpNodePtr
  6464. xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
  6465. if (ctxt == NULL)
  6466. return(NULL);
  6467. if ((left == NULL) || (right == NULL)) {
  6468. xmlExpFree(ctxt, left);
  6469. xmlExpFree(ctxt, right);
  6470. return(NULL);
  6471. }
  6472. return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
  6473. }
  6474. /**
  6475. * xmlExpNewRange:
  6476. * @ctxt: the expression context
  6477. * @subset: the expression to be repeated
  6478. * @min: the lower bound for the repetition
  6479. * @max: the upper bound for the repetition, -1 means infinite
  6480. *
  6481. * Get the atom associated to the range (@subset){@min, @max}
  6482. * Note that @subset is consumed in the operation, to keep
  6483. * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
  6484. * this is true even in case of failure (unless ctxt == NULL).
  6485. *
  6486. * Returns the node or NULL in case of error
  6487. */
  6488. xmlExpNodePtr
  6489. xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
  6490. if (ctxt == NULL)
  6491. return(NULL);
  6492. if ((subset == NULL) || (min < 0) || (max < -1) ||
  6493. ((max >= 0) && (min > max))) {
  6494. xmlExpFree(ctxt, subset);
  6495. return(NULL);
  6496. }
  6497. return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
  6498. NULL, NULL, min, max));
  6499. }
  6500. /************************************************************************
  6501. * *
  6502. * Public API for operations on expressions *
  6503. * *
  6504. ************************************************************************/
  6505. static int
  6506. xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6507. const xmlChar**list, int len, int nb) {
  6508. int tmp, tmp2;
  6509. tail:
  6510. switch (exp->type) {
  6511. case XML_EXP_EMPTY:
  6512. return(0);
  6513. case XML_EXP_ATOM:
  6514. for (tmp = 0;tmp < nb;tmp++)
  6515. if (list[tmp] == exp->exp_str)
  6516. return(0);
  6517. if (nb >= len)
  6518. return(-2);
  6519. list[nb] = exp->exp_str;
  6520. return(1);
  6521. case XML_EXP_COUNT:
  6522. exp = exp->exp_left;
  6523. goto tail;
  6524. case XML_EXP_SEQ:
  6525. case XML_EXP_OR:
  6526. tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
  6527. if (tmp < 0)
  6528. return(tmp);
  6529. tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
  6530. nb + tmp);
  6531. if (tmp2 < 0)
  6532. return(tmp2);
  6533. return(tmp + tmp2);
  6534. }
  6535. return(-1);
  6536. }
  6537. /**
  6538. * xmlExpGetLanguage:
  6539. * @ctxt: the expression context
  6540. * @exp: the expression
  6541. * @langList: where to store the tokens
  6542. * @len: the allocated lenght of @list
  6543. *
  6544. * Find all the strings used in @exp and store them in @list
  6545. *
  6546. * Returns the number of unique strings found, -1 in case of errors and
  6547. * -2 if there is more than @len strings
  6548. */
  6549. int
  6550. xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6551. const xmlChar**langList, int len) {
  6552. if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
  6553. return(-1);
  6554. return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
  6555. }
  6556. static int
  6557. xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6558. const xmlChar**list, int len, int nb) {
  6559. int tmp, tmp2;
  6560. tail:
  6561. switch (exp->type) {
  6562. case XML_EXP_FORBID:
  6563. return(0);
  6564. case XML_EXP_EMPTY:
  6565. return(0);
  6566. case XML_EXP_ATOM:
  6567. for (tmp = 0;tmp < nb;tmp++)
  6568. if (list[tmp] == exp->exp_str)
  6569. return(0);
  6570. if (nb >= len)
  6571. return(-2);
  6572. list[nb] = exp->exp_str;
  6573. return(1);
  6574. case XML_EXP_COUNT:
  6575. exp = exp->exp_left;
  6576. goto tail;
  6577. case XML_EXP_SEQ:
  6578. tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
  6579. if (tmp < 0)
  6580. return(tmp);
  6581. if (IS_NILLABLE(exp->exp_left)) {
  6582. tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
  6583. nb + tmp);
  6584. if (tmp2 < 0)
  6585. return(tmp2);
  6586. tmp += tmp2;
  6587. }
  6588. return(tmp);
  6589. case XML_EXP_OR:
  6590. tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
  6591. if (tmp < 0)
  6592. return(tmp);
  6593. tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
  6594. nb + tmp);
  6595. if (tmp2 < 0)
  6596. return(tmp2);
  6597. return(tmp + tmp2);
  6598. }
  6599. return(-1);
  6600. }
  6601. /**
  6602. * xmlExpGetStart:
  6603. * @ctxt: the expression context
  6604. * @exp: the expression
  6605. * @tokList: where to store the tokens
  6606. * @len: the allocated lenght of @list
  6607. *
  6608. * Find all the strings that appears at the start of the languages
  6609. * accepted by @exp and store them in @list. E.g. for (a, b) | c
  6610. * it will return the list [a, c]
  6611. *
  6612. * Returns the number of unique strings found, -1 in case of errors and
  6613. * -2 if there is more than @len strings
  6614. */
  6615. int
  6616. xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6617. const xmlChar**tokList, int len) {
  6618. if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
  6619. return(-1);
  6620. return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
  6621. }
  6622. /**
  6623. * xmlExpIsNillable:
  6624. * @exp: the expression
  6625. *
  6626. * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
  6627. *
  6628. * Returns 1 if nillable, 0 if not and -1 in case of error
  6629. */
  6630. int
  6631. xmlExpIsNillable(xmlExpNodePtr exp) {
  6632. if (exp == NULL)
  6633. return(-1);
  6634. return(IS_NILLABLE(exp) != 0);
  6635. }
  6636. static xmlExpNodePtr
  6637. xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
  6638. {
  6639. xmlExpNodePtr ret;
  6640. switch (exp->type) {
  6641. case XML_EXP_EMPTY:
  6642. return(forbiddenExp);
  6643. case XML_EXP_FORBID:
  6644. return(forbiddenExp);
  6645. case XML_EXP_ATOM:
  6646. if (exp->exp_str == str) {
  6647. #ifdef DEBUG_DERIV
  6648. printf("deriv atom: equal => Empty\n");
  6649. #endif
  6650. ret = emptyExp;
  6651. } else {
  6652. #ifdef DEBUG_DERIV
  6653. printf("deriv atom: mismatch => forbid\n");
  6654. #endif
  6655. /* TODO wildcards here */
  6656. ret = forbiddenExp;
  6657. }
  6658. return(ret);
  6659. case XML_EXP_OR: {
  6660. xmlExpNodePtr tmp;
  6661. #ifdef DEBUG_DERIV
  6662. printf("deriv or: => or(derivs)\n");
  6663. #endif
  6664. tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
  6665. if (tmp == NULL) {
  6666. return(NULL);
  6667. }
  6668. ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
  6669. if (ret == NULL) {
  6670. xmlExpFree(ctxt, tmp);
  6671. return(NULL);
  6672. }
  6673. ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
  6674. NULL, 0, 0);
  6675. return(ret);
  6676. }
  6677. case XML_EXP_SEQ:
  6678. #ifdef DEBUG_DERIV
  6679. printf("deriv seq: starting with left\n");
  6680. #endif
  6681. ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
  6682. if (ret == NULL) {
  6683. return(NULL);
  6684. } else if (ret == forbiddenExp) {
  6685. if (IS_NILLABLE(exp->exp_left)) {
  6686. #ifdef DEBUG_DERIV
  6687. printf("deriv seq: left failed but nillable\n");
  6688. #endif
  6689. ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
  6690. }
  6691. } else {
  6692. #ifdef DEBUG_DERIV
  6693. printf("deriv seq: left match => sequence\n");
  6694. #endif
  6695. exp->exp_right->ref++;
  6696. ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
  6697. NULL, 0, 0);
  6698. }
  6699. return(ret);
  6700. case XML_EXP_COUNT: {
  6701. int min, max;
  6702. xmlExpNodePtr tmp;
  6703. if (exp->exp_max == 0)
  6704. return(forbiddenExp);
  6705. ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
  6706. if (ret == NULL)
  6707. return(NULL);
  6708. if (ret == forbiddenExp) {
  6709. #ifdef DEBUG_DERIV
  6710. printf("deriv count: pattern mismatch => forbid\n");
  6711. #endif
  6712. return(ret);
  6713. }
  6714. if (exp->exp_max == 1)
  6715. return(ret);
  6716. if (exp->exp_max < 0) /* unbounded */
  6717. max = -1;
  6718. else
  6719. max = exp->exp_max - 1;
  6720. if (exp->exp_min > 0)
  6721. min = exp->exp_min - 1;
  6722. else
  6723. min = 0;
  6724. exp->exp_left->ref++;
  6725. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
  6726. NULL, min, max);
  6727. if (ret == emptyExp) {
  6728. #ifdef DEBUG_DERIV
  6729. printf("deriv count: match to empty => new count\n");
  6730. #endif
  6731. return(tmp);
  6732. }
  6733. #ifdef DEBUG_DERIV
  6734. printf("deriv count: match => sequence with new count\n");
  6735. #endif
  6736. return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
  6737. NULL, 0, 0));
  6738. }
  6739. }
  6740. return(NULL);
  6741. }
  6742. /**
  6743. * xmlExpStringDerive:
  6744. * @ctxt: the expression context
  6745. * @exp: the expression
  6746. * @str: the string
  6747. * @len: the string len in bytes if available
  6748. *
  6749. * Do one step of Brzozowski derivation of the expression @exp with
  6750. * respect to the input string
  6751. *
  6752. * Returns the resulting expression or NULL in case of internal error
  6753. */
  6754. xmlExpNodePtr
  6755. xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6756. const xmlChar *str, int len) {
  6757. const xmlChar *input;
  6758. if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
  6759. return(NULL);
  6760. }
  6761. /*
  6762. * check the string is in the dictionnary, if yes use an interned
  6763. * copy, otherwise we know it's not an acceptable input
  6764. */
  6765. input = xmlDictExists(ctxt->dict, str, len);
  6766. if (input == NULL) {
  6767. return(forbiddenExp);
  6768. }
  6769. return(xmlExpStringDeriveInt(ctxt, exp, input));
  6770. }
  6771. static int
  6772. xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
  6773. int ret = 1;
  6774. if (sub->c_max == -1) {
  6775. if (exp->c_max != -1)
  6776. ret = 0;
  6777. } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
  6778. ret = 0;
  6779. }
  6780. #if 0
  6781. if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
  6782. ret = 0;
  6783. #endif
  6784. return(ret);
  6785. }
  6786. static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6787. xmlExpNodePtr sub);
  6788. /**
  6789. * xmlExpDivide:
  6790. * @ctxt: the expressions context
  6791. * @exp: the englobing expression
  6792. * @sub: the subexpression
  6793. * @mult: the multiple expression
  6794. * @remain: the remain from the derivation of the multiple
  6795. *
  6796. * Check if exp is a multiple of sub, i.e. if there is a finite number n
  6797. * so that sub{n} subsume exp
  6798. *
  6799. * Returns the multiple value if successful, 0 if it is not a multiple
  6800. * and -1 in case of internel error.
  6801. */
  6802. static int
  6803. xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
  6804. xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
  6805. int i;
  6806. xmlExpNodePtr tmp, tmp2;
  6807. if (mult != NULL) *mult = NULL;
  6808. if (remain != NULL) *remain = NULL;
  6809. if (exp->c_max == -1) return(0);
  6810. if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
  6811. for (i = 1;i <= exp->c_max;i++) {
  6812. sub->ref++;
  6813. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
  6814. sub, NULL, NULL, i, i);
  6815. if (tmp == NULL) {
  6816. return(-1);
  6817. }
  6818. if (!xmlExpCheckCard(tmp, exp)) {
  6819. xmlExpFree(ctxt, tmp);
  6820. continue;
  6821. }
  6822. tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
  6823. if (tmp2 == NULL) {
  6824. xmlExpFree(ctxt, tmp);
  6825. return(-1);
  6826. }
  6827. if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
  6828. if (remain != NULL)
  6829. *remain = tmp2;
  6830. else
  6831. xmlExpFree(ctxt, tmp2);
  6832. if (mult != NULL)
  6833. *mult = tmp;
  6834. else
  6835. xmlExpFree(ctxt, tmp);
  6836. #ifdef DEBUG_DERIV
  6837. printf("Divide succeeded %d\n", i);
  6838. #endif
  6839. return(i);
  6840. }
  6841. xmlExpFree(ctxt, tmp);
  6842. xmlExpFree(ctxt, tmp2);
  6843. }
  6844. #ifdef DEBUG_DERIV
  6845. printf("Divide failed\n");
  6846. #endif
  6847. return(0);
  6848. }
  6849. /**
  6850. * xmlExpExpDeriveInt:
  6851. * @ctxt: the expressions context
  6852. * @exp: the englobing expression
  6853. * @sub: the subexpression
  6854. *
  6855. * Try to do a step of Brzozowski derivation but at a higher level
  6856. * the input being a subexpression.
  6857. *
  6858. * Returns the resulting expression or NULL in case of internal error
  6859. */
  6860. static xmlExpNodePtr
  6861. xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
  6862. xmlExpNodePtr ret, tmp, tmp2, tmp3;
  6863. const xmlChar **tab;
  6864. int len, i;
  6865. /*
  6866. * In case of equality and if the expression can only consume a finite
  6867. * amount, then the derivation is empty
  6868. */
  6869. if ((exp == sub) && (exp->c_max >= 0)) {
  6870. #ifdef DEBUG_DERIV
  6871. printf("Equal(exp, sub) and finite -> Empty\n");
  6872. #endif
  6873. return(emptyExp);
  6874. }
  6875. /*
  6876. * decompose sub sequence first
  6877. */
  6878. if (sub->type == XML_EXP_EMPTY) {
  6879. #ifdef DEBUG_DERIV
  6880. printf("Empty(sub) -> Empty\n");
  6881. #endif
  6882. exp->ref++;
  6883. return(exp);
  6884. }
  6885. if (sub->type == XML_EXP_SEQ) {
  6886. #ifdef DEBUG_DERIV
  6887. printf("Seq(sub) -> decompose\n");
  6888. #endif
  6889. tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
  6890. if (tmp == NULL)
  6891. return(NULL);
  6892. if (tmp == forbiddenExp)
  6893. return(tmp);
  6894. ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
  6895. xmlExpFree(ctxt, tmp);
  6896. return(ret);
  6897. }
  6898. if (sub->type == XML_EXP_OR) {
  6899. #ifdef DEBUG_DERIV
  6900. printf("Or(sub) -> decompose\n");
  6901. #endif
  6902. tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
  6903. if (tmp == forbiddenExp)
  6904. return(tmp);
  6905. if (tmp == NULL)
  6906. return(NULL);
  6907. ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
  6908. if ((ret == NULL) || (ret == forbiddenExp)) {
  6909. xmlExpFree(ctxt, tmp);
  6910. return(ret);
  6911. }
  6912. return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
  6913. }
  6914. if (!xmlExpCheckCard(exp, sub)) {
  6915. #ifdef DEBUG_DERIV
  6916. printf("CheckCard(exp, sub) failed -> Forbid\n");
  6917. #endif
  6918. return(forbiddenExp);
  6919. }
  6920. switch (exp->type) {
  6921. case XML_EXP_EMPTY:
  6922. if (sub == emptyExp)
  6923. return(emptyExp);
  6924. #ifdef DEBUG_DERIV
  6925. printf("Empty(exp) -> Forbid\n");
  6926. #endif
  6927. return(forbiddenExp);
  6928. case XML_EXP_FORBID:
  6929. #ifdef DEBUG_DERIV
  6930. printf("Forbid(exp) -> Forbid\n");
  6931. #endif
  6932. return(forbiddenExp);
  6933. case XML_EXP_ATOM:
  6934. if (sub->type == XML_EXP_ATOM) {
  6935. /* TODO: handle wildcards */
  6936. if (exp->exp_str == sub->exp_str) {
  6937. #ifdef DEBUG_DERIV
  6938. printf("Atom match -> Empty\n");
  6939. #endif
  6940. return(emptyExp);
  6941. }
  6942. #ifdef DEBUG_DERIV
  6943. printf("Atom mismatch -> Forbid\n");
  6944. #endif
  6945. return(forbiddenExp);
  6946. }
  6947. if ((sub->type == XML_EXP_COUNT) &&
  6948. (sub->exp_max == 1) &&
  6949. (sub->exp_left->type == XML_EXP_ATOM)) {
  6950. /* TODO: handle wildcards */
  6951. if (exp->exp_str == sub->exp_left->exp_str) {
  6952. #ifdef DEBUG_DERIV
  6953. printf("Atom match -> Empty\n");
  6954. #endif
  6955. return(emptyExp);
  6956. }
  6957. #ifdef DEBUG_DERIV
  6958. printf("Atom mismatch -> Forbid\n");
  6959. #endif
  6960. return(forbiddenExp);
  6961. }
  6962. #ifdef DEBUG_DERIV
  6963. printf("Compex exp vs Atom -> Forbid\n");
  6964. #endif
  6965. return(forbiddenExp);
  6966. case XML_EXP_SEQ:
  6967. /* try to get the sequence consumed only if possible */
  6968. if (xmlExpCheckCard(exp->exp_left, sub)) {
  6969. /* See if the sequence can be consumed directly */
  6970. #ifdef DEBUG_DERIV
  6971. printf("Seq trying left only\n");
  6972. #endif
  6973. ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
  6974. if ((ret != forbiddenExp) && (ret != NULL)) {
  6975. #ifdef DEBUG_DERIV
  6976. printf("Seq trying left only worked\n");
  6977. #endif
  6978. /*
  6979. * TODO: assumption here that we are determinist
  6980. * i.e. we won't get to a nillable exp left
  6981. * subset which could be matched by the right
  6982. * part too.
  6983. * e.g.: (a | b)+,(a | c) and 'a+,a'
  6984. */
  6985. exp->exp_right->ref++;
  6986. return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
  6987. exp->exp_right, NULL, 0, 0));
  6988. }
  6989. #ifdef DEBUG_DERIV
  6990. } else {
  6991. printf("Seq: left too short\n");
  6992. #endif
  6993. }
  6994. /* Try instead to decompose */
  6995. if (sub->type == XML_EXP_COUNT) {
  6996. int min, max;
  6997. #ifdef DEBUG_DERIV
  6998. printf("Seq: sub is a count\n");
  6999. #endif
  7000. ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
  7001. if (ret == NULL)
  7002. return(NULL);
  7003. if (ret != forbiddenExp) {
  7004. #ifdef DEBUG_DERIV
  7005. printf("Seq , Count match on left\n");
  7006. #endif
  7007. if (sub->exp_max < 0)
  7008. max = -1;
  7009. else
  7010. max = sub->exp_max -1;
  7011. if (sub->exp_min > 0)
  7012. min = sub->exp_min -1;
  7013. else
  7014. min = 0;
  7015. exp->exp_right->ref++;
  7016. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
  7017. exp->exp_right, NULL, 0, 0);
  7018. if (tmp == NULL)
  7019. return(NULL);
  7020. sub->exp_left->ref++;
  7021. tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
  7022. sub->exp_left, NULL, NULL, min, max);
  7023. if (tmp2 == NULL) {
  7024. xmlExpFree(ctxt, tmp);
  7025. return(NULL);
  7026. }
  7027. ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
  7028. xmlExpFree(ctxt, tmp);
  7029. xmlExpFree(ctxt, tmp2);
  7030. return(ret);
  7031. }
  7032. }
  7033. /* we made no progress on structured operations */
  7034. break;
  7035. case XML_EXP_OR:
  7036. #ifdef DEBUG_DERIV
  7037. printf("Or , trying both side\n");
  7038. #endif
  7039. ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
  7040. if (ret == NULL)
  7041. return(NULL);
  7042. tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
  7043. if (tmp == NULL) {
  7044. xmlExpFree(ctxt, ret);
  7045. return(NULL);
  7046. }
  7047. return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
  7048. case XML_EXP_COUNT: {
  7049. int min, max;
  7050. if (sub->type == XML_EXP_COUNT) {
  7051. /*
  7052. * Try to see if the loop is completely subsumed
  7053. */
  7054. tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
  7055. if (tmp == NULL)
  7056. return(NULL);
  7057. if (tmp == forbiddenExp) {
  7058. int mult;
  7059. #ifdef DEBUG_DERIV
  7060. printf("Count, Count inner don't subsume\n");
  7061. #endif
  7062. mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
  7063. NULL, &tmp);
  7064. if (mult <= 0) {
  7065. #ifdef DEBUG_DERIV
  7066. printf("Count, Count not multiple => forbidden\n");
  7067. #endif
  7068. return(forbiddenExp);
  7069. }
  7070. if (sub->exp_max == -1) {
  7071. max = -1;
  7072. if (exp->exp_max == -1) {
  7073. if (exp->exp_min <= sub->exp_min * mult)
  7074. min = 0;
  7075. else
  7076. min = exp->exp_min - sub->exp_min * mult;
  7077. } else {
  7078. #ifdef DEBUG_DERIV
  7079. printf("Count, Count finite can't subsume infinite\n");
  7080. #endif
  7081. xmlExpFree(ctxt, tmp);
  7082. return(forbiddenExp);
  7083. }
  7084. } else {
  7085. if (exp->exp_max == -1) {
  7086. #ifdef DEBUG_DERIV
  7087. printf("Infinite loop consume mult finite loop\n");
  7088. #endif
  7089. if (exp->exp_min > sub->exp_min * mult) {
  7090. max = -1;
  7091. min = exp->exp_min - sub->exp_min * mult;
  7092. } else {
  7093. max = -1;
  7094. min = 0;
  7095. }
  7096. } else {
  7097. if (exp->exp_max < sub->exp_max * mult) {
  7098. #ifdef DEBUG_DERIV
  7099. printf("loops max mult mismatch => forbidden\n");
  7100. #endif
  7101. xmlExpFree(ctxt, tmp);
  7102. return(forbiddenExp);
  7103. }
  7104. if (sub->exp_max * mult > exp->exp_min)
  7105. min = 0;
  7106. else
  7107. min = exp->exp_min - sub->exp_max * mult;
  7108. max = exp->exp_max - sub->exp_max * mult;
  7109. }
  7110. }
  7111. } else if (!IS_NILLABLE(tmp)) {
  7112. /*
  7113. * TODO: loop here to try to grow if working on finite
  7114. * blocks.
  7115. */
  7116. #ifdef DEBUG_DERIV
  7117. printf("Count, Count remain not nillable => forbidden\n");
  7118. #endif
  7119. xmlExpFree(ctxt, tmp);
  7120. return(forbiddenExp);
  7121. } else if (sub->exp_max == -1) {
  7122. if (exp->exp_max == -1) {
  7123. if (exp->exp_min <= sub->exp_min) {
  7124. #ifdef DEBUG_DERIV
  7125. printf("Infinite loops Okay => COUNT(0,Inf)\n");
  7126. #endif
  7127. max = -1;
  7128. min = 0;
  7129. } else {
  7130. #ifdef DEBUG_DERIV
  7131. printf("Infinite loops min => Count(X,Inf)\n");
  7132. #endif
  7133. max = -1;
  7134. min = exp->exp_min - sub->exp_min;
  7135. }
  7136. } else if (exp->exp_min > sub->exp_min) {
  7137. #ifdef DEBUG_DERIV
  7138. printf("loops min mismatch 1 => forbidden ???\n");
  7139. #endif
  7140. xmlExpFree(ctxt, tmp);
  7141. return(forbiddenExp);
  7142. } else {
  7143. max = -1;
  7144. min = 0;
  7145. }
  7146. } else {
  7147. if (exp->exp_max == -1) {
  7148. #ifdef DEBUG_DERIV
  7149. printf("Infinite loop consume finite loop\n");
  7150. #endif
  7151. if (exp->exp_min > sub->exp_min) {
  7152. max = -1;
  7153. min = exp->exp_min - sub->exp_min;
  7154. } else {
  7155. max = -1;
  7156. min = 0;
  7157. }
  7158. } else {
  7159. if (exp->exp_max < sub->exp_max) {
  7160. #ifdef DEBUG_DERIV
  7161. printf("loops max mismatch => forbidden\n");
  7162. #endif
  7163. xmlExpFree(ctxt, tmp);
  7164. return(forbiddenExp);
  7165. }
  7166. if (sub->exp_max > exp->exp_min)
  7167. min = 0;
  7168. else
  7169. min = exp->exp_min - sub->exp_max;
  7170. max = exp->exp_max - sub->exp_max;
  7171. }
  7172. }
  7173. #ifdef DEBUG_DERIV
  7174. printf("loops match => SEQ(COUNT())\n");
  7175. #endif
  7176. exp->exp_left->ref++;
  7177. tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
  7178. NULL, NULL, min, max);
  7179. if (tmp2 == NULL) {
  7180. return(NULL);
  7181. }
  7182. ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
  7183. NULL, 0, 0);
  7184. return(ret);
  7185. }
  7186. tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
  7187. if (tmp == NULL)
  7188. return(NULL);
  7189. if (tmp == forbiddenExp) {
  7190. #ifdef DEBUG_DERIV
  7191. printf("loop mismatch => forbidden\n");
  7192. #endif
  7193. return(forbiddenExp);
  7194. }
  7195. if (exp->exp_min > 0)
  7196. min = exp->exp_min - 1;
  7197. else
  7198. min = 0;
  7199. if (exp->exp_max < 0)
  7200. max = -1;
  7201. else
  7202. max = exp->exp_max - 1;
  7203. #ifdef DEBUG_DERIV
  7204. printf("loop match => SEQ(COUNT())\n");
  7205. #endif
  7206. exp->exp_left->ref++;
  7207. tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
  7208. NULL, NULL, min, max);
  7209. if (tmp2 == NULL)
  7210. return(NULL);
  7211. ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
  7212. NULL, 0, 0);
  7213. return(ret);
  7214. }
  7215. }
  7216. #ifdef DEBUG_DERIV
  7217. printf("Fallback to derivative\n");
  7218. #endif
  7219. if (IS_NILLABLE(sub)) {
  7220. if (!(IS_NILLABLE(exp)))
  7221. return(forbiddenExp);
  7222. else
  7223. ret = emptyExp;
  7224. } else
  7225. ret = NULL;
  7226. /*
  7227. * here the structured derivation made no progress so
  7228. * we use the default token based derivation to force one more step
  7229. */
  7230. if (ctxt->tabSize == 0)
  7231. ctxt->tabSize = 40;
  7232. tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
  7233. sizeof(const xmlChar *));
  7234. if (tab == NULL) {
  7235. return(NULL);
  7236. }
  7237. /*
  7238. * collect all the strings accepted by the subexpression on input
  7239. */
  7240. len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
  7241. while (len < 0) {
  7242. const xmlChar **temp;
  7243. temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
  7244. sizeof(const xmlChar *));
  7245. if (temp == NULL) {
  7246. xmlFree((xmlChar **) tab);
  7247. return(NULL);
  7248. }
  7249. tab = temp;
  7250. ctxt->tabSize *= 2;
  7251. len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
  7252. }
  7253. for (i = 0;i < len;i++) {
  7254. tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
  7255. if ((tmp == NULL) || (tmp == forbiddenExp)) {
  7256. xmlExpFree(ctxt, ret);
  7257. xmlFree((xmlChar **) tab);
  7258. return(tmp);
  7259. }
  7260. tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
  7261. if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
  7262. xmlExpFree(ctxt, tmp);
  7263. xmlExpFree(ctxt, ret);
  7264. xmlFree((xmlChar **) tab);
  7265. return(tmp);
  7266. }
  7267. tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
  7268. xmlExpFree(ctxt, tmp);
  7269. xmlExpFree(ctxt, tmp2);
  7270. if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
  7271. xmlExpFree(ctxt, ret);
  7272. xmlFree((xmlChar **) tab);
  7273. return(tmp3);
  7274. }
  7275. if (ret == NULL)
  7276. ret = tmp3;
  7277. else {
  7278. ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
  7279. if (ret == NULL) {
  7280. xmlFree((xmlChar **) tab);
  7281. return(NULL);
  7282. }
  7283. }
  7284. }
  7285. xmlFree((xmlChar **) tab);
  7286. return(ret);
  7287. }
  7288. /**
  7289. * xmlExpExpDerive:
  7290. * @ctxt: the expressions context
  7291. * @exp: the englobing expression
  7292. * @sub: the subexpression
  7293. *
  7294. * Evaluates the expression resulting from @exp consuming a sub expression @sub
  7295. * Based on algebraic derivation and sometimes direct Brzozowski derivation
  7296. * it usually tatkes less than linear time and can handle expressions generating
  7297. * infinite languages.
  7298. *
  7299. * Returns the resulting expression or NULL in case of internal error, the
  7300. * result must be freed
  7301. */
  7302. xmlExpNodePtr
  7303. xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
  7304. if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
  7305. return(NULL);
  7306. /*
  7307. * O(1) speedups
  7308. */
  7309. if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
  7310. #ifdef DEBUG_DERIV
  7311. printf("Sub nillable and not exp : can't subsume\n");
  7312. #endif
  7313. return(forbiddenExp);
  7314. }
  7315. if (xmlExpCheckCard(exp, sub) == 0) {
  7316. #ifdef DEBUG_DERIV
  7317. printf("sub generate longuer sequances than exp : can't subsume\n");
  7318. #endif
  7319. return(forbiddenExp);
  7320. }
  7321. return(xmlExpExpDeriveInt(ctxt, exp, sub));
  7322. }
  7323. /**
  7324. * xmlExpSubsume:
  7325. * @ctxt: the expressions context
  7326. * @exp: the englobing expression
  7327. * @sub: the subexpression
  7328. *
  7329. * Check whether @exp accepts all the languages accexpted by @sub
  7330. * the input being a subexpression.
  7331. *
  7332. * Returns 1 if true 0 if false and -1 in case of failure.
  7333. */
  7334. int
  7335. xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
  7336. xmlExpNodePtr tmp;
  7337. if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
  7338. return(-1);
  7339. /*
  7340. * TODO: speedup by checking the language of sub is a subset of the
  7341. * language of exp
  7342. */
  7343. /*
  7344. * O(1) speedups
  7345. */
  7346. if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
  7347. #ifdef DEBUG_DERIV
  7348. printf("Sub nillable and not exp : can't subsume\n");
  7349. #endif
  7350. return(0);
  7351. }
  7352. if (xmlExpCheckCard(exp, sub) == 0) {
  7353. #ifdef DEBUG_DERIV
  7354. printf("sub generate longuer sequances than exp : can't subsume\n");
  7355. #endif
  7356. return(0);
  7357. }
  7358. tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
  7359. #ifdef DEBUG_DERIV
  7360. printf("Result derivation :\n");
  7361. PRINT_EXP(tmp);
  7362. #endif
  7363. if (tmp == NULL)
  7364. return(-1);
  7365. if (tmp == forbiddenExp)
  7366. return(0);
  7367. if (tmp == emptyExp)
  7368. return(1);
  7369. if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
  7370. xmlExpFree(ctxt, tmp);
  7371. return(1);
  7372. }
  7373. xmlExpFree(ctxt, tmp);
  7374. return(0);
  7375. }
  7376. /************************************************************************
  7377. * *
  7378. * Parsing expression *
  7379. * *
  7380. ************************************************************************/
  7381. static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
  7382. #undef CUR
  7383. #define CUR (*ctxt->cur)
  7384. #undef NEXT
  7385. #define NEXT ctxt->cur++;
  7386. #undef IS_BLANK
  7387. #define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
  7388. #define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
  7389. static int
  7390. xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
  7391. int ret = 0;
  7392. SKIP_BLANKS
  7393. if (CUR == '*') {
  7394. NEXT
  7395. return(-1);
  7396. }
  7397. if ((CUR < '0') || (CUR > '9'))
  7398. return(-1);
  7399. while ((CUR >= '0') && (CUR <= '9')) {
  7400. ret = ret * 10 + (CUR - '0');
  7401. NEXT
  7402. }
  7403. return(ret);
  7404. }
  7405. static xmlExpNodePtr
  7406. xmlExpParseOr(xmlExpCtxtPtr ctxt) {
  7407. const char *base;
  7408. xmlExpNodePtr ret;
  7409. const xmlChar *val;
  7410. SKIP_BLANKS
  7411. base = ctxt->cur;
  7412. if (*ctxt->cur == '(') {
  7413. NEXT
  7414. ret = xmlExpParseExpr(ctxt);
  7415. SKIP_BLANKS
  7416. if (*ctxt->cur != ')') {
  7417. fprintf(stderr, "unbalanced '(' : %s\n", base);
  7418. xmlExpFree(ctxt, ret);
  7419. return(NULL);
  7420. }
  7421. NEXT;
  7422. SKIP_BLANKS
  7423. goto parse_quantifier;
  7424. }
  7425. while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
  7426. (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
  7427. (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
  7428. NEXT;
  7429. val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
  7430. if (val == NULL)
  7431. return(NULL);
  7432. ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
  7433. if (ret == NULL)
  7434. return(NULL);
  7435. SKIP_BLANKS
  7436. parse_quantifier:
  7437. if (CUR == '{') {
  7438. int min, max;
  7439. NEXT
  7440. min = xmlExpParseNumber(ctxt);
  7441. if (min < 0) {
  7442. xmlExpFree(ctxt, ret);
  7443. return(NULL);
  7444. }
  7445. SKIP_BLANKS
  7446. if (CUR == ',') {
  7447. NEXT
  7448. max = xmlExpParseNumber(ctxt);
  7449. SKIP_BLANKS
  7450. } else
  7451. max = min;
  7452. if (CUR != '}') {
  7453. xmlExpFree(ctxt, ret);
  7454. return(NULL);
  7455. }
  7456. NEXT
  7457. ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
  7458. min, max);
  7459. SKIP_BLANKS
  7460. } else if (CUR == '?') {
  7461. NEXT
  7462. ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
  7463. 0, 1);
  7464. SKIP_BLANKS
  7465. } else if (CUR == '+') {
  7466. NEXT
  7467. ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
  7468. 1, -1);
  7469. SKIP_BLANKS
  7470. } else if (CUR == '*') {
  7471. NEXT
  7472. ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
  7473. 0, -1);
  7474. SKIP_BLANKS
  7475. }
  7476. return(ret);
  7477. }
  7478. static xmlExpNodePtr
  7479. xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
  7480. xmlExpNodePtr ret, right;
  7481. ret = xmlExpParseOr(ctxt);
  7482. SKIP_BLANKS
  7483. while (CUR == '|') {
  7484. NEXT
  7485. right = xmlExpParseOr(ctxt);
  7486. if (right == NULL) {
  7487. xmlExpFree(ctxt, ret);
  7488. return(NULL);
  7489. }
  7490. ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
  7491. if (ret == NULL)
  7492. return(NULL);
  7493. }
  7494. return(ret);
  7495. }
  7496. static xmlExpNodePtr
  7497. xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
  7498. xmlExpNodePtr ret, right;
  7499. ret = xmlExpParseSeq(ctxt);
  7500. SKIP_BLANKS
  7501. while (CUR == ',') {
  7502. NEXT
  7503. right = xmlExpParseSeq(ctxt);
  7504. if (right == NULL) {
  7505. xmlExpFree(ctxt, ret);
  7506. return(NULL);
  7507. }
  7508. ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
  7509. if (ret == NULL)
  7510. return(NULL);
  7511. }
  7512. return(ret);
  7513. }
  7514. /**
  7515. * xmlExpParse:
  7516. * @ctxt: the expressions context
  7517. * @expr: the 0 terminated string
  7518. *
  7519. * Minimal parser for regexps, it understand the following constructs
  7520. * - string terminals
  7521. * - choice operator |
  7522. * - sequence operator ,
  7523. * - subexpressions (...)
  7524. * - usual cardinality operators + * and ?
  7525. * - finite sequences { min, max }
  7526. * - infinite sequences { min, * }
  7527. * There is minimal checkings made especially no checking on strings values
  7528. *
  7529. * Returns a new expression or NULL in case of failure
  7530. */
  7531. xmlExpNodePtr
  7532. xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
  7533. xmlExpNodePtr ret;
  7534. ctxt->expr = expr;
  7535. ctxt->cur = expr;
  7536. ret = xmlExpParseExpr(ctxt);
  7537. SKIP_BLANKS
  7538. if (*ctxt->cur != 0) {
  7539. xmlExpFree(ctxt, ret);
  7540. return(NULL);
  7541. }
  7542. return(ret);
  7543. }
  7544. static void
  7545. xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
  7546. xmlExpNodePtr c;
  7547. if (expr == NULL) return;
  7548. if (glob) xmlBufferWriteChar(buf, "(");
  7549. switch (expr->type) {
  7550. case XML_EXP_EMPTY:
  7551. xmlBufferWriteChar(buf, "empty");
  7552. break;
  7553. case XML_EXP_FORBID:
  7554. xmlBufferWriteChar(buf, "forbidden");
  7555. break;
  7556. case XML_EXP_ATOM:
  7557. xmlBufferWriteCHAR(buf, expr->exp_str);
  7558. break;
  7559. case XML_EXP_SEQ:
  7560. c = expr->exp_left;
  7561. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7562. xmlExpDumpInt(buf, c, 1);
  7563. else
  7564. xmlExpDumpInt(buf, c, 0);
  7565. xmlBufferWriteChar(buf, " , ");
  7566. c = expr->exp_right;
  7567. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7568. xmlExpDumpInt(buf, c, 1);
  7569. else
  7570. xmlExpDumpInt(buf, c, 0);
  7571. break;
  7572. case XML_EXP_OR:
  7573. c = expr->exp_left;
  7574. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7575. xmlExpDumpInt(buf, c, 1);
  7576. else
  7577. xmlExpDumpInt(buf, c, 0);
  7578. xmlBufferWriteChar(buf, " | ");
  7579. c = expr->exp_right;
  7580. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7581. xmlExpDumpInt(buf, c, 1);
  7582. else
  7583. xmlExpDumpInt(buf, c, 0);
  7584. break;
  7585. case XML_EXP_COUNT: {
  7586. char rep[40];
  7587. c = expr->exp_left;
  7588. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7589. xmlExpDumpInt(buf, c, 1);
  7590. else
  7591. xmlExpDumpInt(buf, c, 0);
  7592. if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
  7593. rep[0] = '?';
  7594. rep[1] = 0;
  7595. } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
  7596. rep[0] = '*';
  7597. rep[1] = 0;
  7598. } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
  7599. rep[0] = '+';
  7600. rep[1] = 0;
  7601. } else if (expr->exp_max == expr->exp_min) {
  7602. snprintf(rep, 39, "{%d}", expr->exp_min);
  7603. } else if (expr->exp_max < 0) {
  7604. snprintf(rep, 39, "{%d,inf}", expr->exp_min);
  7605. } else {
  7606. snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
  7607. }
  7608. rep[39] = 0;
  7609. xmlBufferWriteChar(buf, rep);
  7610. break;
  7611. }
  7612. default:
  7613. fprintf(stderr, "Error in tree\n");
  7614. }
  7615. if (glob)
  7616. xmlBufferWriteChar(buf, ")");
  7617. }
  7618. /**
  7619. * xmlExpDump:
  7620. * @buf: a buffer to receive the output
  7621. * @expr: the compiled expression
  7622. *
  7623. * Serialize the expression as compiled to the buffer
  7624. */
  7625. void
  7626. xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
  7627. if ((buf == NULL) || (expr == NULL))
  7628. return;
  7629. xmlExpDumpInt(buf, expr, 0);
  7630. }
  7631. /**
  7632. * xmlExpMaxToken:
  7633. * @expr: a compiled expression
  7634. *
  7635. * Indicate the maximum number of input a expression can accept
  7636. *
  7637. * Returns the maximum length or -1 in case of error
  7638. */
  7639. int
  7640. xmlExpMaxToken(xmlExpNodePtr expr) {
  7641. if (expr == NULL)
  7642. return(-1);
  7643. return(expr->c_max);
  7644. }
  7645. /**
  7646. * xmlExpCtxtNbNodes:
  7647. * @ctxt: an expression context
  7648. *
  7649. * Debugging facility provides the number of allocated nodes at a that point
  7650. *
  7651. * Returns the number of nodes in use or -1 in case of error
  7652. */
  7653. int
  7654. xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
  7655. if (ctxt == NULL)
  7656. return(-1);
  7657. return(ctxt->nb_nodes);
  7658. }
  7659. /**
  7660. * xmlExpCtxtNbCons:
  7661. * @ctxt: an expression context
  7662. *
  7663. * Debugging facility provides the number of allocated nodes over lifetime
  7664. *
  7665. * Returns the number of nodes ever allocated or -1 in case of error
  7666. */
  7667. int
  7668. xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
  7669. if (ctxt == NULL)
  7670. return(-1);
  7671. return(ctxt->nb_cons);
  7672. }
  7673. #endif /* LIBXML_EXPR_ENABLED */
  7674. #define bottom_xmlregexp
  7675. #include "elfgcchack.h"
  7676. #endif /* LIBXML_REGEXP_ENABLED */