xmlregexp.c 217 KB

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