xmlregexp.c 214 KB

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