pcre_exec.c 213 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2018 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This module contains pcre_exec(), the externally visible function that does
  33. pattern matching using an NFA algorithm, trying to mimic Perl as closely as
  34. possible. There are also some static supporting functions. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #define NLBLOCK md /* Block containing newline information */
  39. #define PSSTART start_subject /* Field containing processed string start */
  40. #define PSEND end_subject /* Field containing processed string end */
  41. #include "pcre_internal.h"
  42. /* Undefine some potentially clashing cpp symbols */
  43. #undef min
  44. #undef max
  45. /* The md->capture_last field uses the lower 16 bits for the last captured
  46. substring (which can never be greater than 65535) and a bit in the top half
  47. to mean "capture vector overflowed". This odd way of doing things was
  48. implemented when it was realized that preserving and restoring the overflow bit
  49. whenever the last capture number was saved/restored made for a neater
  50. interface, and doing it this way saved on (a) another variable, which would
  51. have increased the stack frame size (a big NO-NO in PCRE) and (b) another
  52. separate set of save/restore instructions. The following defines are used in
  53. implementing this. */
  54. #define CAPLMASK 0x0000ffff /* The bits used for last_capture */
  55. #define OVFLMASK 0xffff0000 /* The bits used for the overflow flag */
  56. #define OVFLBIT 0x00010000 /* The bit that is set for overflow */
  57. /* Values for setting in md->match_function_type to indicate two special types
  58. of call to match(). We do it this way to save on using another stack variable,
  59. as stack usage is to be discouraged. */
  60. #define MATCH_CONDASSERT 1 /* Called to check a condition assertion */
  61. #define MATCH_CBEGROUP 2 /* Could-be-empty unlimited repeat group */
  62. /* Non-error returns from the match() function. Error returns are externally
  63. defined PCRE_ERROR_xxx codes, which are all negative. */
  64. #define MATCH_MATCH 1
  65. #define MATCH_NOMATCH 0
  66. /* Special internal returns from the match() function. Make them sufficiently
  67. negative to avoid the external error codes. */
  68. #define MATCH_ACCEPT (-999)
  69. #define MATCH_KETRPOS (-998)
  70. #define MATCH_ONCE (-997)
  71. /* The next 5 must be kept together and in sequence so that a test that checks
  72. for any one of them can use a range. */
  73. #define MATCH_COMMIT (-996)
  74. #define MATCH_PRUNE (-995)
  75. #define MATCH_SKIP (-994)
  76. #define MATCH_SKIP_ARG (-993)
  77. #define MATCH_THEN (-992)
  78. #define MATCH_BACKTRACK_MAX MATCH_THEN
  79. #define MATCH_BACKTRACK_MIN MATCH_COMMIT
  80. /* Maximum number of ints of offset to save on the stack for recursive calls.
  81. If the offset vector is bigger, malloc is used. This should be a multiple of 3,
  82. because the offset vector is always a multiple of 3 long. */
  83. #define REC_STACK_SAVE_MAX 30
  84. /* Min and max values for the common repeats; for the maxima, 0 => infinity */
  85. static const char rep_min[] = { 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, };
  86. static const char rep_max[] = { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, };
  87. #ifdef PCRE_DEBUG
  88. /*************************************************
  89. * Debugging function to print chars *
  90. *************************************************/
  91. /* Print a sequence of chars in printable format, stopping at the end of the
  92. subject if the requested.
  93. Arguments:
  94. p points to characters
  95. length number to print
  96. is_subject TRUE if printing from within md->start_subject
  97. md pointer to matching data block, if is_subject is TRUE
  98. Returns: nothing
  99. */
  100. static void
  101. pchars(const pcre_uchar *p, int length, BOOL is_subject, match_data *md)
  102. {
  103. pcre_uint32 c;
  104. BOOL utf = md->utf;
  105. if (is_subject && length > md->end_subject - p) length = md->end_subject - p;
  106. while (length-- > 0)
  107. if (isprint(c = UCHAR21INCTEST(p))) printf("%c", (char)c); else printf("\\x{%02x}", c);
  108. }
  109. #endif
  110. /*************************************************
  111. * Match a back-reference *
  112. *************************************************/
  113. /* Normally, if a back reference hasn't been set, the length that is passed is
  114. negative, so the match always fails. However, in JavaScript compatibility mode,
  115. the length passed is zero. Note that in caseless UTF-8 mode, the number of
  116. subject bytes matched may be different to the number of reference bytes.
  117. Arguments:
  118. offset index into the offset vector
  119. eptr pointer into the subject
  120. length length of reference to be matched (number of bytes)
  121. md points to match data block
  122. caseless TRUE if caseless
  123. Returns: >= 0 the number of subject bytes matched
  124. -1 no match
  125. -2 partial match; always given if at end subject
  126. */
  127. static int
  128. match_ref(int offset, register PCRE_PUCHAR eptr, int length, match_data *md,
  129. BOOL caseless)
  130. {
  131. PCRE_PUCHAR eptr_start = eptr;
  132. register PCRE_PUCHAR p = md->start_subject + md->offset_vector[offset];
  133. #if defined SUPPORT_UTF && defined SUPPORT_UCP
  134. BOOL utf = md->utf;
  135. #endif
  136. #ifdef PCRE_DEBUG
  137. if (eptr >= md->end_subject)
  138. printf("matching subject <null>");
  139. else
  140. {
  141. printf("matching subject ");
  142. pchars(eptr, length, TRUE, md);
  143. }
  144. printf(" against backref ");
  145. pchars(p, length, FALSE, md);
  146. printf("\n");
  147. #endif
  148. /* Always fail if reference not set (and not JavaScript compatible - in that
  149. case the length is passed as zero). */
  150. if (length < 0) return -1;
  151. /* Separate the caseless case for speed. In UTF-8 mode we can only do this
  152. properly if Unicode properties are supported. Otherwise, we can check only
  153. ASCII characters. */
  154. if (caseless)
  155. {
  156. #if defined SUPPORT_UTF && defined SUPPORT_UCP
  157. if (utf)
  158. {
  159. /* Match characters up to the end of the reference. NOTE: the number of
  160. data units matched may differ, because in UTF-8 there are some characters
  161. whose upper and lower case versions code have different numbers of bytes.
  162. For example, U+023A (2 bytes in UTF-8) is the upper case version of U+2C65
  163. (3 bytes in UTF-8); a sequence of 3 of the former uses 6 bytes, as does a
  164. sequence of two of the latter. It is important, therefore, to check the
  165. length along the reference, not along the subject (earlier code did this
  166. wrong). */
  167. PCRE_PUCHAR endptr = p + length;
  168. while (p < endptr)
  169. {
  170. pcre_uint32 c, d;
  171. const ucd_record *ur;
  172. if (eptr >= md->end_subject) return -2; /* Partial match */
  173. GETCHARINC(c, eptr);
  174. GETCHARINC(d, p);
  175. ur = GET_UCD(d);
  176. if (c != d && c != d + ur->other_case)
  177. {
  178. const pcre_uint32 *pp = PRIV(ucd_caseless_sets) + ur->caseset;
  179. for (;;)
  180. {
  181. if (c < *pp) return -1;
  182. if (c == *pp++) break;
  183. }
  184. }
  185. }
  186. }
  187. else
  188. #endif
  189. /* The same code works when not in UTF-8 mode and in UTF-8 mode when there
  190. is no UCP support. */
  191. {
  192. while (length-- > 0)
  193. {
  194. pcre_uint32 cc, cp;
  195. if (eptr >= md->end_subject) return -2; /* Partial match */
  196. cc = UCHAR21TEST(eptr);
  197. cp = UCHAR21TEST(p);
  198. if (TABLE_GET(cp, md->lcc, cp) != TABLE_GET(cc, md->lcc, cc)) return -1;
  199. p++;
  200. eptr++;
  201. }
  202. }
  203. }
  204. /* In the caseful case, we can just compare the bytes, whether or not we
  205. are in UTF-8 mode. */
  206. else
  207. {
  208. while (length-- > 0)
  209. {
  210. if (eptr >= md->end_subject) return -2; /* Partial match */
  211. if (UCHAR21INCTEST(p) != UCHAR21INCTEST(eptr)) return -1;
  212. }
  213. }
  214. return (int)(eptr - eptr_start);
  215. }
  216. /***************************************************************************
  217. ****************************************************************************
  218. RECURSION IN THE match() FUNCTION
  219. The match() function is highly recursive, though not every recursive call
  220. increases the recursive depth. Nevertheless, some regular expressions can cause
  221. it to recurse to a great depth. I was writing for Unix, so I just let it call
  222. itself recursively. This uses the stack for saving everything that has to be
  223. saved for a recursive call. On Unix, the stack can be large, and this works
  224. fine.
  225. It turns out that on some non-Unix-like systems there are problems with
  226. programs that use a lot of stack. (This despite the fact that every last chip
  227. has oodles of memory these days, and techniques for extending the stack have
  228. been known for decades.) So....
  229. There is a fudge, triggered by defining NO_RECURSE, which avoids recursive
  230. calls by keeping local variables that need to be preserved in blocks of memory
  231. obtained from malloc() instead instead of on the stack. Macros are used to
  232. achieve this so that the actual code doesn't look very different to what it
  233. always used to.
  234. The original heap-recursive code used longjmp(). However, it seems that this
  235. can be very slow on some operating systems. Following a suggestion from Stan
  236. Switzer, the use of longjmp() has been abolished, at the cost of having to
  237. provide a unique number for each call to RMATCH. There is no way of generating
  238. a sequence of numbers at compile time in C. I have given them names, to make
  239. them stand out more clearly.
  240. Crude tests on x86 Linux show a small speedup of around 5-8%. However, on
  241. FreeBSD, avoiding longjmp() more than halves the time taken to run the standard
  242. tests. Furthermore, not using longjmp() means that local dynamic variables
  243. don't have indeterminate values; this has meant that the frame size can be
  244. reduced because the result can be "passed back" by straight setting of the
  245. variable instead of being passed in the frame.
  246. ****************************************************************************
  247. ***************************************************************************/
  248. /* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN
  249. below must be updated in sync. */
  250. enum { RM1=1, RM2, RM3, RM4, RM5, RM6, RM7, RM8, RM9, RM10,
  251. RM11, RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,
  252. RM21, RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
  253. RM31, RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
  254. RM41, RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
  255. RM51, RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60,
  256. RM61, RM62, RM63, RM64, RM65, RM66, RM67 };
  257. /* These versions of the macros use the stack, as normal. There are debugging
  258. versions and production versions. Note that the "rw" argument of RMATCH isn't
  259. actually used in this definition. */
  260. #ifndef NO_RECURSE
  261. #define REGISTER register
  262. #ifdef PCRE_DEBUG
  263. #define RMATCH(ra,rb,rc,rd,re,rw) \
  264. { \
  265. printf("match() called in line %d\n", __LINE__); \
  266. rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1); \
  267. printf("to line %d\n", __LINE__); \
  268. }
  269. #define RRETURN(ra) \
  270. { \
  271. printf("match() returned %d from line %d\n", ra, __LINE__); \
  272. return ra; \
  273. }
  274. #else
  275. #define RMATCH(ra,rb,rc,rd,re,rw) \
  276. rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1)
  277. #define RRETURN(ra) return ra
  278. #endif
  279. #else
  280. /* These versions of the macros manage a private stack on the heap. Note that
  281. the "rd" argument of RMATCH isn't actually used in this definition. It's the md
  282. argument of match(), which never changes. */
  283. #define REGISTER
  284. #define RMATCH(ra,rb,rc,rd,re,rw)\
  285. {\
  286. heapframe *newframe = frame->Xnextframe;\
  287. if (newframe == NULL)\
  288. {\
  289. newframe = (heapframe *)(PUBL(stack_malloc))(sizeof(heapframe));\
  290. if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\
  291. newframe->Xnextframe = NULL;\
  292. frame->Xnextframe = newframe;\
  293. }\
  294. frame->Xwhere = rw;\
  295. newframe->Xeptr = ra;\
  296. newframe->Xecode = rb;\
  297. newframe->Xmstart = mstart;\
  298. newframe->Xoffset_top = rc;\
  299. newframe->Xeptrb = re;\
  300. newframe->Xrdepth = frame->Xrdepth + 1;\
  301. newframe->Xprevframe = frame;\
  302. frame = newframe;\
  303. DPRINTF(("restarting from line %d\n", __LINE__));\
  304. goto HEAP_RECURSE;\
  305. L_##rw:\
  306. DPRINTF(("jumped back to line %d\n", __LINE__));\
  307. }
  308. #define RRETURN(ra)\
  309. {\
  310. heapframe *oldframe = frame;\
  311. frame = oldframe->Xprevframe;\
  312. if (frame != NULL)\
  313. {\
  314. rrc = ra;\
  315. goto HEAP_RETURN;\
  316. }\
  317. return ra;\
  318. }
  319. /* Structure for remembering the local variables in a private frame */
  320. typedef struct heapframe {
  321. struct heapframe *Xprevframe;
  322. struct heapframe *Xnextframe;
  323. /* Function arguments that may change */
  324. PCRE_PUCHAR Xeptr;
  325. const pcre_uchar *Xecode;
  326. PCRE_PUCHAR Xmstart;
  327. int Xoffset_top;
  328. eptrblock *Xeptrb;
  329. unsigned int Xrdepth;
  330. /* Function local variables */
  331. PCRE_PUCHAR Xcallpat;
  332. #ifdef SUPPORT_UTF
  333. PCRE_PUCHAR Xcharptr;
  334. #endif
  335. PCRE_PUCHAR Xdata;
  336. PCRE_PUCHAR Xnext;
  337. PCRE_PUCHAR Xpp;
  338. PCRE_PUCHAR Xprev;
  339. PCRE_PUCHAR Xsaved_eptr;
  340. recursion_info Xnew_recursive;
  341. BOOL Xcur_is_word;
  342. BOOL Xcondition;
  343. BOOL Xprev_is_word;
  344. #ifdef SUPPORT_UCP
  345. int Xprop_type;
  346. unsigned int Xprop_value;
  347. int Xprop_fail_result;
  348. int Xoclength;
  349. pcre_uchar Xocchars[6];
  350. #endif
  351. int Xcodelink;
  352. int Xctype;
  353. unsigned int Xfc;
  354. int Xfi;
  355. int Xlength;
  356. int Xmax;
  357. int Xmin;
  358. unsigned int Xnumber;
  359. int Xoffset;
  360. unsigned int Xop;
  361. pcre_int32 Xsave_capture_last;
  362. int Xsave_offset1, Xsave_offset2, Xsave_offset3;
  363. int Xstacksave[REC_STACK_SAVE_MAX];
  364. eptrblock Xnewptrb;
  365. /* Where to jump back to */
  366. int Xwhere;
  367. } heapframe;
  368. #endif
  369. /***************************************************************************
  370. ***************************************************************************/
  371. /*************************************************
  372. * Match from current position *
  373. *************************************************/
  374. /* This function is called recursively in many circumstances. Whenever it
  375. returns a negative (error) response, the outer incarnation must also return the
  376. same response. */
  377. /* These macros pack up tests that are used for partial matching, and which
  378. appear several times in the code. We set the "hit end" flag if the pointer is
  379. at the end of the subject and also past the start of the subject (i.e.
  380. something has been matched). For hard partial matching, we then return
  381. immediately. The second one is used when we already know we are past the end of
  382. the subject. */
  383. #define CHECK_PARTIAL()\
  384. if (md->partial != 0 && eptr >= md->end_subject && \
  385. eptr > md->start_used_ptr) \
  386. { \
  387. md->hitend = TRUE; \
  388. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
  389. }
  390. #define SCHECK_PARTIAL()\
  391. if (md->partial != 0 && eptr > md->start_used_ptr) \
  392. { \
  393. md->hitend = TRUE; \
  394. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
  395. }
  396. /* Performance note: It might be tempting to extract commonly used fields from
  397. the md structure (e.g. utf, end_subject) into individual variables to improve
  398. performance. Tests using gcc on a SPARC disproved this; in the first case, it
  399. made performance worse.
  400. Arguments:
  401. eptr pointer to current character in subject
  402. ecode pointer to current position in compiled code
  403. mstart pointer to the current match start position (can be modified
  404. by encountering \K)
  405. offset_top current top pointer
  406. md pointer to "static" info for the match
  407. eptrb pointer to chain of blocks containing eptr at start of
  408. brackets - for testing for empty matches
  409. rdepth the recursion depth
  410. Returns: MATCH_MATCH if matched ) these values are >= 0
  411. MATCH_NOMATCH if failed to match )
  412. a negative MATCH_xxx value for PRUNE, SKIP, etc
  413. a negative PCRE_ERROR_xxx value if aborted by an error condition
  414. (e.g. stopped by repeated call or recursion limit)
  415. */
  416. static int
  417. match(REGISTER PCRE_PUCHAR eptr, REGISTER const pcre_uchar *ecode,
  418. PCRE_PUCHAR mstart, int offset_top, match_data *md, eptrblock *eptrb,
  419. unsigned int rdepth)
  420. {
  421. /* These variables do not need to be preserved over recursion in this function,
  422. so they can be ordinary variables in all cases. Mark some of them with
  423. "register" because they are used a lot in loops. */
  424. register int rrc; /* Returns from recursive calls */
  425. register int i; /* Used for loops not involving calls to RMATCH() */
  426. register pcre_uint32 c; /* Character values not kept over RMATCH() calls */
  427. register BOOL utf; /* Local copy of UTF flag for speed */
  428. BOOL minimize, possessive; /* Quantifier options */
  429. BOOL caseless;
  430. int condcode;
  431. /* When recursion is not being used, all "local" variables that have to be
  432. preserved over calls to RMATCH() are part of a "frame". We set up the top-level
  433. frame on the stack here; subsequent instantiations are obtained from the heap
  434. whenever RMATCH() does a "recursion". See the macro definitions above. Putting
  435. the top-level on the stack rather than malloc-ing them all gives a performance
  436. boost in many cases where there is not much "recursion". */
  437. #ifdef NO_RECURSE
  438. heapframe *frame = (heapframe *)md->match_frames_base;
  439. /* Copy in the original argument variables */
  440. frame->Xeptr = eptr;
  441. frame->Xecode = ecode;
  442. frame->Xmstart = mstart;
  443. frame->Xoffset_top = offset_top;
  444. frame->Xeptrb = eptrb;
  445. frame->Xrdepth = rdepth;
  446. /* This is where control jumps back to to effect "recursion" */
  447. HEAP_RECURSE:
  448. /* Macros make the argument variables come from the current frame */
  449. #define eptr frame->Xeptr
  450. #define ecode frame->Xecode
  451. #define mstart frame->Xmstart
  452. #define offset_top frame->Xoffset_top
  453. #define eptrb frame->Xeptrb
  454. #define rdepth frame->Xrdepth
  455. /* Ditto for the local variables */
  456. #ifdef SUPPORT_UTF
  457. #define charptr frame->Xcharptr
  458. #endif
  459. #define callpat frame->Xcallpat
  460. #define codelink frame->Xcodelink
  461. #define data frame->Xdata
  462. #define next frame->Xnext
  463. #define pp frame->Xpp
  464. #define prev frame->Xprev
  465. #define saved_eptr frame->Xsaved_eptr
  466. #define new_recursive frame->Xnew_recursive
  467. #define cur_is_word frame->Xcur_is_word
  468. #define condition frame->Xcondition
  469. #define prev_is_word frame->Xprev_is_word
  470. #ifdef SUPPORT_UCP
  471. #define prop_type frame->Xprop_type
  472. #define prop_value frame->Xprop_value
  473. #define prop_fail_result frame->Xprop_fail_result
  474. #define oclength frame->Xoclength
  475. #define occhars frame->Xocchars
  476. #endif
  477. #define ctype frame->Xctype
  478. #define fc frame->Xfc
  479. #define fi frame->Xfi
  480. #define length frame->Xlength
  481. #define max frame->Xmax
  482. #define min frame->Xmin
  483. #define number frame->Xnumber
  484. #define offset frame->Xoffset
  485. #define op frame->Xop
  486. #define save_capture_last frame->Xsave_capture_last
  487. #define save_offset1 frame->Xsave_offset1
  488. #define save_offset2 frame->Xsave_offset2
  489. #define save_offset3 frame->Xsave_offset3
  490. #define stacksave frame->Xstacksave
  491. #define newptrb frame->Xnewptrb
  492. /* When recursion is being used, local variables are allocated on the stack and
  493. get preserved during recursion in the normal way. In this environment, fi and
  494. i, and fc and c, can be the same variables. */
  495. #else /* NO_RECURSE not defined */
  496. #define fi i
  497. #define fc c
  498. /* Many of the following variables are used only in small blocks of the code.
  499. My normal style of coding would have declared them within each of those blocks.
  500. However, in order to accommodate the version of this code that uses an external
  501. "stack" implemented on the heap, it is easier to declare them all here, so the
  502. declarations can be cut out in a block. The only declarations within blocks
  503. below are for variables that do not have to be preserved over a recursive call
  504. to RMATCH(). */
  505. #ifdef SUPPORT_UTF
  506. const pcre_uchar *charptr;
  507. #endif
  508. const pcre_uchar *callpat;
  509. const pcre_uchar *data;
  510. const pcre_uchar *next;
  511. PCRE_PUCHAR pp;
  512. const pcre_uchar *prev;
  513. PCRE_PUCHAR saved_eptr;
  514. recursion_info new_recursive;
  515. BOOL cur_is_word;
  516. BOOL condition;
  517. BOOL prev_is_word;
  518. #ifdef SUPPORT_UCP
  519. int prop_type;
  520. unsigned int prop_value;
  521. int prop_fail_result;
  522. int oclength;
  523. pcre_uchar occhars[6];
  524. #endif
  525. int codelink;
  526. int ctype;
  527. int length;
  528. int max;
  529. int min;
  530. unsigned int number;
  531. int offset;
  532. unsigned int op;
  533. pcre_int32 save_capture_last;
  534. int save_offset1, save_offset2, save_offset3;
  535. int stacksave[REC_STACK_SAVE_MAX];
  536. eptrblock newptrb;
  537. /* There is a special fudge for calling match() in a way that causes it to
  538. measure the size of its basic stack frame when the stack is being used for
  539. recursion. The second argument (ecode) being NULL triggers this behaviour. It
  540. cannot normally ever be NULL. The return is the negated value of the frame
  541. size. */
  542. if (ecode == NULL)
  543. {
  544. if (rdepth == 0)
  545. return match((PCRE_PUCHAR)&rdepth, NULL, NULL, 0, NULL, NULL, 1);
  546. else
  547. {
  548. int len = (int)((char *)&rdepth - (char *)eptr);
  549. return (len > 0)? -len : len;
  550. }
  551. }
  552. #endif /* NO_RECURSE */
  553. /* To save space on the stack and in the heap frame, I have doubled up on some
  554. of the local variables that are used only in localised parts of the code, but
  555. still need to be preserved over recursive calls of match(). These macros define
  556. the alternative names that are used. */
  557. #define allow_zero cur_is_word
  558. #define cbegroup condition
  559. #define code_offset codelink
  560. #define condassert condition
  561. #define matched_once prev_is_word
  562. #define foc number
  563. #define save_mark data
  564. /* These statements are here to stop the compiler complaining about unitialized
  565. variables. */
  566. #ifdef SUPPORT_UCP
  567. prop_value = 0;
  568. prop_fail_result = 0;
  569. #endif
  570. /* This label is used for tail recursion, which is used in a few cases even
  571. when NO_RECURSE is not defined, in order to reduce the amount of stack that is
  572. used. Thanks to Ian Taylor for noticing this possibility and sending the
  573. original patch. */
  574. TAIL_RECURSE:
  575. /* OK, now we can get on with the real code of the function. Recursive calls
  576. are specified by the macro RMATCH and RRETURN is used to return. When
  577. NO_RECURSE is *not* defined, these just turn into a recursive call to match()
  578. and a "return", respectively (possibly with some debugging if PCRE_DEBUG is
  579. defined). However, RMATCH isn't like a function call because it's quite a
  580. complicated macro. It has to be used in one particular way. This shouldn't,
  581. however, impact performance when true recursion is being used. */
  582. #ifdef SUPPORT_UTF
  583. utf = md->utf; /* Local copy of the flag */
  584. #else
  585. utf = FALSE;
  586. #endif
  587. /* First check that we haven't called match() too many times, or that we
  588. haven't exceeded the recursive call limit. */
  589. if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
  590. if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
  591. /* At the start of a group with an unlimited repeat that may match an empty
  592. string, the variable md->match_function_type is set to MATCH_CBEGROUP. It is
  593. done this way to save having to use another function argument, which would take
  594. up space on the stack. See also MATCH_CONDASSERT below.
  595. When MATCH_CBEGROUP is set, add the current subject pointer to the chain of
  596. such remembered pointers, to be checked when we hit the closing ket, in order
  597. to break infinite loops that match no characters. When match() is called in
  598. other circumstances, don't add to the chain. The MATCH_CBEGROUP feature must
  599. NOT be used with tail recursion, because the memory block that is used is on
  600. the stack, so a new one may be required for each match(). */
  601. if (md->match_function_type == MATCH_CBEGROUP)
  602. {
  603. newptrb.epb_saved_eptr = eptr;
  604. newptrb.epb_prev = eptrb;
  605. eptrb = &newptrb;
  606. md->match_function_type = 0;
  607. }
  608. /* Now start processing the opcodes. */
  609. for (;;)
  610. {
  611. minimize = possessive = FALSE;
  612. op = *ecode;
  613. switch(op)
  614. {
  615. case OP_MARK:
  616. md->nomatch_mark = ecode + 2;
  617. md->mark = NULL; /* In case previously set by assertion */
  618. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
  619. eptrb, RM55);
  620. if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
  621. md->mark == NULL) md->mark = ecode + 2;
  622. /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
  623. argument, and we must check whether that argument matches this MARK's
  624. argument. It is passed back in md->start_match_ptr (an overloading of that
  625. variable). If it does match, we reset that variable to the current subject
  626. position and return MATCH_SKIP. Otherwise, pass back the return code
  627. unaltered. */
  628. else if (rrc == MATCH_SKIP_ARG &&
  629. STRCMP_UC_UC_TEST(ecode + 2, md->start_match_ptr) == 0)
  630. {
  631. md->start_match_ptr = eptr;
  632. RRETURN(MATCH_SKIP);
  633. }
  634. RRETURN(rrc);
  635. case OP_FAIL:
  636. RRETURN(MATCH_NOMATCH);
  637. case OP_COMMIT:
  638. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
  639. eptrb, RM52);
  640. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  641. RRETURN(MATCH_COMMIT);
  642. case OP_PRUNE:
  643. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
  644. eptrb, RM51);
  645. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  646. RRETURN(MATCH_PRUNE);
  647. case OP_PRUNE_ARG:
  648. md->nomatch_mark = ecode + 2;
  649. md->mark = NULL; /* In case previously set by assertion */
  650. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
  651. eptrb, RM56);
  652. if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
  653. md->mark == NULL) md->mark = ecode + 2;
  654. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  655. RRETURN(MATCH_PRUNE);
  656. case OP_SKIP:
  657. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
  658. eptrb, RM53);
  659. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  660. md->start_match_ptr = eptr; /* Pass back current position */
  661. RRETURN(MATCH_SKIP);
  662. /* Note that, for Perl compatibility, SKIP with an argument does NOT set
  663. nomatch_mark. When a pattern match ends with a SKIP_ARG for which there was
  664. not a matching mark, we have to re-run the match, ignoring the SKIP_ARG
  665. that failed and any that precede it (either they also failed, or were not
  666. triggered). To do this, we maintain a count of executed SKIP_ARGs. If a
  667. SKIP_ARG gets to top level, the match is re-run with md->ignore_skip_arg
  668. set to the count of the one that failed. */
  669. case OP_SKIP_ARG:
  670. md->skip_arg_count++;
  671. if (md->skip_arg_count <= md->ignore_skip_arg)
  672. {
  673. ecode += PRIV(OP_lengths)[*ecode] + ecode[1];
  674. break;
  675. }
  676. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
  677. eptrb, RM57);
  678. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  679. /* Pass back the current skip name by overloading md->start_match_ptr and
  680. returning the special MATCH_SKIP_ARG return code. This will either be
  681. caught by a matching MARK, or get to the top, where it causes a rematch
  682. with md->ignore_skip_arg set to the value of md->skip_arg_count. */
  683. md->start_match_ptr = ecode + 2;
  684. RRETURN(MATCH_SKIP_ARG);
  685. /* For THEN (and THEN_ARG) we pass back the address of the opcode, so that
  686. the branch in which it occurs can be determined. Overload the start of
  687. match pointer to do this. */
  688. case OP_THEN:
  689. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
  690. eptrb, RM54);
  691. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  692. md->start_match_ptr = ecode;
  693. RRETURN(MATCH_THEN);
  694. case OP_THEN_ARG:
  695. md->nomatch_mark = ecode + 2;
  696. md->mark = NULL; /* In case previously set by assertion */
  697. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top,
  698. md, eptrb, RM58);
  699. if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
  700. md->mark == NULL) md->mark = ecode + 2;
  701. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  702. md->start_match_ptr = ecode;
  703. RRETURN(MATCH_THEN);
  704. /* Handle an atomic group that does not contain any capturing parentheses.
  705. This can be handled like an assertion. Prior to 8.13, all atomic groups
  706. were handled this way. In 8.13, the code was changed as below for ONCE, so
  707. that backups pass through the group and thereby reset captured values.
  708. However, this uses a lot more stack, so in 8.20, atomic groups that do not
  709. contain any captures generate OP_ONCE_NC, which can be handled in the old,
  710. less stack intensive way.
  711. Check the alternative branches in turn - the matching won't pass the KET
  712. for this kind of subpattern. If any one branch matches, we carry on as at
  713. the end of a normal bracket, leaving the subject pointer, but resetting
  714. the start-of-match value in case it was changed by \K. */
  715. case OP_ONCE_NC:
  716. prev = ecode;
  717. saved_eptr = eptr;
  718. save_mark = md->mark;
  719. do
  720. {
  721. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM64);
  722. if (rrc == MATCH_MATCH) /* Note: _not_ MATCH_ACCEPT */
  723. {
  724. mstart = md->start_match_ptr;
  725. break;
  726. }
  727. if (rrc == MATCH_THEN)
  728. {
  729. next = ecode + GET(ecode,1);
  730. if (md->start_match_ptr < next &&
  731. (*ecode == OP_ALT || *next == OP_ALT))
  732. rrc = MATCH_NOMATCH;
  733. }
  734. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  735. ecode += GET(ecode,1);
  736. md->mark = save_mark;
  737. }
  738. while (*ecode == OP_ALT);
  739. /* If hit the end of the group (which could be repeated), fail */
  740. if (*ecode != OP_ONCE_NC && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
  741. /* Continue as from after the group, updating the offsets high water
  742. mark, since extracts may have been taken. */
  743. do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
  744. offset_top = md->end_offset_top;
  745. eptr = md->end_match_ptr;
  746. /* For a non-repeating ket, just continue at this level. This also
  747. happens for a repeating ket if no characters were matched in the group.
  748. This is the forcible breaking of infinite loops as implemented in Perl
  749. 5.005. */
  750. if (*ecode == OP_KET || eptr == saved_eptr)
  751. {
  752. ecode += 1+LINK_SIZE;
  753. break;
  754. }
  755. /* The repeating kets try the rest of the pattern or restart from the
  756. preceding bracket, in the appropriate order. The second "call" of match()
  757. uses tail recursion, to avoid using another stack frame. */
  758. if (*ecode == OP_KETRMIN)
  759. {
  760. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM65);
  761. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  762. ecode = prev;
  763. goto TAIL_RECURSE;
  764. }
  765. else /* OP_KETRMAX */
  766. {
  767. RMATCH(eptr, prev, offset_top, md, eptrb, RM66);
  768. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  769. ecode += 1 + LINK_SIZE;
  770. goto TAIL_RECURSE;
  771. }
  772. /* Control never gets here */
  773. /* Handle a capturing bracket, other than those that are possessive with an
  774. unlimited repeat. If there is space in the offset vector, save the current
  775. subject position in the working slot at the top of the vector. We mustn't
  776. change the current values of the data slot, because they may be set from a
  777. previous iteration of this group, and be referred to by a reference inside
  778. the group. A failure to match might occur after the group has succeeded,
  779. if something later on doesn't match. For this reason, we need to restore
  780. the working value and also the values of the final offsets, in case they
  781. were set by a previous iteration of the same bracket.
  782. If there isn't enough space in the offset vector, treat this as if it were
  783. a non-capturing bracket. Don't worry about setting the flag for the error
  784. case here; that is handled in the code for KET. */
  785. case OP_CBRA:
  786. case OP_SCBRA:
  787. number = GET2(ecode, 1+LINK_SIZE);
  788. offset = number << 1;
  789. #ifdef PCRE_DEBUG
  790. printf("start bracket %d\n", number);
  791. printf("subject=");
  792. pchars(eptr, 16, TRUE, md);
  793. printf("\n");
  794. #endif
  795. if (offset < md->offset_max)
  796. {
  797. save_offset1 = md->offset_vector[offset];
  798. save_offset2 = md->offset_vector[offset+1];
  799. save_offset3 = md->offset_vector[md->offset_end - number];
  800. save_capture_last = md->capture_last;
  801. save_mark = md->mark;
  802. DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
  803. md->offset_vector[md->offset_end - number] =
  804. (int)(eptr - md->start_subject);
  805. for (;;)
  806. {
  807. if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
  808. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
  809. eptrb, RM1);
  810. if (rrc == MATCH_ONCE) break; /* Backing up through an atomic group */
  811. /* If we backed up to a THEN, check whether it is within the current
  812. branch by comparing the address of the THEN that is passed back with
  813. the end of the branch. If it is within the current branch, and the
  814. branch is one of two or more alternatives (it either starts or ends
  815. with OP_ALT), we have reached the limit of THEN's action, so convert
  816. the return code to NOMATCH, which will cause normal backtracking to
  817. happen from now on. Otherwise, THEN is passed back to an outer
  818. alternative. This implements Perl's treatment of parenthesized groups,
  819. where a group not containing | does not affect the current alternative,
  820. that is, (X) is NOT the same as (X|(*F)). */
  821. if (rrc == MATCH_THEN)
  822. {
  823. next = ecode + GET(ecode,1);
  824. if (md->start_match_ptr < next &&
  825. (*ecode == OP_ALT || *next == OP_ALT))
  826. rrc = MATCH_NOMATCH;
  827. }
  828. /* Anything other than NOMATCH is passed back. */
  829. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  830. md->capture_last = save_capture_last;
  831. ecode += GET(ecode, 1);
  832. md->mark = save_mark;
  833. if (*ecode != OP_ALT) break;
  834. }
  835. DPRINTF(("bracket %d failed\n", number));
  836. md->offset_vector[offset] = save_offset1;
  837. md->offset_vector[offset+1] = save_offset2;
  838. md->offset_vector[md->offset_end - number] = save_offset3;
  839. /* At this point, rrc will be one of MATCH_ONCE or MATCH_NOMATCH. */
  840. RRETURN(rrc);
  841. }
  842. /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
  843. as a non-capturing bracket. */
  844. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  845. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  846. DPRINTF(("insufficient capture room: treat as non-capturing\n"));
  847. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  848. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  849. /* Non-capturing or atomic group, except for possessive with unlimited
  850. repeat and ONCE group with no captures. Loop for all the alternatives.
  851. When we get to the final alternative within the brackets, we used to return
  852. the result of a recursive call to match() whatever happened so it was
  853. possible to reduce stack usage by turning this into a tail recursion,
  854. except in the case of a possibly empty group. However, now that there is
  855. the possiblity of (*THEN) occurring in the final alternative, this
  856. optimization is no longer always possible.
  857. We can optimize if we know there are no (*THEN)s in the pattern; at present
  858. this is the best that can be done.
  859. MATCH_ONCE is returned when the end of an atomic group is successfully
  860. reached, but subsequent matching fails. It passes back up the tree (causing
  861. captured values to be reset) until the original atomic group level is
  862. reached. This is tested by comparing md->once_target with the start of the
  863. group. At this point, the return is converted into MATCH_NOMATCH so that
  864. previous backup points can be taken. */
  865. case OP_ONCE:
  866. case OP_BRA:
  867. case OP_SBRA:
  868. DPRINTF(("start non-capturing bracket\n"));
  869. for (;;)
  870. {
  871. if (op >= OP_SBRA || op == OP_ONCE)
  872. md->match_function_type = MATCH_CBEGROUP;
  873. /* If this is not a possibly empty group, and there are no (*THEN)s in
  874. the pattern, and this is the final alternative, optimize as described
  875. above. */
  876. else if (!md->hasthen && ecode[GET(ecode, 1)] != OP_ALT)
  877. {
  878. ecode += PRIV(OP_lengths)[*ecode];
  879. goto TAIL_RECURSE;
  880. }
  881. /* In all other cases, we have to make another call to match(). */
  882. save_mark = md->mark;
  883. save_capture_last = md->capture_last;
  884. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, eptrb,
  885. RM2);
  886. /* See comment in the code for capturing groups above about handling
  887. THEN. */
  888. if (rrc == MATCH_THEN)
  889. {
  890. next = ecode + GET(ecode,1);
  891. if (md->start_match_ptr < next &&
  892. (*ecode == OP_ALT || *next == OP_ALT))
  893. rrc = MATCH_NOMATCH;
  894. }
  895. if (rrc != MATCH_NOMATCH)
  896. {
  897. if (rrc == MATCH_ONCE)
  898. {
  899. const pcre_uchar *scode = ecode;
  900. if (*scode != OP_ONCE) /* If not at start, find it */
  901. {
  902. while (*scode == OP_ALT) scode += GET(scode, 1);
  903. scode -= GET(scode, 1);
  904. }
  905. if (md->once_target == scode) rrc = MATCH_NOMATCH;
  906. }
  907. RRETURN(rrc);
  908. }
  909. ecode += GET(ecode, 1);
  910. md->mark = save_mark;
  911. if (*ecode != OP_ALT) break;
  912. md->capture_last = save_capture_last;
  913. }
  914. RRETURN(MATCH_NOMATCH);
  915. /* Handle possessive capturing brackets with an unlimited repeat. We come
  916. here from BRAZERO with allow_zero set TRUE. The offset_vector values are
  917. handled similarly to the normal case above. However, the matching is
  918. different. The end of these brackets will always be OP_KETRPOS, which
  919. returns MATCH_KETRPOS without going further in the pattern. By this means
  920. we can handle the group by iteration rather than recursion, thereby
  921. reducing the amount of stack needed. */
  922. case OP_CBRAPOS:
  923. case OP_SCBRAPOS:
  924. allow_zero = FALSE;
  925. POSSESSIVE_CAPTURE:
  926. number = GET2(ecode, 1+LINK_SIZE);
  927. offset = number << 1;
  928. #ifdef PCRE_DEBUG
  929. printf("start possessive bracket %d\n", number);
  930. printf("subject=");
  931. pchars(eptr, 16, TRUE, md);
  932. printf("\n");
  933. #endif
  934. if (offset >= md->offset_max) goto POSSESSIVE_NON_CAPTURE;
  935. matched_once = FALSE;
  936. code_offset = (int)(ecode - md->start_code);
  937. save_offset1 = md->offset_vector[offset];
  938. save_offset2 = md->offset_vector[offset+1];
  939. save_offset3 = md->offset_vector[md->offset_end - number];
  940. save_capture_last = md->capture_last;
  941. DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
  942. /* Each time round the loop, save the current subject position for use
  943. when the group matches. For MATCH_MATCH, the group has matched, so we
  944. restart it with a new subject starting position, remembering that we had
  945. at least one match. For MATCH_NOMATCH, carry on with the alternatives, as
  946. usual. If we haven't matched any alternatives in any iteration, check to
  947. see if a previous iteration matched. If so, the group has matched;
  948. continue from afterwards. Otherwise it has failed; restore the previous
  949. capture values before returning NOMATCH. */
  950. for (;;)
  951. {
  952. md->offset_vector[md->offset_end - number] =
  953. (int)(eptr - md->start_subject);
  954. if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
  955. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
  956. eptrb, RM63);
  957. if (rrc == MATCH_KETRPOS)
  958. {
  959. offset_top = md->end_offset_top;
  960. ecode = md->start_code + code_offset;
  961. save_capture_last = md->capture_last;
  962. matched_once = TRUE;
  963. mstart = md->start_match_ptr; /* In case \K changed it */
  964. if (eptr == md->end_match_ptr) /* Matched an empty string */
  965. {
  966. do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
  967. break;
  968. }
  969. eptr = md->end_match_ptr;
  970. continue;
  971. }
  972. /* See comment in the code for capturing groups above about handling
  973. THEN. */
  974. if (rrc == MATCH_THEN)
  975. {
  976. next = ecode + GET(ecode,1);
  977. if (md->start_match_ptr < next &&
  978. (*ecode == OP_ALT || *next == OP_ALT))
  979. rrc = MATCH_NOMATCH;
  980. }
  981. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  982. md->capture_last = save_capture_last;
  983. ecode += GET(ecode, 1);
  984. if (*ecode != OP_ALT) break;
  985. }
  986. if (!matched_once)
  987. {
  988. md->offset_vector[offset] = save_offset1;
  989. md->offset_vector[offset+1] = save_offset2;
  990. md->offset_vector[md->offset_end - number] = save_offset3;
  991. }
  992. if (allow_zero || matched_once)
  993. {
  994. ecode += 1 + LINK_SIZE;
  995. break;
  996. }
  997. RRETURN(MATCH_NOMATCH);
  998. /* Non-capturing possessive bracket with unlimited repeat. We come here
  999. from BRAZERO with allow_zero = TRUE. The code is similar to the above,
  1000. without the capturing complication. It is written out separately for speed
  1001. and cleanliness. */
  1002. case OP_BRAPOS:
  1003. case OP_SBRAPOS:
  1004. allow_zero = FALSE;
  1005. POSSESSIVE_NON_CAPTURE:
  1006. matched_once = FALSE;
  1007. code_offset = (int)(ecode - md->start_code);
  1008. save_capture_last = md->capture_last;
  1009. for (;;)
  1010. {
  1011. if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
  1012. RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
  1013. eptrb, RM48);
  1014. if (rrc == MATCH_KETRPOS)
  1015. {
  1016. offset_top = md->end_offset_top;
  1017. ecode = md->start_code + code_offset;
  1018. matched_once = TRUE;
  1019. mstart = md->start_match_ptr; /* In case \K reset it */
  1020. if (eptr == md->end_match_ptr) /* Matched an empty string */
  1021. {
  1022. do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
  1023. break;
  1024. }
  1025. eptr = md->end_match_ptr;
  1026. continue;
  1027. }
  1028. /* See comment in the code for capturing groups above about handling
  1029. THEN. */
  1030. if (rrc == MATCH_THEN)
  1031. {
  1032. next = ecode + GET(ecode,1);
  1033. if (md->start_match_ptr < next &&
  1034. (*ecode == OP_ALT || *next == OP_ALT))
  1035. rrc = MATCH_NOMATCH;
  1036. }
  1037. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1038. ecode += GET(ecode, 1);
  1039. if (*ecode != OP_ALT) break;
  1040. md->capture_last = save_capture_last;
  1041. }
  1042. if (matched_once || allow_zero)
  1043. {
  1044. ecode += 1 + LINK_SIZE;
  1045. break;
  1046. }
  1047. RRETURN(MATCH_NOMATCH);
  1048. /* Control never reaches here. */
  1049. /* Conditional group: compilation checked that there are no more than two
  1050. branches. If the condition is false, skipping the first branch takes us
  1051. past the end of the item if there is only one branch, but that's exactly
  1052. what we want. */
  1053. case OP_COND:
  1054. case OP_SCOND:
  1055. /* The variable codelink will be added to ecode when the condition is
  1056. false, to get to the second branch. Setting it to the offset to the ALT
  1057. or KET, then incrementing ecode achieves this effect. We now have ecode
  1058. pointing to the condition or callout. */
  1059. codelink = GET(ecode, 1); /* Offset to the second branch */
  1060. ecode += 1 + LINK_SIZE; /* From this opcode */
  1061. /* Because of the way auto-callout works during compile, a callout item is
  1062. inserted between OP_COND and an assertion condition. */
  1063. if (*ecode == OP_CALLOUT)
  1064. {
  1065. if (PUBL(callout) != NULL)
  1066. {
  1067. PUBL(callout_block) cb;
  1068. cb.version = 2; /* Version 1 of the callout block */
  1069. cb.callout_number = ecode[1];
  1070. cb.offset_vector = md->offset_vector;
  1071. #if defined COMPILE_PCRE8
  1072. cb.subject = (PCRE_SPTR)md->start_subject;
  1073. #elif defined COMPILE_PCRE16
  1074. cb.subject = (PCRE_SPTR16)md->start_subject;
  1075. #elif defined COMPILE_PCRE32
  1076. cb.subject = (PCRE_SPTR32)md->start_subject;
  1077. #endif
  1078. cb.subject_length = (int)(md->end_subject - md->start_subject);
  1079. cb.start_match = (int)(mstart - md->start_subject);
  1080. cb.current_position = (int)(eptr - md->start_subject);
  1081. cb.pattern_position = GET(ecode, 2);
  1082. cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
  1083. cb.capture_top = offset_top/2;
  1084. cb.capture_last = md->capture_last & CAPLMASK;
  1085. /* Internal change requires this for API compatibility. */
  1086. if (cb.capture_last == 0) cb.capture_last = -1;
  1087. cb.callout_data = md->callout_data;
  1088. cb.mark = md->nomatch_mark;
  1089. if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH);
  1090. if (rrc < 0) RRETURN(rrc);
  1091. }
  1092. /* Advance ecode past the callout, so it now points to the condition. We
  1093. must adjust codelink so that the value of ecode+codelink is unchanged. */
  1094. ecode += PRIV(OP_lengths)[OP_CALLOUT];
  1095. codelink -= PRIV(OP_lengths)[OP_CALLOUT];
  1096. }
  1097. /* Test the various possible conditions */
  1098. condition = FALSE;
  1099. switch(condcode = *ecode)
  1100. {
  1101. case OP_RREF: /* Numbered group recursion test */
  1102. if (md->recursive != NULL) /* Not recursing => FALSE */
  1103. {
  1104. unsigned int recno = GET2(ecode, 1); /* Recursion group number*/
  1105. condition = (recno == RREF_ANY || recno == md->recursive->group_num);
  1106. }
  1107. break;
  1108. case OP_DNRREF: /* Duplicate named group recursion test */
  1109. if (md->recursive != NULL)
  1110. {
  1111. int count = GET2(ecode, 1 + IMM2_SIZE);
  1112. pcre_uchar *slot = md->name_table + GET2(ecode, 1) * md->name_entry_size;
  1113. while (count-- > 0)
  1114. {
  1115. unsigned int recno = GET2(slot, 0);
  1116. condition = recno == md->recursive->group_num;
  1117. if (condition) break;
  1118. slot += md->name_entry_size;
  1119. }
  1120. }
  1121. break;
  1122. case OP_CREF: /* Numbered group used test */
  1123. offset = GET2(ecode, 1) << 1; /* Doubled ref number */
  1124. condition = offset < offset_top && md->offset_vector[offset] >= 0;
  1125. break;
  1126. case OP_DNCREF: /* Duplicate named group used test */
  1127. {
  1128. int count = GET2(ecode, 1 + IMM2_SIZE);
  1129. pcre_uchar *slot = md->name_table + GET2(ecode, 1) * md->name_entry_size;
  1130. while (count-- > 0)
  1131. {
  1132. offset = GET2(slot, 0) << 1;
  1133. condition = offset < offset_top && md->offset_vector[offset] >= 0;
  1134. if (condition) break;
  1135. slot += md->name_entry_size;
  1136. }
  1137. }
  1138. break;
  1139. case OP_DEF: /* DEFINE - always false */
  1140. case OP_FAIL: /* From optimized (?!) condition */
  1141. break;
  1142. /* The condition is an assertion. Call match() to evaluate it - setting
  1143. md->match_function_type to MATCH_CONDASSERT causes it to stop at the end
  1144. of an assertion. */
  1145. default:
  1146. md->match_function_type = MATCH_CONDASSERT;
  1147. RMATCH(eptr, ecode, offset_top, md, NULL, RM3);
  1148. if (rrc == MATCH_MATCH)
  1149. {
  1150. if (md->end_offset_top > offset_top)
  1151. offset_top = md->end_offset_top; /* Captures may have happened */
  1152. condition = TRUE;
  1153. /* Advance ecode past the assertion to the start of the first branch,
  1154. but adjust it so that the general choosing code below works. If the
  1155. assertion has a quantifier that allows zero repeats we must skip over
  1156. the BRAZERO. This is a lunatic thing to do, but somebody did! */
  1157. if (*ecode == OP_BRAZERO) ecode++;
  1158. ecode += GET(ecode, 1);
  1159. while (*ecode == OP_ALT) ecode += GET(ecode, 1);
  1160. ecode += 1 + LINK_SIZE - PRIV(OP_lengths)[condcode];
  1161. }
  1162. /* PCRE doesn't allow the effect of (*THEN) to escape beyond an
  1163. assertion; it is therefore treated as NOMATCH. Any other return is an
  1164. error. */
  1165. else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
  1166. {
  1167. RRETURN(rrc); /* Need braces because of following else */
  1168. }
  1169. break;
  1170. }
  1171. /* Choose branch according to the condition */
  1172. ecode += condition? PRIV(OP_lengths)[condcode] : codelink;
  1173. /* We are now at the branch that is to be obeyed. As there is only one, we
  1174. can use tail recursion to avoid using another stack frame, except when
  1175. there is unlimited repeat of a possibly empty group. In the latter case, a
  1176. recursive call to match() is always required, unless the second alternative
  1177. doesn't exist, in which case we can just plough on. Note that, for
  1178. compatibility with Perl, the | in a conditional group is NOT treated as
  1179. creating two alternatives. If a THEN is encountered in the branch, it
  1180. propagates out to the enclosing alternative (unless nested in a deeper set
  1181. of alternatives, of course). */
  1182. if (condition || ecode[-(1+LINK_SIZE)] == OP_ALT)
  1183. {
  1184. if (op != OP_SCOND)
  1185. {
  1186. goto TAIL_RECURSE;
  1187. }
  1188. md->match_function_type = MATCH_CBEGROUP;
  1189. RMATCH(eptr, ecode, offset_top, md, eptrb, RM49);
  1190. RRETURN(rrc);
  1191. }
  1192. /* Condition false & no alternative; continue after the group. */
  1193. else
  1194. {
  1195. }
  1196. break;
  1197. /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
  1198. to close any currently open capturing brackets. */
  1199. case OP_CLOSE:
  1200. number = GET2(ecode, 1); /* Must be less than 65536 */
  1201. offset = number << 1;
  1202. #ifdef PCRE_DEBUG
  1203. printf("end bracket %d at *ACCEPT", number);
  1204. printf("\n");
  1205. #endif
  1206. md->capture_last = (md->capture_last & OVFLMASK) | number;
  1207. if (offset >= md->offset_max) md->capture_last |= OVFLBIT; else
  1208. {
  1209. md->offset_vector[offset] =
  1210. md->offset_vector[md->offset_end - number];
  1211. md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
  1212. /* If this group is at or above the current highwater mark, ensure that
  1213. any groups between the current high water mark and this group are marked
  1214. unset and then update the high water mark. */
  1215. if (offset >= offset_top)
  1216. {
  1217. register int *iptr = md->offset_vector + offset_top;
  1218. register int *iend = md->offset_vector + offset;
  1219. while (iptr < iend) *iptr++ = -1;
  1220. offset_top = offset + 2;
  1221. }
  1222. }
  1223. ecode += 1 + IMM2_SIZE;
  1224. break;
  1225. /* End of the pattern, either real or forced. */
  1226. case OP_END:
  1227. case OP_ACCEPT:
  1228. case OP_ASSERT_ACCEPT:
  1229. /* If we have matched an empty string, fail if not in an assertion and not
  1230. in a recursion if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART
  1231. is set and we have matched at the start of the subject. In both cases,
  1232. backtracking will then try other alternatives, if any. */
  1233. if (eptr == mstart && op != OP_ASSERT_ACCEPT &&
  1234. md->recursive == NULL &&
  1235. (md->notempty ||
  1236. (md->notempty_atstart &&
  1237. mstart == md->start_subject + md->start_offset)))
  1238. RRETURN(MATCH_NOMATCH);
  1239. /* Otherwise, we have a match. */
  1240. md->end_match_ptr = eptr; /* Record where we ended */
  1241. md->end_offset_top = offset_top; /* and how many extracts were taken */
  1242. md->start_match_ptr = mstart; /* and the start (\K can modify) */
  1243. /* For some reason, the macros don't work properly if an expression is
  1244. given as the argument to RRETURN when the heap is in use. */
  1245. rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
  1246. RRETURN(rrc);
  1247. /* Assertion brackets. Check the alternative branches in turn - the
  1248. matching won't pass the KET for an assertion. If any one branch matches,
  1249. the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
  1250. start of each branch to move the current point backwards, so the code at
  1251. this level is identical to the lookahead case. When the assertion is part
  1252. of a condition, we want to return immediately afterwards. The caller of
  1253. this incarnation of the match() function will have set MATCH_CONDASSERT in
  1254. md->match_function type, and one of these opcodes will be the first opcode
  1255. that is processed. We use a local variable that is preserved over calls to
  1256. match() to remember this case. */
  1257. case OP_ASSERT:
  1258. case OP_ASSERTBACK:
  1259. save_mark = md->mark;
  1260. if (md->match_function_type == MATCH_CONDASSERT)
  1261. {
  1262. condassert = TRUE;
  1263. md->match_function_type = 0;
  1264. }
  1265. else condassert = FALSE;
  1266. /* Loop for each branch */
  1267. do
  1268. {
  1269. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
  1270. /* A match means that the assertion is true; break out of the loop
  1271. that matches its alternatives. */
  1272. if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
  1273. {
  1274. mstart = md->start_match_ptr; /* In case \K reset it */
  1275. break;
  1276. }
  1277. /* If not matched, restore the previous mark setting. */
  1278. md->mark = save_mark;
  1279. /* See comment in the code for capturing groups above about handling
  1280. THEN. */
  1281. if (rrc == MATCH_THEN)
  1282. {
  1283. next = ecode + GET(ecode,1);
  1284. if (md->start_match_ptr < next &&
  1285. (*ecode == OP_ALT || *next == OP_ALT))
  1286. rrc = MATCH_NOMATCH;
  1287. }
  1288. /* Anything other than NOMATCH causes the entire assertion to fail,
  1289. passing back the return code. This includes COMMIT, SKIP, PRUNE and an
  1290. uncaptured THEN, which means they take their normal effect. This
  1291. consistent approach does not always have exactly the same effect as in
  1292. Perl. */
  1293. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1294. ecode += GET(ecode, 1);
  1295. }
  1296. while (*ecode == OP_ALT); /* Continue for next alternative */
  1297. /* If we have tried all the alternative branches, the assertion has
  1298. failed. If not, we broke out after a match. */
  1299. if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);
  1300. /* If checking an assertion for a condition, return MATCH_MATCH. */
  1301. if (condassert) RRETURN(MATCH_MATCH);
  1302. /* Continue from after a successful assertion, updating the offsets high
  1303. water mark, since extracts may have been taken during the assertion. */
  1304. do ecode += GET(ecode,1); while (*ecode == OP_ALT);
  1305. ecode += 1 + LINK_SIZE;
  1306. offset_top = md->end_offset_top;
  1307. continue;
  1308. /* Negative assertion: all branches must fail to match for the assertion to
  1309. succeed. */
  1310. case OP_ASSERT_NOT:
  1311. case OP_ASSERTBACK_NOT:
  1312. save_mark = md->mark;
  1313. if (md->match_function_type == MATCH_CONDASSERT)
  1314. {
  1315. condassert = TRUE;
  1316. md->match_function_type = 0;
  1317. }
  1318. else condassert = FALSE;
  1319. /* Loop for each alternative branch. */
  1320. do
  1321. {
  1322. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
  1323. md->mark = save_mark; /* Always restore the mark setting */
  1324. switch(rrc)
  1325. {
  1326. case MATCH_MATCH: /* A successful match means */
  1327. case MATCH_ACCEPT: /* the assertion has failed. */
  1328. RRETURN(MATCH_NOMATCH);
  1329. case MATCH_NOMATCH: /* Carry on with next branch */
  1330. break;
  1331. /* See comment in the code for capturing groups above about handling
  1332. THEN. */
  1333. case MATCH_THEN:
  1334. next = ecode + GET(ecode,1);
  1335. if (md->start_match_ptr < next &&
  1336. (*ecode == OP_ALT || *next == OP_ALT))
  1337. {
  1338. rrc = MATCH_NOMATCH;
  1339. break;
  1340. }
  1341. /* Otherwise fall through. */
  1342. /* COMMIT, SKIP, PRUNE, and an uncaptured THEN cause the whole
  1343. assertion to fail to match, without considering any more alternatives.
  1344. Failing to match means the assertion is true. This is a consistent
  1345. approach, but does not always have the same effect as in Perl. */
  1346. case MATCH_COMMIT:
  1347. case MATCH_SKIP:
  1348. case MATCH_SKIP_ARG:
  1349. case MATCH_PRUNE:
  1350. do ecode += GET(ecode,1); while (*ecode == OP_ALT);
  1351. goto NEG_ASSERT_TRUE; /* Break out of alternation loop */
  1352. /* Anything else is an error */
  1353. default:
  1354. RRETURN(rrc);
  1355. }
  1356. /* Continue with next branch */
  1357. ecode += GET(ecode,1);
  1358. }
  1359. while (*ecode == OP_ALT);
  1360. /* All branches in the assertion failed to match. */
  1361. NEG_ASSERT_TRUE:
  1362. if (condassert) RRETURN(MATCH_MATCH); /* Condition assertion */
  1363. ecode += 1 + LINK_SIZE; /* Continue with current branch */
  1364. continue;
  1365. /* Move the subject pointer back. This occurs only at the start of
  1366. each branch of a lookbehind assertion. If we are too close to the start to
  1367. move back, this match function fails. When working with UTF-8 we move
  1368. back a number of characters, not bytes. */
  1369. case OP_REVERSE:
  1370. #ifdef SUPPORT_UTF
  1371. if (utf)
  1372. {
  1373. i = GET(ecode, 1);
  1374. while (i-- > 0)
  1375. {
  1376. eptr--;
  1377. if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
  1378. BACKCHAR(eptr);
  1379. }
  1380. }
  1381. else
  1382. #endif
  1383. /* No UTF-8 support, or not in UTF-8 mode: count is byte count */
  1384. {
  1385. eptr -= GET(ecode, 1);
  1386. if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
  1387. }
  1388. /* Save the earliest consulted character, then skip to next op code */
  1389. if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
  1390. ecode += 1 + LINK_SIZE;
  1391. break;
  1392. /* The callout item calls an external function, if one is provided, passing
  1393. details of the match so far. This is mainly for debugging, though the
  1394. function is able to force a failure. */
  1395. case OP_CALLOUT:
  1396. if (PUBL(callout) != NULL)
  1397. {
  1398. PUBL(callout_block) cb;
  1399. cb.version = 2; /* Version 1 of the callout block */
  1400. cb.callout_number = ecode[1];
  1401. cb.offset_vector = md->offset_vector;
  1402. #if defined COMPILE_PCRE8
  1403. cb.subject = (PCRE_SPTR)md->start_subject;
  1404. #elif defined COMPILE_PCRE16
  1405. cb.subject = (PCRE_SPTR16)md->start_subject;
  1406. #elif defined COMPILE_PCRE32
  1407. cb.subject = (PCRE_SPTR32)md->start_subject;
  1408. #endif
  1409. cb.subject_length = (int)(md->end_subject - md->start_subject);
  1410. cb.start_match = (int)(mstart - md->start_subject);
  1411. cb.current_position = (int)(eptr - md->start_subject);
  1412. cb.pattern_position = GET(ecode, 2);
  1413. cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
  1414. cb.capture_top = offset_top/2;
  1415. cb.capture_last = md->capture_last & CAPLMASK;
  1416. /* Internal change requires this for API compatibility. */
  1417. if (cb.capture_last == 0) cb.capture_last = -1;
  1418. cb.callout_data = md->callout_data;
  1419. cb.mark = md->nomatch_mark;
  1420. if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH);
  1421. if (rrc < 0) RRETURN(rrc);
  1422. }
  1423. ecode += 2 + 2*LINK_SIZE;
  1424. break;
  1425. /* Recursion either matches the current regex, or some subexpression. The
  1426. offset data is the offset to the starting bracket from the start of the
  1427. whole pattern. (This is so that it works from duplicated subpatterns.)
  1428. The state of the capturing groups is preserved over recursion, and
  1429. re-instated afterwards. We don't know how many are started and not yet
  1430. finished (offset_top records the completed total) so we just have to save
  1431. all the potential data. There may be up to 65535 such values, which is too
  1432. large to put on the stack, but using malloc for small numbers seems
  1433. expensive. As a compromise, the stack is used when there are no more than
  1434. REC_STACK_SAVE_MAX values to store; otherwise malloc is used.
  1435. There are also other values that have to be saved. We use a chained
  1436. sequence of blocks that actually live on the stack. Thanks to Robin Houston
  1437. for the original version of this logic. It has, however, been hacked around
  1438. a lot, so he is not to blame for the current way it works. */
  1439. case OP_RECURSE:
  1440. {
  1441. recursion_info *ri;
  1442. unsigned int recno;
  1443. callpat = md->start_code + GET(ecode, 1);
  1444. recno = (callpat == md->start_code)? 0 :
  1445. GET2(callpat, 1 + LINK_SIZE);
  1446. /* Check for repeating a recursion without advancing the subject pointer.
  1447. This should catch convoluted mutual recursions. (Some simple cases are
  1448. caught at compile time.) */
  1449. for (ri = md->recursive; ri != NULL; ri = ri->prevrec)
  1450. if (recno == ri->group_num && eptr == ri->subject_position)
  1451. RRETURN(PCRE_ERROR_RECURSELOOP);
  1452. /* Add to "recursing stack" */
  1453. new_recursive.group_num = recno;
  1454. new_recursive.saved_capture_last = md->capture_last;
  1455. new_recursive.subject_position = eptr;
  1456. new_recursive.prevrec = md->recursive;
  1457. md->recursive = &new_recursive;
  1458. /* Where to continue from afterwards */
  1459. ecode += 1 + LINK_SIZE;
  1460. /* Now save the offset data */
  1461. new_recursive.saved_max = md->offset_end;
  1462. if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)
  1463. new_recursive.offset_save = stacksave;
  1464. else
  1465. {
  1466. new_recursive.offset_save =
  1467. (int *)(PUBL(malloc))(new_recursive.saved_max * sizeof(int));
  1468. if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
  1469. }
  1470. memcpy(new_recursive.offset_save, md->offset_vector,
  1471. new_recursive.saved_max * sizeof(int));
  1472. /* OK, now we can do the recursion. After processing each alternative,
  1473. restore the offset data and the last captured value. If there were nested
  1474. recursions, md->recursive might be changed, so reset it before looping.
  1475. */
  1476. DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
  1477. cbegroup = (*callpat >= OP_SBRA);
  1478. do
  1479. {
  1480. if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
  1481. RMATCH(eptr, callpat + PRIV(OP_lengths)[*callpat], offset_top,
  1482. md, eptrb, RM6);
  1483. memcpy(md->offset_vector, new_recursive.offset_save,
  1484. new_recursive.saved_max * sizeof(int));
  1485. md->capture_last = new_recursive.saved_capture_last;
  1486. md->recursive = new_recursive.prevrec;
  1487. if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
  1488. {
  1489. DPRINTF(("Recursion matched\n"));
  1490. if (new_recursive.offset_save != stacksave)
  1491. (PUBL(free))(new_recursive.offset_save);
  1492. /* Set where we got to in the subject, and reset the start in case
  1493. it was changed by \K. This *is* propagated back out of a recursion,
  1494. for Perl compatibility. */
  1495. eptr = md->end_match_ptr;
  1496. mstart = md->start_match_ptr;
  1497. goto RECURSION_MATCHED; /* Exit loop; end processing */
  1498. }
  1499. /* PCRE does not allow THEN, SKIP, PRUNE or COMMIT to escape beyond a
  1500. recursion; they cause a NOMATCH for the entire recursion. These codes
  1501. are defined in a range that can be tested for. */
  1502. if (rrc >= MATCH_BACKTRACK_MIN && rrc <= MATCH_BACKTRACK_MAX)
  1503. {
  1504. if (new_recursive.offset_save != stacksave)
  1505. (PUBL(free))(new_recursive.offset_save);
  1506. RRETURN(MATCH_NOMATCH);
  1507. }
  1508. /* Any return code other than NOMATCH is an error. */
  1509. if (rrc != MATCH_NOMATCH)
  1510. {
  1511. DPRINTF(("Recursion gave error %d\n", rrc));
  1512. if (new_recursive.offset_save != stacksave)
  1513. (PUBL(free))(new_recursive.offset_save);
  1514. RRETURN(rrc);
  1515. }
  1516. md->recursive = &new_recursive;
  1517. callpat += GET(callpat, 1);
  1518. }
  1519. while (*callpat == OP_ALT);
  1520. DPRINTF(("Recursion didn't match\n"));
  1521. md->recursive = new_recursive.prevrec;
  1522. if (new_recursive.offset_save != stacksave)
  1523. (PUBL(free))(new_recursive.offset_save);
  1524. RRETURN(MATCH_NOMATCH);
  1525. }
  1526. RECURSION_MATCHED:
  1527. break;
  1528. /* An alternation is the end of a branch; scan along to find the end of the
  1529. bracketed group and go to there. */
  1530. case OP_ALT:
  1531. do ecode += GET(ecode,1); while (*ecode == OP_ALT);
  1532. break;
  1533. /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
  1534. indicating that it may occur zero times. It may repeat infinitely, or not
  1535. at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets
  1536. with fixed upper repeat limits are compiled as a number of copies, with the
  1537. optional ones preceded by BRAZERO or BRAMINZERO. */
  1538. case OP_BRAZERO:
  1539. next = ecode + 1;
  1540. RMATCH(eptr, next, offset_top, md, eptrb, RM10);
  1541. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1542. do next += GET(next, 1); while (*next == OP_ALT);
  1543. ecode = next + 1 + LINK_SIZE;
  1544. break;
  1545. case OP_BRAMINZERO:
  1546. next = ecode + 1;
  1547. do next += GET(next, 1); while (*next == OP_ALT);
  1548. RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
  1549. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1550. ecode++;
  1551. break;
  1552. case OP_SKIPZERO:
  1553. next = ecode+1;
  1554. do next += GET(next,1); while (*next == OP_ALT);
  1555. ecode = next + 1 + LINK_SIZE;
  1556. break;
  1557. /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
  1558. here; just jump to the group, with allow_zero set TRUE. */
  1559. case OP_BRAPOSZERO:
  1560. op = *(++ecode);
  1561. allow_zero = TRUE;
  1562. if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
  1563. goto POSSESSIVE_NON_CAPTURE;
  1564. /* End of a group, repeated or non-repeating. */
  1565. case OP_KET:
  1566. case OP_KETRMIN:
  1567. case OP_KETRMAX:
  1568. case OP_KETRPOS:
  1569. prev = ecode - GET(ecode, 1);
  1570. /* If this was a group that remembered the subject start, in order to break
  1571. infinite repeats of empty string matches, retrieve the subject start from
  1572. the chain. Otherwise, set it NULL. */
  1573. if (*prev >= OP_SBRA || *prev == OP_ONCE)
  1574. {
  1575. saved_eptr = eptrb->epb_saved_eptr; /* Value at start of group */
  1576. eptrb = eptrb->epb_prev; /* Backup to previous group */
  1577. }
  1578. else saved_eptr = NULL;
  1579. /* If we are at the end of an assertion group or a non-capturing atomic
  1580. group, stop matching and return MATCH_MATCH, but record the current high
  1581. water mark for use by positive assertions. We also need to record the match
  1582. start in case it was changed by \K. */
  1583. if ((*prev >= OP_ASSERT && *prev <= OP_ASSERTBACK_NOT) ||
  1584. *prev == OP_ONCE_NC)
  1585. {
  1586. md->end_match_ptr = eptr; /* For ONCE_NC */
  1587. md->end_offset_top = offset_top;
  1588. md->start_match_ptr = mstart;
  1589. RRETURN(MATCH_MATCH); /* Sets md->mark */
  1590. }
  1591. /* For capturing groups we have to check the group number back at the start
  1592. and if necessary complete handling an extraction by setting the offsets and
  1593. bumping the high water mark. Whole-pattern recursion is coded as a recurse
  1594. into group 0, so it won't be picked up here. Instead, we catch it when the
  1595. OP_END is reached. Other recursion is handled here. We just have to record
  1596. the current subject position and start match pointer and give a MATCH
  1597. return. */
  1598. if (*prev == OP_CBRA || *prev == OP_SCBRA ||
  1599. *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
  1600. {
  1601. number = GET2(prev, 1+LINK_SIZE);
  1602. offset = number << 1;
  1603. #ifdef PCRE_DEBUG
  1604. printf("end bracket %d", number);
  1605. printf("\n");
  1606. #endif
  1607. /* Handle a recursively called group. */
  1608. if (md->recursive != NULL && md->recursive->group_num == number)
  1609. {
  1610. md->end_match_ptr = eptr;
  1611. md->start_match_ptr = mstart;
  1612. RRETURN(MATCH_MATCH);
  1613. }
  1614. /* Deal with capturing */
  1615. md->capture_last = (md->capture_last & OVFLMASK) | number;
  1616. if (offset >= md->offset_max) md->capture_last |= OVFLBIT; else
  1617. {
  1618. /* If offset is greater than offset_top, it means that we are
  1619. "skipping" a capturing group, and that group's offsets must be marked
  1620. unset. In earlier versions of PCRE, all the offsets were unset at the
  1621. start of matching, but this doesn't work because atomic groups and
  1622. assertions can cause a value to be set that should later be unset.
  1623. Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as
  1624. part of the atomic group, but this is not on the final matching path,
  1625. so must be unset when 2 is set. (If there is no group 2, there is no
  1626. problem, because offset_top will then be 2, indicating no capture.) */
  1627. if (offset > offset_top)
  1628. {
  1629. register int *iptr = md->offset_vector + offset_top;
  1630. register int *iend = md->offset_vector + offset;
  1631. while (iptr < iend) *iptr++ = -1;
  1632. }
  1633. /* Now make the extraction */
  1634. md->offset_vector[offset] =
  1635. md->offset_vector[md->offset_end - number];
  1636. md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
  1637. if (offset_top <= offset) offset_top = offset + 2;
  1638. }
  1639. }
  1640. /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
  1641. and return the MATCH_KETRPOS. This makes it possible to do the repeats one
  1642. at a time from the outer level, thus saving stack. This must precede the
  1643. empty string test - in this case that test is done at the outer level. */
  1644. if (*ecode == OP_KETRPOS)
  1645. {
  1646. md->start_match_ptr = mstart; /* In case \K reset it */
  1647. md->end_match_ptr = eptr;
  1648. md->end_offset_top = offset_top;
  1649. RRETURN(MATCH_KETRPOS);
  1650. }
  1651. /* For an ordinary non-repeating ket, just continue at this level. This
  1652. also happens for a repeating ket if no characters were matched in the
  1653. group. This is the forcible breaking of infinite loops as implemented in
  1654. Perl 5.005. For a non-repeating atomic group that includes captures,
  1655. establish a backup point by processing the rest of the pattern at a lower
  1656. level. If this results in a NOMATCH return, pass MATCH_ONCE back to the
  1657. original OP_ONCE level, thereby bypassing intermediate backup points, but
  1658. resetting any captures that happened along the way. */
  1659. if (*ecode == OP_KET || eptr == saved_eptr)
  1660. {
  1661. if (*prev == OP_ONCE)
  1662. {
  1663. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
  1664. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1665. md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */
  1666. RRETURN(MATCH_ONCE);
  1667. }
  1668. ecode += 1 + LINK_SIZE; /* Carry on at this level */
  1669. break;
  1670. }
  1671. /* The normal repeating kets try the rest of the pattern or restart from
  1672. the preceding bracket, in the appropriate order. In the second case, we can
  1673. use tail recursion to avoid using another stack frame, unless we have an
  1674. an atomic group or an unlimited repeat of a group that can match an empty
  1675. string. */
  1676. if (*ecode == OP_KETRMIN)
  1677. {
  1678. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
  1679. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1680. if (*prev == OP_ONCE)
  1681. {
  1682. RMATCH(eptr, prev, offset_top, md, eptrb, RM8);
  1683. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1684. md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */
  1685. RRETURN(MATCH_ONCE);
  1686. }
  1687. if (*prev >= OP_SBRA) /* Could match an empty string */
  1688. {
  1689. RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
  1690. RRETURN(rrc);
  1691. }
  1692. ecode = prev;
  1693. goto TAIL_RECURSE;
  1694. }
  1695. else /* OP_KETRMAX */
  1696. {
  1697. RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
  1698. if (rrc == MATCH_ONCE && md->once_target == prev) rrc = MATCH_NOMATCH;
  1699. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1700. if (*prev == OP_ONCE)
  1701. {
  1702. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM9);
  1703. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1704. md->once_target = prev;
  1705. RRETURN(MATCH_ONCE);
  1706. }
  1707. ecode += 1 + LINK_SIZE;
  1708. goto TAIL_RECURSE;
  1709. }
  1710. /* Control never gets here */
  1711. /* Not multiline mode: start of subject assertion, unless notbol. */
  1712. case OP_CIRC:
  1713. if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
  1714. /* Start of subject assertion */
  1715. case OP_SOD:
  1716. if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);
  1717. ecode++;
  1718. break;
  1719. /* Multiline mode: start of subject unless notbol, or after any newline. */
  1720. case OP_CIRCM:
  1721. if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
  1722. if (eptr != md->start_subject &&
  1723. (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
  1724. RRETURN(MATCH_NOMATCH);
  1725. ecode++;
  1726. break;
  1727. /* Start of match assertion */
  1728. case OP_SOM:
  1729. if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);
  1730. ecode++;
  1731. break;
  1732. /* Reset the start of match point */
  1733. case OP_SET_SOM:
  1734. mstart = eptr;
  1735. ecode++;
  1736. break;
  1737. /* Multiline mode: assert before any newline, or before end of subject
  1738. unless noteol is set. */
  1739. case OP_DOLLM:
  1740. if (eptr < md->end_subject)
  1741. {
  1742. if (!IS_NEWLINE(eptr))
  1743. {
  1744. if (md->partial != 0 &&
  1745. eptr + 1 >= md->end_subject &&
  1746. NLBLOCK->nltype == NLTYPE_FIXED &&
  1747. NLBLOCK->nllen == 2 &&
  1748. UCHAR21TEST(eptr) == NLBLOCK->nl[0])
  1749. {
  1750. md->hitend = TRUE;
  1751. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  1752. }
  1753. RRETURN(MATCH_NOMATCH);
  1754. }
  1755. }
  1756. else
  1757. {
  1758. if (md->noteol) RRETURN(MATCH_NOMATCH);
  1759. SCHECK_PARTIAL();
  1760. }
  1761. ecode++;
  1762. break;
  1763. /* Not multiline mode: assert before a terminating newline or before end of
  1764. subject unless noteol is set. */
  1765. case OP_DOLL:
  1766. if (md->noteol) RRETURN(MATCH_NOMATCH);
  1767. if (!md->endonly) goto ASSERT_NL_OR_EOS;
  1768. /* ... else fall through for endonly */
  1769. /* End of subject assertion (\z) */
  1770. case OP_EOD:
  1771. if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);
  1772. SCHECK_PARTIAL();
  1773. ecode++;
  1774. break;
  1775. /* End of subject or ending \n assertion (\Z) */
  1776. case OP_EODN:
  1777. ASSERT_NL_OR_EOS:
  1778. if (eptr < md->end_subject &&
  1779. (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
  1780. {
  1781. if (md->partial != 0 &&
  1782. eptr + 1 >= md->end_subject &&
  1783. NLBLOCK->nltype == NLTYPE_FIXED &&
  1784. NLBLOCK->nllen == 2 &&
  1785. UCHAR21TEST(eptr) == NLBLOCK->nl[0])
  1786. {
  1787. md->hitend = TRUE;
  1788. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  1789. }
  1790. RRETURN(MATCH_NOMATCH);
  1791. }
  1792. /* Either at end of string or \n before end. */
  1793. SCHECK_PARTIAL();
  1794. ecode++;
  1795. break;
  1796. /* Word boundary assertions */
  1797. case OP_NOT_WORD_BOUNDARY:
  1798. case OP_WORD_BOUNDARY:
  1799. {
  1800. /* Find out if the previous and current characters are "word" characters.
  1801. It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to
  1802. be "non-word" characters. Remember the earliest consulted character for
  1803. partial matching. */
  1804. #ifdef SUPPORT_UTF
  1805. if (utf)
  1806. {
  1807. /* Get status of previous character */
  1808. if (eptr == md->start_subject) prev_is_word = FALSE; else
  1809. {
  1810. PCRE_PUCHAR lastptr = eptr - 1;
  1811. BACKCHAR(lastptr);
  1812. if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
  1813. GETCHAR(c, lastptr);
  1814. #ifdef SUPPORT_UCP
  1815. if (md->use_ucp)
  1816. {
  1817. if (c == '_') prev_is_word = TRUE; else
  1818. {
  1819. int cat = UCD_CATEGORY(c);
  1820. prev_is_word = (cat == ucp_L || cat == ucp_N);
  1821. }
  1822. }
  1823. else
  1824. #endif
  1825. prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
  1826. }
  1827. /* Get status of next character */
  1828. if (eptr >= md->end_subject)
  1829. {
  1830. SCHECK_PARTIAL();
  1831. cur_is_word = FALSE;
  1832. }
  1833. else
  1834. {
  1835. GETCHAR(c, eptr);
  1836. #ifdef SUPPORT_UCP
  1837. if (md->use_ucp)
  1838. {
  1839. if (c == '_') cur_is_word = TRUE; else
  1840. {
  1841. int cat = UCD_CATEGORY(c);
  1842. cur_is_word = (cat == ucp_L || cat == ucp_N);
  1843. }
  1844. }
  1845. else
  1846. #endif
  1847. cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
  1848. }
  1849. }
  1850. else
  1851. #endif
  1852. /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for
  1853. consistency with the behaviour of \w we do use it in this case. */
  1854. {
  1855. /* Get status of previous character */
  1856. if (eptr == md->start_subject) prev_is_word = FALSE; else
  1857. {
  1858. if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
  1859. #ifdef SUPPORT_UCP
  1860. if (md->use_ucp)
  1861. {
  1862. c = eptr[-1];
  1863. if (c == '_') prev_is_word = TRUE; else
  1864. {
  1865. int cat = UCD_CATEGORY(c);
  1866. prev_is_word = (cat == ucp_L || cat == ucp_N);
  1867. }
  1868. }
  1869. else
  1870. #endif
  1871. prev_is_word = MAX_255(eptr[-1])
  1872. && ((md->ctypes[eptr[-1]] & ctype_word) != 0);
  1873. }
  1874. /* Get status of next character */
  1875. if (eptr >= md->end_subject)
  1876. {
  1877. SCHECK_PARTIAL();
  1878. cur_is_word = FALSE;
  1879. }
  1880. else
  1881. #ifdef SUPPORT_UCP
  1882. if (md->use_ucp)
  1883. {
  1884. c = *eptr;
  1885. if (c == '_') cur_is_word = TRUE; else
  1886. {
  1887. int cat = UCD_CATEGORY(c);
  1888. cur_is_word = (cat == ucp_L || cat == ucp_N);
  1889. }
  1890. }
  1891. else
  1892. #endif
  1893. cur_is_word = MAX_255(*eptr)
  1894. && ((md->ctypes[*eptr] & ctype_word) != 0);
  1895. }
  1896. /* Now see if the situation is what we want */
  1897. if ((*ecode++ == OP_WORD_BOUNDARY)?
  1898. cur_is_word == prev_is_word : cur_is_word != prev_is_word)
  1899. RRETURN(MATCH_NOMATCH);
  1900. }
  1901. break;
  1902. /* Match any single character type except newline; have to take care with
  1903. CRLF newlines and partial matching. */
  1904. case OP_ANY:
  1905. if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
  1906. if (md->partial != 0 &&
  1907. eptr == md->end_subject - 1 &&
  1908. NLBLOCK->nltype == NLTYPE_FIXED &&
  1909. NLBLOCK->nllen == 2 &&
  1910. UCHAR21TEST(eptr) == NLBLOCK->nl[0])
  1911. {
  1912. md->hitend = TRUE;
  1913. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  1914. }
  1915. /* Fall through */
  1916. /* Match any single character whatsoever. */
  1917. case OP_ALLANY:
  1918. if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */
  1919. { /* not be updated before SCHECK_PARTIAL. */
  1920. SCHECK_PARTIAL();
  1921. RRETURN(MATCH_NOMATCH);
  1922. }
  1923. eptr++;
  1924. #ifdef SUPPORT_UTF
  1925. if (utf) ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
  1926. #endif
  1927. ecode++;
  1928. break;
  1929. /* Match a single byte, even in UTF-8 mode. This opcode really does match
  1930. any byte, even newline, independent of the setting of PCRE_DOTALL. */
  1931. case OP_ANYBYTE:
  1932. if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */
  1933. { /* not be updated before SCHECK_PARTIAL. */
  1934. SCHECK_PARTIAL();
  1935. RRETURN(MATCH_NOMATCH);
  1936. }
  1937. eptr++;
  1938. ecode++;
  1939. break;
  1940. case OP_NOT_DIGIT:
  1941. if (eptr >= md->end_subject)
  1942. {
  1943. SCHECK_PARTIAL();
  1944. RRETURN(MATCH_NOMATCH);
  1945. }
  1946. GETCHARINCTEST(c, eptr);
  1947. if (
  1948. #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
  1949. c < 256 &&
  1950. #endif
  1951. (md->ctypes[c] & ctype_digit) != 0
  1952. )
  1953. RRETURN(MATCH_NOMATCH);
  1954. ecode++;
  1955. break;
  1956. case OP_DIGIT:
  1957. if (eptr >= md->end_subject)
  1958. {
  1959. SCHECK_PARTIAL();
  1960. RRETURN(MATCH_NOMATCH);
  1961. }
  1962. GETCHARINCTEST(c, eptr);
  1963. if (
  1964. #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
  1965. c > 255 ||
  1966. #endif
  1967. (md->ctypes[c] & ctype_digit) == 0
  1968. )
  1969. RRETURN(MATCH_NOMATCH);
  1970. ecode++;
  1971. break;
  1972. case OP_NOT_WHITESPACE:
  1973. if (eptr >= md->end_subject)
  1974. {
  1975. SCHECK_PARTIAL();
  1976. RRETURN(MATCH_NOMATCH);
  1977. }
  1978. GETCHARINCTEST(c, eptr);
  1979. if (
  1980. #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
  1981. c < 256 &&
  1982. #endif
  1983. (md->ctypes[c] & ctype_space) != 0
  1984. )
  1985. RRETURN(MATCH_NOMATCH);
  1986. ecode++;
  1987. break;
  1988. case OP_WHITESPACE:
  1989. if (eptr >= md->end_subject)
  1990. {
  1991. SCHECK_PARTIAL();
  1992. RRETURN(MATCH_NOMATCH);
  1993. }
  1994. GETCHARINCTEST(c, eptr);
  1995. if (
  1996. #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
  1997. c > 255 ||
  1998. #endif
  1999. (md->ctypes[c] & ctype_space) == 0
  2000. )
  2001. RRETURN(MATCH_NOMATCH);
  2002. ecode++;
  2003. break;
  2004. case OP_NOT_WORDCHAR:
  2005. if (eptr >= md->end_subject)
  2006. {
  2007. SCHECK_PARTIAL();
  2008. RRETURN(MATCH_NOMATCH);
  2009. }
  2010. GETCHARINCTEST(c, eptr);
  2011. if (
  2012. #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
  2013. c < 256 &&
  2014. #endif
  2015. (md->ctypes[c] & ctype_word) != 0
  2016. )
  2017. RRETURN(MATCH_NOMATCH);
  2018. ecode++;
  2019. break;
  2020. case OP_WORDCHAR:
  2021. if (eptr >= md->end_subject)
  2022. {
  2023. SCHECK_PARTIAL();
  2024. RRETURN(MATCH_NOMATCH);
  2025. }
  2026. GETCHARINCTEST(c, eptr);
  2027. if (
  2028. #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
  2029. c > 255 ||
  2030. #endif
  2031. (md->ctypes[c] & ctype_word) == 0
  2032. )
  2033. RRETURN(MATCH_NOMATCH);
  2034. ecode++;
  2035. break;
  2036. case OP_ANYNL:
  2037. if (eptr >= md->end_subject)
  2038. {
  2039. SCHECK_PARTIAL();
  2040. RRETURN(MATCH_NOMATCH);
  2041. }
  2042. GETCHARINCTEST(c, eptr);
  2043. switch(c)
  2044. {
  2045. default: RRETURN(MATCH_NOMATCH);
  2046. case CHAR_CR:
  2047. if (eptr >= md->end_subject)
  2048. {
  2049. SCHECK_PARTIAL();
  2050. }
  2051. else if (UCHAR21TEST(eptr) == CHAR_LF) eptr++;
  2052. break;
  2053. case CHAR_LF:
  2054. break;
  2055. case CHAR_VT:
  2056. case CHAR_FF:
  2057. case CHAR_NEL:
  2058. #ifndef EBCDIC
  2059. case 0x2028:
  2060. case 0x2029:
  2061. #endif /* Not EBCDIC */
  2062. if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
  2063. break;
  2064. }
  2065. ecode++;
  2066. break;
  2067. case OP_NOT_HSPACE:
  2068. if (eptr >= md->end_subject)
  2069. {
  2070. SCHECK_PARTIAL();
  2071. RRETURN(MATCH_NOMATCH);
  2072. }
  2073. GETCHARINCTEST(c, eptr);
  2074. switch(c)
  2075. {
  2076. HSPACE_CASES: RRETURN(MATCH_NOMATCH); /* Byte and multibyte cases */
  2077. default: break;
  2078. }
  2079. ecode++;
  2080. break;
  2081. case OP_HSPACE:
  2082. if (eptr >= md->end_subject)
  2083. {
  2084. SCHECK_PARTIAL();
  2085. RRETURN(MATCH_NOMATCH);
  2086. }
  2087. GETCHARINCTEST(c, eptr);
  2088. switch(c)
  2089. {
  2090. HSPACE_CASES: break; /* Byte and multibyte cases */
  2091. default: RRETURN(MATCH_NOMATCH);
  2092. }
  2093. ecode++;
  2094. break;
  2095. case OP_NOT_VSPACE:
  2096. if (eptr >= md->end_subject)
  2097. {
  2098. SCHECK_PARTIAL();
  2099. RRETURN(MATCH_NOMATCH);
  2100. }
  2101. GETCHARINCTEST(c, eptr);
  2102. switch(c)
  2103. {
  2104. VSPACE_CASES: RRETURN(MATCH_NOMATCH);
  2105. default: break;
  2106. }
  2107. ecode++;
  2108. break;
  2109. case OP_VSPACE:
  2110. if (eptr >= md->end_subject)
  2111. {
  2112. SCHECK_PARTIAL();
  2113. RRETURN(MATCH_NOMATCH);
  2114. }
  2115. GETCHARINCTEST(c, eptr);
  2116. switch(c)
  2117. {
  2118. VSPACE_CASES: break;
  2119. default: RRETURN(MATCH_NOMATCH);
  2120. }
  2121. ecode++;
  2122. break;
  2123. #ifdef SUPPORT_UCP
  2124. /* Check the next character by Unicode property. We will get here only
  2125. if the support is in the binary; otherwise a compile-time error occurs. */
  2126. case OP_PROP:
  2127. case OP_NOTPROP:
  2128. if (eptr >= md->end_subject)
  2129. {
  2130. SCHECK_PARTIAL();
  2131. RRETURN(MATCH_NOMATCH);
  2132. }
  2133. GETCHARINCTEST(c, eptr);
  2134. {
  2135. const pcre_uint32 *cp;
  2136. const ucd_record *prop = GET_UCD(c);
  2137. switch(ecode[1])
  2138. {
  2139. case PT_ANY:
  2140. if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
  2141. break;
  2142. case PT_LAMP:
  2143. if ((prop->chartype == ucp_Lu ||
  2144. prop->chartype == ucp_Ll ||
  2145. prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
  2146. RRETURN(MATCH_NOMATCH);
  2147. break;
  2148. case PT_GC:
  2149. if ((ecode[2] != PRIV(ucp_gentype)[prop->chartype]) == (op == OP_PROP))
  2150. RRETURN(MATCH_NOMATCH);
  2151. break;
  2152. case PT_PC:
  2153. if ((ecode[2] != prop->chartype) == (op == OP_PROP))
  2154. RRETURN(MATCH_NOMATCH);
  2155. break;
  2156. case PT_SC:
  2157. if ((ecode[2] != prop->script) == (op == OP_PROP))
  2158. RRETURN(MATCH_NOMATCH);
  2159. break;
  2160. /* These are specials */
  2161. case PT_ALNUM:
  2162. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  2163. PRIV(ucp_gentype)[prop->chartype] == ucp_N) == (op == OP_NOTPROP))
  2164. RRETURN(MATCH_NOMATCH);
  2165. break;
  2166. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  2167. which means that Perl space and POSIX space are now identical. PCRE
  2168. was changed at release 8.34. */
  2169. case PT_SPACE: /* Perl space */
  2170. case PT_PXSPACE: /* POSIX space */
  2171. switch(c)
  2172. {
  2173. HSPACE_CASES:
  2174. VSPACE_CASES:
  2175. if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
  2176. break;
  2177. default:
  2178. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) ==
  2179. (op == OP_NOTPROP)) RRETURN(MATCH_NOMATCH);
  2180. break;
  2181. }
  2182. break;
  2183. case PT_WORD:
  2184. if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  2185. PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
  2186. c == CHAR_UNDERSCORE) == (op == OP_NOTPROP))
  2187. RRETURN(MATCH_NOMATCH);
  2188. break;
  2189. case PT_CLIST:
  2190. cp = PRIV(ucd_caseless_sets) + ecode[2];
  2191. for (;;)
  2192. {
  2193. if (c < *cp)
  2194. { if (op == OP_PROP) { RRETURN(MATCH_NOMATCH); } else break; }
  2195. if (c == *cp++)
  2196. { if (op == OP_PROP) break; else { RRETURN(MATCH_NOMATCH); } }
  2197. }
  2198. break;
  2199. case PT_UCNC:
  2200. if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  2201. c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
  2202. c >= 0xe000) == (op == OP_NOTPROP))
  2203. RRETURN(MATCH_NOMATCH);
  2204. break;
  2205. /* This should never occur */
  2206. default:
  2207. RRETURN(PCRE_ERROR_INTERNAL);
  2208. }
  2209. ecode += 3;
  2210. }
  2211. break;
  2212. /* Match an extended Unicode sequence. We will get here only if the support
  2213. is in the binary; otherwise a compile-time error occurs. */
  2214. case OP_EXTUNI:
  2215. if (eptr >= md->end_subject)
  2216. {
  2217. SCHECK_PARTIAL();
  2218. RRETURN(MATCH_NOMATCH);
  2219. }
  2220. else
  2221. {
  2222. int lgb, rgb;
  2223. GETCHARINCTEST(c, eptr);
  2224. lgb = UCD_GRAPHBREAK(c);
  2225. while (eptr < md->end_subject)
  2226. {
  2227. int len = 1;
  2228. if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
  2229. rgb = UCD_GRAPHBREAK(c);
  2230. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  2231. lgb = rgb;
  2232. eptr += len;
  2233. }
  2234. }
  2235. CHECK_PARTIAL();
  2236. ecode++;
  2237. break;
  2238. #endif /* SUPPORT_UCP */
  2239. /* Match a back reference, possibly repeatedly. Look past the end of the
  2240. item to see if there is repeat information following. The code is similar
  2241. to that for character classes, but repeated for efficiency. Then obey
  2242. similar code to character type repeats - written out again for speed.
  2243. However, if the referenced string is the empty string, always treat
  2244. it as matched, any number of times (otherwise there could be infinite
  2245. loops). If the reference is unset, there are two possibilities:
  2246. (a) In the default, Perl-compatible state, set the length negative;
  2247. this ensures that every attempt at a match fails. We can't just fail
  2248. here, because of the possibility of quantifiers with zero minima.
  2249. (b) If the JavaScript compatibility flag is set, set the length to zero
  2250. so that the back reference matches an empty string.
  2251. Otherwise, set the length to the length of what was matched by the
  2252. referenced subpattern.
  2253. The OP_REF and OP_REFI opcodes are used for a reference to a numbered group
  2254. or to a non-duplicated named group. For a duplicated named group, OP_DNREF
  2255. and OP_DNREFI are used. In this case we must scan the list of groups to
  2256. which the name refers, and use the first one that is set. */
  2257. case OP_DNREF:
  2258. case OP_DNREFI:
  2259. caseless = op == OP_DNREFI;
  2260. {
  2261. int count = GET2(ecode, 1+IMM2_SIZE);
  2262. pcre_uchar *slot = md->name_table + GET2(ecode, 1) * md->name_entry_size;
  2263. ecode += 1 + 2*IMM2_SIZE;
  2264. /* Setting the default length first and initializing 'offset' avoids
  2265. compiler warnings in the REF_REPEAT code. */
  2266. length = (md->jscript_compat)? 0 : -1;
  2267. offset = 0;
  2268. while (count-- > 0)
  2269. {
  2270. offset = GET2(slot, 0) << 1;
  2271. if (offset < offset_top && md->offset_vector[offset] >= 0)
  2272. {
  2273. length = md->offset_vector[offset+1] - md->offset_vector[offset];
  2274. break;
  2275. }
  2276. slot += md->name_entry_size;
  2277. }
  2278. }
  2279. goto REF_REPEAT;
  2280. case OP_REF:
  2281. case OP_REFI:
  2282. caseless = op == OP_REFI;
  2283. offset = GET2(ecode, 1) << 1; /* Doubled ref number */
  2284. ecode += 1 + IMM2_SIZE;
  2285. if (offset >= offset_top || md->offset_vector[offset] < 0)
  2286. length = (md->jscript_compat)? 0 : -1;
  2287. else
  2288. length = md->offset_vector[offset+1] - md->offset_vector[offset];
  2289. /* Set up for repetition, or handle the non-repeated case */
  2290. REF_REPEAT:
  2291. switch (*ecode)
  2292. {
  2293. case OP_CRSTAR:
  2294. case OP_CRMINSTAR:
  2295. case OP_CRPLUS:
  2296. case OP_CRMINPLUS:
  2297. case OP_CRQUERY:
  2298. case OP_CRMINQUERY:
  2299. c = *ecode++ - OP_CRSTAR;
  2300. minimize = (c & 1) != 0;
  2301. min = rep_min[c]; /* Pick up values from tables; */
  2302. max = rep_max[c]; /* zero for max => infinity */
  2303. if (max == 0) max = INT_MAX;
  2304. break;
  2305. case OP_CRRANGE:
  2306. case OP_CRMINRANGE:
  2307. minimize = (*ecode == OP_CRMINRANGE);
  2308. min = GET2(ecode, 1);
  2309. max = GET2(ecode, 1 + IMM2_SIZE);
  2310. if (max == 0) max = INT_MAX;
  2311. ecode += 1 + 2 * IMM2_SIZE;
  2312. break;
  2313. default: /* No repeat follows */
  2314. if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
  2315. {
  2316. if (length == -2) eptr = md->end_subject; /* Partial match */
  2317. CHECK_PARTIAL();
  2318. RRETURN(MATCH_NOMATCH);
  2319. }
  2320. eptr += length;
  2321. continue; /* With the main loop */
  2322. }
  2323. /* Handle repeated back references. If the length of the reference is
  2324. zero, just continue with the main loop. If the length is negative, it
  2325. means the reference is unset in non-Java-compatible mode. If the minimum is
  2326. zero, we can continue at the same level without recursion. For any other
  2327. minimum, carrying on will result in NOMATCH. */
  2328. if (length == 0) continue;
  2329. if (length < 0 && min == 0) continue;
  2330. /* First, ensure the minimum number of matches are present. We get back
  2331. the length of the reference string explicitly rather than passing the
  2332. address of eptr, so that eptr can be a register variable. */
  2333. for (i = 1; i <= min; i++)
  2334. {
  2335. int slength;
  2336. if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
  2337. {
  2338. if (slength == -2) eptr = md->end_subject; /* Partial match */
  2339. CHECK_PARTIAL();
  2340. RRETURN(MATCH_NOMATCH);
  2341. }
  2342. eptr += slength;
  2343. }
  2344. /* If min = max, continue at the same level without recursion.
  2345. They are not both allowed to be zero. */
  2346. if (min == max) continue;
  2347. /* If minimizing, keep trying and advancing the pointer */
  2348. if (minimize)
  2349. {
  2350. for (fi = min;; fi++)
  2351. {
  2352. int slength;
  2353. RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
  2354. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2355. if (fi >= max) RRETURN(MATCH_NOMATCH);
  2356. if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
  2357. {
  2358. if (slength == -2) eptr = md->end_subject; /* Partial match */
  2359. CHECK_PARTIAL();
  2360. RRETURN(MATCH_NOMATCH);
  2361. }
  2362. eptr += slength;
  2363. }
  2364. /* Control never gets here */
  2365. }
  2366. /* If maximizing, find the longest string and work backwards */
  2367. else
  2368. {
  2369. pp = eptr;
  2370. for (i = min; i < max; i++)
  2371. {
  2372. int slength;
  2373. if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
  2374. {
  2375. /* Can't use CHECK_PARTIAL because we don't want to update eptr in
  2376. the soft partial matching case. */
  2377. if (slength == -2 && md->partial != 0 &&
  2378. md->end_subject > md->start_used_ptr)
  2379. {
  2380. md->hitend = TRUE;
  2381. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  2382. }
  2383. break;
  2384. }
  2385. eptr += slength;
  2386. }
  2387. while (eptr >= pp)
  2388. {
  2389. RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
  2390. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2391. eptr -= length;
  2392. }
  2393. RRETURN(MATCH_NOMATCH);
  2394. }
  2395. /* Control never gets here */
  2396. /* Match a bit-mapped character class, possibly repeatedly. This op code is
  2397. used when all the characters in the class have values in the range 0-255,
  2398. and either the matching is caseful, or the characters are in the range
  2399. 0-127 when UTF-8 processing is enabled. The only difference between
  2400. OP_CLASS and OP_NCLASS occurs when a data character outside the range is
  2401. encountered.
  2402. First, look past the end of the item to see if there is repeat information
  2403. following. Then obey similar code to character type repeats - written out
  2404. again for speed. */
  2405. case OP_NCLASS:
  2406. case OP_CLASS:
  2407. {
  2408. /* The data variable is saved across frames, so the byte map needs to
  2409. be stored there. */
  2410. #define BYTE_MAP ((pcre_uint8 *)data)
  2411. data = ecode + 1; /* Save for matching */
  2412. ecode += 1 + (32 / sizeof(pcre_uchar)); /* Advance past the item */
  2413. switch (*ecode)
  2414. {
  2415. case OP_CRSTAR:
  2416. case OP_CRMINSTAR:
  2417. case OP_CRPLUS:
  2418. case OP_CRMINPLUS:
  2419. case OP_CRQUERY:
  2420. case OP_CRMINQUERY:
  2421. case OP_CRPOSSTAR:
  2422. case OP_CRPOSPLUS:
  2423. case OP_CRPOSQUERY:
  2424. c = *ecode++ - OP_CRSTAR;
  2425. if (c < OP_CRPOSSTAR - OP_CRSTAR) minimize = (c & 1) != 0;
  2426. else possessive = TRUE;
  2427. min = rep_min[c]; /* Pick up values from tables; */
  2428. max = rep_max[c]; /* zero for max => infinity */
  2429. if (max == 0) max = INT_MAX;
  2430. break;
  2431. case OP_CRRANGE:
  2432. case OP_CRMINRANGE:
  2433. case OP_CRPOSRANGE:
  2434. minimize = (*ecode == OP_CRMINRANGE);
  2435. possessive = (*ecode == OP_CRPOSRANGE);
  2436. min = GET2(ecode, 1);
  2437. max = GET2(ecode, 1 + IMM2_SIZE);
  2438. if (max == 0) max = INT_MAX;
  2439. ecode += 1 + 2 * IMM2_SIZE;
  2440. break;
  2441. default: /* No repeat follows */
  2442. min = max = 1;
  2443. break;
  2444. }
  2445. /* First, ensure the minimum number of matches are present. */
  2446. #ifdef SUPPORT_UTF
  2447. if (utf)
  2448. {
  2449. for (i = 1; i <= min; i++)
  2450. {
  2451. if (eptr >= md->end_subject)
  2452. {
  2453. SCHECK_PARTIAL();
  2454. RRETURN(MATCH_NOMATCH);
  2455. }
  2456. GETCHARINC(c, eptr);
  2457. if (c > 255)
  2458. {
  2459. if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
  2460. }
  2461. else
  2462. if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
  2463. }
  2464. }
  2465. else
  2466. #endif
  2467. /* Not UTF mode */
  2468. {
  2469. for (i = 1; i <= min; i++)
  2470. {
  2471. if (eptr >= md->end_subject)
  2472. {
  2473. SCHECK_PARTIAL();
  2474. RRETURN(MATCH_NOMATCH);
  2475. }
  2476. c = *eptr++;
  2477. #ifndef COMPILE_PCRE8
  2478. if (c > 255)
  2479. {
  2480. if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
  2481. }
  2482. else
  2483. #endif
  2484. if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
  2485. }
  2486. }
  2487. /* If max == min we can continue with the main loop without the
  2488. need to recurse. */
  2489. if (min == max) continue;
  2490. /* If minimizing, keep testing the rest of the expression and advancing
  2491. the pointer while it matches the class. */
  2492. if (minimize)
  2493. {
  2494. #ifdef SUPPORT_UTF
  2495. if (utf)
  2496. {
  2497. for (fi = min;; fi++)
  2498. {
  2499. RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
  2500. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2501. if (fi >= max) RRETURN(MATCH_NOMATCH);
  2502. if (eptr >= md->end_subject)
  2503. {
  2504. SCHECK_PARTIAL();
  2505. RRETURN(MATCH_NOMATCH);
  2506. }
  2507. GETCHARINC(c, eptr);
  2508. if (c > 255)
  2509. {
  2510. if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
  2511. }
  2512. else
  2513. if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
  2514. }
  2515. }
  2516. else
  2517. #endif
  2518. /* Not UTF mode */
  2519. {
  2520. for (fi = min;; fi++)
  2521. {
  2522. RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
  2523. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2524. if (fi >= max) RRETURN(MATCH_NOMATCH);
  2525. if (eptr >= md->end_subject)
  2526. {
  2527. SCHECK_PARTIAL();
  2528. RRETURN(MATCH_NOMATCH);
  2529. }
  2530. c = *eptr++;
  2531. #ifndef COMPILE_PCRE8
  2532. if (c > 255)
  2533. {
  2534. if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
  2535. }
  2536. else
  2537. #endif
  2538. if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
  2539. }
  2540. }
  2541. /* Control never gets here */
  2542. }
  2543. /* If maximizing, find the longest possible run, then work backwards. */
  2544. else
  2545. {
  2546. pp = eptr;
  2547. #ifdef SUPPORT_UTF
  2548. if (utf)
  2549. {
  2550. for (i = min; i < max; i++)
  2551. {
  2552. int len = 1;
  2553. if (eptr >= md->end_subject)
  2554. {
  2555. SCHECK_PARTIAL();
  2556. break;
  2557. }
  2558. GETCHARLEN(c, eptr, len);
  2559. if (c > 255)
  2560. {
  2561. if (op == OP_CLASS) break;
  2562. }
  2563. else
  2564. if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break;
  2565. eptr += len;
  2566. }
  2567. if (possessive) continue; /* No backtracking */
  2568. for (;;)
  2569. {
  2570. RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
  2571. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2572. if (eptr-- <= pp) break; /* Stop if tried at original pos */
  2573. BACKCHAR(eptr);
  2574. }
  2575. }
  2576. else
  2577. #endif
  2578. /* Not UTF mode */
  2579. {
  2580. for (i = min; i < max; i++)
  2581. {
  2582. if (eptr >= md->end_subject)
  2583. {
  2584. SCHECK_PARTIAL();
  2585. break;
  2586. }
  2587. c = *eptr;
  2588. #ifndef COMPILE_PCRE8
  2589. if (c > 255)
  2590. {
  2591. if (op == OP_CLASS) break;
  2592. }
  2593. else
  2594. #endif
  2595. if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break;
  2596. eptr++;
  2597. }
  2598. if (possessive) continue; /* No backtracking */
  2599. while (eptr >= pp)
  2600. {
  2601. RMATCH(eptr, ecode, offset_top, md, eptrb, RM19);
  2602. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2603. eptr--;
  2604. }
  2605. }
  2606. RRETURN(MATCH_NOMATCH);
  2607. }
  2608. #undef BYTE_MAP
  2609. }
  2610. /* Control never gets here */
  2611. /* Match an extended character class. In the 8-bit library, this opcode is
  2612. encountered only when UTF-8 mode mode is supported. In the 16-bit and
  2613. 32-bit libraries, codepoints greater than 255 may be encountered even when
  2614. UTF is not supported. */
  2615. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  2616. case OP_XCLASS:
  2617. {
  2618. data = ecode + 1 + LINK_SIZE; /* Save for matching */
  2619. ecode += GET(ecode, 1); /* Advance past the item */
  2620. switch (*ecode)
  2621. {
  2622. case OP_CRSTAR:
  2623. case OP_CRMINSTAR:
  2624. case OP_CRPLUS:
  2625. case OP_CRMINPLUS:
  2626. case OP_CRQUERY:
  2627. case OP_CRMINQUERY:
  2628. case OP_CRPOSSTAR:
  2629. case OP_CRPOSPLUS:
  2630. case OP_CRPOSQUERY:
  2631. c = *ecode++ - OP_CRSTAR;
  2632. if (c < OP_CRPOSSTAR - OP_CRSTAR) minimize = (c & 1) != 0;
  2633. else possessive = TRUE;
  2634. min = rep_min[c]; /* Pick up values from tables; */
  2635. max = rep_max[c]; /* zero for max => infinity */
  2636. if (max == 0) max = INT_MAX;
  2637. break;
  2638. case OP_CRRANGE:
  2639. case OP_CRMINRANGE:
  2640. case OP_CRPOSRANGE:
  2641. minimize = (*ecode == OP_CRMINRANGE);
  2642. possessive = (*ecode == OP_CRPOSRANGE);
  2643. min = GET2(ecode, 1);
  2644. max = GET2(ecode, 1 + IMM2_SIZE);
  2645. if (max == 0) max = INT_MAX;
  2646. ecode += 1 + 2 * IMM2_SIZE;
  2647. break;
  2648. default: /* No repeat follows */
  2649. min = max = 1;
  2650. break;
  2651. }
  2652. /* First, ensure the minimum number of matches are present. */
  2653. for (i = 1; i <= min; i++)
  2654. {
  2655. if (eptr >= md->end_subject)
  2656. {
  2657. SCHECK_PARTIAL();
  2658. RRETURN(MATCH_NOMATCH);
  2659. }
  2660. GETCHARINCTEST(c, eptr);
  2661. if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH);
  2662. }
  2663. /* If max == min we can continue with the main loop without the
  2664. need to recurse. */
  2665. if (min == max) continue;
  2666. /* If minimizing, keep testing the rest of the expression and advancing
  2667. the pointer while it matches the class. */
  2668. if (minimize)
  2669. {
  2670. for (fi = min;; fi++)
  2671. {
  2672. RMATCH(eptr, ecode, offset_top, md, eptrb, RM20);
  2673. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2674. if (fi >= max) RRETURN(MATCH_NOMATCH);
  2675. if (eptr >= md->end_subject)
  2676. {
  2677. SCHECK_PARTIAL();
  2678. RRETURN(MATCH_NOMATCH);
  2679. }
  2680. GETCHARINCTEST(c, eptr);
  2681. if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH);
  2682. }
  2683. /* Control never gets here */
  2684. }
  2685. /* If maximizing, find the longest possible run, then work backwards. */
  2686. else
  2687. {
  2688. pp = eptr;
  2689. for (i = min; i < max; i++)
  2690. {
  2691. int len = 1;
  2692. if (eptr >= md->end_subject)
  2693. {
  2694. SCHECK_PARTIAL();
  2695. break;
  2696. }
  2697. #ifdef SUPPORT_UTF
  2698. GETCHARLENTEST(c, eptr, len);
  2699. #else
  2700. c = *eptr;
  2701. #endif
  2702. if (!PRIV(xclass)(c, data, utf)) break;
  2703. eptr += len;
  2704. }
  2705. if (possessive) continue; /* No backtracking */
  2706. for(;;)
  2707. {
  2708. RMATCH(eptr, ecode, offset_top, md, eptrb, RM21);
  2709. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2710. if (eptr-- <= pp) break; /* Stop if tried at original pos */
  2711. #ifdef SUPPORT_UTF
  2712. if (utf) BACKCHAR(eptr);
  2713. #endif
  2714. }
  2715. RRETURN(MATCH_NOMATCH);
  2716. }
  2717. /* Control never gets here */
  2718. }
  2719. #endif /* End of XCLASS */
  2720. /* Match a single character, casefully */
  2721. case OP_CHAR:
  2722. #ifdef SUPPORT_UTF
  2723. if (utf)
  2724. {
  2725. length = 1;
  2726. ecode++;
  2727. GETCHARLEN(fc, ecode, length);
  2728. if (length > md->end_subject - eptr)
  2729. {
  2730. CHECK_PARTIAL(); /* Not SCHECK_PARTIAL() */
  2731. RRETURN(MATCH_NOMATCH);
  2732. }
  2733. while (length-- > 0) if (*ecode++ != UCHAR21INC(eptr)) RRETURN(MATCH_NOMATCH);
  2734. }
  2735. else
  2736. #endif
  2737. /* Not UTF mode */
  2738. {
  2739. if (md->end_subject - eptr < 1)
  2740. {
  2741. SCHECK_PARTIAL(); /* This one can use SCHECK_PARTIAL() */
  2742. RRETURN(MATCH_NOMATCH);
  2743. }
  2744. if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);
  2745. ecode += 2;
  2746. }
  2747. break;
  2748. /* Match a single character, caselessly. If we are at the end of the
  2749. subject, give up immediately. */
  2750. case OP_CHARI:
  2751. if (eptr >= md->end_subject)
  2752. {
  2753. SCHECK_PARTIAL();
  2754. RRETURN(MATCH_NOMATCH);
  2755. }
  2756. #ifdef SUPPORT_UTF
  2757. if (utf)
  2758. {
  2759. length = 1;
  2760. ecode++;
  2761. GETCHARLEN(fc, ecode, length);
  2762. /* If the pattern character's value is < 128, we have only one byte, and
  2763. we know that its other case must also be one byte long, so we can use the
  2764. fast lookup table. We know that there is at least one byte left in the
  2765. subject. */
  2766. if (fc < 128)
  2767. {
  2768. pcre_uint32 cc = UCHAR21(eptr);
  2769. if (md->lcc[fc] != TABLE_GET(cc, md->lcc, cc)) RRETURN(MATCH_NOMATCH);
  2770. ecode++;
  2771. eptr++;
  2772. }
  2773. /* Otherwise we must pick up the subject character. Note that we cannot
  2774. use the value of "length" to check for sufficient bytes left, because the
  2775. other case of the character may have more or fewer bytes. */
  2776. else
  2777. {
  2778. pcre_uint32 dc;
  2779. GETCHARINC(dc, eptr);
  2780. ecode += length;
  2781. /* If we have Unicode property support, we can use it to test the other
  2782. case of the character, if there is one. */
  2783. if (fc != dc)
  2784. {
  2785. #ifdef SUPPORT_UCP
  2786. if (dc != UCD_OTHERCASE(fc))
  2787. #endif
  2788. RRETURN(MATCH_NOMATCH);
  2789. }
  2790. }
  2791. }
  2792. else
  2793. #endif /* SUPPORT_UTF */
  2794. /* Not UTF mode */
  2795. {
  2796. if (TABLE_GET(ecode[1], md->lcc, ecode[1])
  2797. != TABLE_GET(*eptr, md->lcc, *eptr)) RRETURN(MATCH_NOMATCH);
  2798. eptr++;
  2799. ecode += 2;
  2800. }
  2801. break;
  2802. /* Match a single character repeatedly. */
  2803. case OP_EXACT:
  2804. case OP_EXACTI:
  2805. min = max = GET2(ecode, 1);
  2806. ecode += 1 + IMM2_SIZE;
  2807. goto REPEATCHAR;
  2808. case OP_POSUPTO:
  2809. case OP_POSUPTOI:
  2810. possessive = TRUE;
  2811. /* Fall through */
  2812. case OP_UPTO:
  2813. case OP_UPTOI:
  2814. case OP_MINUPTO:
  2815. case OP_MINUPTOI:
  2816. min = 0;
  2817. max = GET2(ecode, 1);
  2818. minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI;
  2819. ecode += 1 + IMM2_SIZE;
  2820. goto REPEATCHAR;
  2821. case OP_POSSTAR:
  2822. case OP_POSSTARI:
  2823. possessive = TRUE;
  2824. min = 0;
  2825. max = INT_MAX;
  2826. ecode++;
  2827. goto REPEATCHAR;
  2828. case OP_POSPLUS:
  2829. case OP_POSPLUSI:
  2830. possessive = TRUE;
  2831. min = 1;
  2832. max = INT_MAX;
  2833. ecode++;
  2834. goto REPEATCHAR;
  2835. case OP_POSQUERY:
  2836. case OP_POSQUERYI:
  2837. possessive = TRUE;
  2838. min = 0;
  2839. max = 1;
  2840. ecode++;
  2841. goto REPEATCHAR;
  2842. case OP_STAR:
  2843. case OP_STARI:
  2844. case OP_MINSTAR:
  2845. case OP_MINSTARI:
  2846. case OP_PLUS:
  2847. case OP_PLUSI:
  2848. case OP_MINPLUS:
  2849. case OP_MINPLUSI:
  2850. case OP_QUERY:
  2851. case OP_QUERYI:
  2852. case OP_MINQUERY:
  2853. case OP_MINQUERYI:
  2854. c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI);
  2855. minimize = (c & 1) != 0;
  2856. min = rep_min[c]; /* Pick up values from tables; */
  2857. max = rep_max[c]; /* zero for max => infinity */
  2858. if (max == 0) max = INT_MAX;
  2859. /* Common code for all repeated single-character matches. We first check
  2860. for the minimum number of characters. If the minimum equals the maximum, we
  2861. are done. Otherwise, if minimizing, check the rest of the pattern for a
  2862. match; if there isn't one, advance up to the maximum, one character at a
  2863. time.
  2864. If maximizing, advance up to the maximum number of matching characters,
  2865. until eptr is past the end of the maximum run. If possessive, we are
  2866. then done (no backing up). Otherwise, match at this position; anything
  2867. other than no match is immediately returned. For nomatch, back up one
  2868. character, unless we are matching \R and the last thing matched was
  2869. \r\n, in which case, back up two bytes. When we reach the first optional
  2870. character position, we can save stack by doing a tail recurse.
  2871. The various UTF/non-UTF and caseful/caseless cases are handled separately,
  2872. for speed. */
  2873. REPEATCHAR:
  2874. #ifdef SUPPORT_UTF
  2875. if (utf)
  2876. {
  2877. length = 1;
  2878. charptr = ecode;
  2879. GETCHARLEN(fc, ecode, length);
  2880. ecode += length;
  2881. /* Handle multibyte character matching specially here. There is
  2882. support for caseless matching if UCP support is present. */
  2883. if (length > 1)
  2884. {
  2885. #ifdef SUPPORT_UCP
  2886. pcre_uint32 othercase;
  2887. if (op >= OP_STARI && /* Caseless */
  2888. (othercase = UCD_OTHERCASE(fc)) != fc)
  2889. oclength = PRIV(ord2utf)(othercase, occhars);
  2890. else oclength = 0;
  2891. #endif /* SUPPORT_UCP */
  2892. for (i = 1; i <= min; i++)
  2893. {
  2894. if (eptr <= md->end_subject - length &&
  2895. memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length;
  2896. #ifdef SUPPORT_UCP
  2897. else if (oclength > 0 &&
  2898. eptr <= md->end_subject - oclength &&
  2899. memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength;
  2900. #endif /* SUPPORT_UCP */
  2901. else
  2902. {
  2903. CHECK_PARTIAL();
  2904. RRETURN(MATCH_NOMATCH);
  2905. }
  2906. }
  2907. if (min == max) continue;
  2908. if (minimize)
  2909. {
  2910. for (fi = min;; fi++)
  2911. {
  2912. RMATCH(eptr, ecode, offset_top, md, eptrb, RM22);
  2913. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2914. if (fi >= max) RRETURN(MATCH_NOMATCH);
  2915. if (eptr <= md->end_subject - length &&
  2916. memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length;
  2917. #ifdef SUPPORT_UCP
  2918. else if (oclength > 0 &&
  2919. eptr <= md->end_subject - oclength &&
  2920. memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength;
  2921. #endif /* SUPPORT_UCP */
  2922. else
  2923. {
  2924. CHECK_PARTIAL();
  2925. RRETURN(MATCH_NOMATCH);
  2926. }
  2927. }
  2928. /* Control never gets here */
  2929. }
  2930. else /* Maximize */
  2931. {
  2932. pp = eptr;
  2933. for (i = min; i < max; i++)
  2934. {
  2935. if (eptr <= md->end_subject - length &&
  2936. memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length;
  2937. #ifdef SUPPORT_UCP
  2938. else if (oclength > 0 &&
  2939. eptr <= md->end_subject - oclength &&
  2940. memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength;
  2941. #endif /* SUPPORT_UCP */
  2942. else
  2943. {
  2944. CHECK_PARTIAL();
  2945. break;
  2946. }
  2947. }
  2948. if (possessive) continue; /* No backtracking */
  2949. for(;;)
  2950. {
  2951. if (eptr <= pp) goto TAIL_RECURSE;
  2952. RMATCH(eptr, ecode, offset_top, md, eptrb, RM23);
  2953. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  2954. #ifdef SUPPORT_UCP
  2955. eptr--;
  2956. BACKCHAR(eptr);
  2957. #else /* without SUPPORT_UCP */
  2958. eptr -= length;
  2959. #endif /* SUPPORT_UCP */
  2960. }
  2961. }
  2962. /* Control never gets here */
  2963. }
  2964. /* If the length of a UTF-8 character is 1, we fall through here, and
  2965. obey the code as for non-UTF-8 characters below, though in this case the
  2966. value of fc will always be < 128. */
  2967. }
  2968. else
  2969. #endif /* SUPPORT_UTF */
  2970. /* When not in UTF-8 mode, load a single-byte character. */
  2971. fc = *ecode++;
  2972. /* The value of fc at this point is always one character, though we may
  2973. or may not be in UTF mode. The code is duplicated for the caseless and
  2974. caseful cases, for speed, since matching characters is likely to be quite
  2975. common. First, ensure the minimum number of matches are present. If min =
  2976. max, continue at the same level without recursing. Otherwise, if
  2977. minimizing, keep trying the rest of the expression and advancing one
  2978. matching character if failing, up to the maximum. Alternatively, if
  2979. maximizing, find the maximum number of characters and work backwards. */
  2980. DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,
  2981. max, (char *)eptr));
  2982. if (op >= OP_STARI) /* Caseless */
  2983. {
  2984. #ifdef COMPILE_PCRE8
  2985. /* fc must be < 128 if UTF is enabled. */
  2986. foc = md->fcc[fc];
  2987. #else
  2988. #ifdef SUPPORT_UTF
  2989. #ifdef SUPPORT_UCP
  2990. if (utf && fc > 127)
  2991. foc = UCD_OTHERCASE(fc);
  2992. #else
  2993. if (utf && fc > 127)
  2994. foc = fc;
  2995. #endif /* SUPPORT_UCP */
  2996. else
  2997. #endif /* SUPPORT_UTF */
  2998. foc = TABLE_GET(fc, md->fcc, fc);
  2999. #endif /* COMPILE_PCRE8 */
  3000. for (i = 1; i <= min; i++)
  3001. {
  3002. pcre_uint32 cc; /* Faster than pcre_uchar */
  3003. if (eptr >= md->end_subject)
  3004. {
  3005. SCHECK_PARTIAL();
  3006. RRETURN(MATCH_NOMATCH);
  3007. }
  3008. cc = UCHAR21TEST(eptr);
  3009. if (fc != cc && foc != cc) RRETURN(MATCH_NOMATCH);
  3010. eptr++;
  3011. }
  3012. if (min == max) continue;
  3013. if (minimize)
  3014. {
  3015. for (fi = min;; fi++)
  3016. {
  3017. pcre_uint32 cc; /* Faster than pcre_uchar */
  3018. RMATCH(eptr, ecode, offset_top, md, eptrb, RM24);
  3019. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3020. if (fi >= max) RRETURN(MATCH_NOMATCH);
  3021. if (eptr >= md->end_subject)
  3022. {
  3023. SCHECK_PARTIAL();
  3024. RRETURN(MATCH_NOMATCH);
  3025. }
  3026. cc = UCHAR21TEST(eptr);
  3027. if (fc != cc && foc != cc) RRETURN(MATCH_NOMATCH);
  3028. eptr++;
  3029. }
  3030. /* Control never gets here */
  3031. }
  3032. else /* Maximize */
  3033. {
  3034. pp = eptr;
  3035. for (i = min; i < max; i++)
  3036. {
  3037. pcre_uint32 cc; /* Faster than pcre_uchar */
  3038. if (eptr >= md->end_subject)
  3039. {
  3040. SCHECK_PARTIAL();
  3041. break;
  3042. }
  3043. cc = UCHAR21TEST(eptr);
  3044. if (fc != cc && foc != cc) break;
  3045. eptr++;
  3046. }
  3047. if (possessive) continue; /* No backtracking */
  3048. for (;;)
  3049. {
  3050. if (eptr == pp) goto TAIL_RECURSE;
  3051. RMATCH(eptr, ecode, offset_top, md, eptrb, RM25);
  3052. eptr--;
  3053. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3054. }
  3055. /* Control never gets here */
  3056. }
  3057. }
  3058. /* Caseful comparisons (includes all multi-byte characters) */
  3059. else
  3060. {
  3061. for (i = 1; i <= min; i++)
  3062. {
  3063. if (eptr >= md->end_subject)
  3064. {
  3065. SCHECK_PARTIAL();
  3066. RRETURN(MATCH_NOMATCH);
  3067. }
  3068. if (fc != UCHAR21INCTEST(eptr)) RRETURN(MATCH_NOMATCH);
  3069. }
  3070. if (min == max) continue;
  3071. if (minimize)
  3072. {
  3073. for (fi = min;; fi++)
  3074. {
  3075. RMATCH(eptr, ecode, offset_top, md, eptrb, RM26);
  3076. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3077. if (fi >= max) RRETURN(MATCH_NOMATCH);
  3078. if (eptr >= md->end_subject)
  3079. {
  3080. SCHECK_PARTIAL();
  3081. RRETURN(MATCH_NOMATCH);
  3082. }
  3083. if (fc != UCHAR21INCTEST(eptr)) RRETURN(MATCH_NOMATCH);
  3084. }
  3085. /* Control never gets here */
  3086. }
  3087. else /* Maximize */
  3088. {
  3089. pp = eptr;
  3090. for (i = min; i < max; i++)
  3091. {
  3092. if (eptr >= md->end_subject)
  3093. {
  3094. SCHECK_PARTIAL();
  3095. break;
  3096. }
  3097. if (fc != UCHAR21TEST(eptr)) break;
  3098. eptr++;
  3099. }
  3100. if (possessive) continue; /* No backtracking */
  3101. for (;;)
  3102. {
  3103. if (eptr == pp) goto TAIL_RECURSE;
  3104. RMATCH(eptr, ecode, offset_top, md, eptrb, RM27);
  3105. eptr--;
  3106. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3107. }
  3108. /* Control never gets here */
  3109. }
  3110. }
  3111. /* Control never gets here */
  3112. /* Match a negated single one-byte character. The character we are
  3113. checking can be multibyte. */
  3114. case OP_NOT:
  3115. case OP_NOTI:
  3116. if (eptr >= md->end_subject)
  3117. {
  3118. SCHECK_PARTIAL();
  3119. RRETURN(MATCH_NOMATCH);
  3120. }
  3121. #ifdef SUPPORT_UTF
  3122. if (utf)
  3123. {
  3124. register pcre_uint32 ch, och;
  3125. ecode++;
  3126. GETCHARINC(ch, ecode);
  3127. GETCHARINC(c, eptr);
  3128. if (op == OP_NOT)
  3129. {
  3130. if (ch == c) RRETURN(MATCH_NOMATCH);
  3131. }
  3132. else
  3133. {
  3134. #ifdef SUPPORT_UCP
  3135. if (ch > 127)
  3136. och = UCD_OTHERCASE(ch);
  3137. #else
  3138. if (ch > 127)
  3139. och = ch;
  3140. #endif /* SUPPORT_UCP */
  3141. else
  3142. och = TABLE_GET(ch, md->fcc, ch);
  3143. if (ch == c || och == c) RRETURN(MATCH_NOMATCH);
  3144. }
  3145. }
  3146. else
  3147. #endif
  3148. {
  3149. register pcre_uint32 ch = ecode[1];
  3150. c = *eptr++;
  3151. if (ch == c || (op == OP_NOTI && TABLE_GET(ch, md->fcc, ch) == c))
  3152. RRETURN(MATCH_NOMATCH);
  3153. ecode += 2;
  3154. }
  3155. break;
  3156. /* Match a negated single one-byte character repeatedly. This is almost a
  3157. repeat of the code for a repeated single character, but I haven't found a
  3158. nice way of commoning these up that doesn't require a test of the
  3159. positive/negative option for each character match. Maybe that wouldn't add
  3160. very much to the time taken, but character matching *is* what this is all
  3161. about... */
  3162. case OP_NOTEXACT:
  3163. case OP_NOTEXACTI:
  3164. min = max = GET2(ecode, 1);
  3165. ecode += 1 + IMM2_SIZE;
  3166. goto REPEATNOTCHAR;
  3167. case OP_NOTUPTO:
  3168. case OP_NOTUPTOI:
  3169. case OP_NOTMINUPTO:
  3170. case OP_NOTMINUPTOI:
  3171. min = 0;
  3172. max = GET2(ecode, 1);
  3173. minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI;
  3174. ecode += 1 + IMM2_SIZE;
  3175. goto REPEATNOTCHAR;
  3176. case OP_NOTPOSSTAR:
  3177. case OP_NOTPOSSTARI:
  3178. possessive = TRUE;
  3179. min = 0;
  3180. max = INT_MAX;
  3181. ecode++;
  3182. goto REPEATNOTCHAR;
  3183. case OP_NOTPOSPLUS:
  3184. case OP_NOTPOSPLUSI:
  3185. possessive = TRUE;
  3186. min = 1;
  3187. max = INT_MAX;
  3188. ecode++;
  3189. goto REPEATNOTCHAR;
  3190. case OP_NOTPOSQUERY:
  3191. case OP_NOTPOSQUERYI:
  3192. possessive = TRUE;
  3193. min = 0;
  3194. max = 1;
  3195. ecode++;
  3196. goto REPEATNOTCHAR;
  3197. case OP_NOTPOSUPTO:
  3198. case OP_NOTPOSUPTOI:
  3199. possessive = TRUE;
  3200. min = 0;
  3201. max = GET2(ecode, 1);
  3202. ecode += 1 + IMM2_SIZE;
  3203. goto REPEATNOTCHAR;
  3204. case OP_NOTSTAR:
  3205. case OP_NOTSTARI:
  3206. case OP_NOTMINSTAR:
  3207. case OP_NOTMINSTARI:
  3208. case OP_NOTPLUS:
  3209. case OP_NOTPLUSI:
  3210. case OP_NOTMINPLUS:
  3211. case OP_NOTMINPLUSI:
  3212. case OP_NOTQUERY:
  3213. case OP_NOTQUERYI:
  3214. case OP_NOTMINQUERY:
  3215. case OP_NOTMINQUERYI:
  3216. c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
  3217. minimize = (c & 1) != 0;
  3218. min = rep_min[c]; /* Pick up values from tables; */
  3219. max = rep_max[c]; /* zero for max => infinity */
  3220. if (max == 0) max = INT_MAX;
  3221. /* Common code for all repeated single-byte matches. */
  3222. REPEATNOTCHAR:
  3223. GETCHARINCTEST(fc, ecode);
  3224. /* The code is duplicated for the caseless and caseful cases, for speed,
  3225. since matching characters is likely to be quite common. First, ensure the
  3226. minimum number of matches are present. If min = max, continue at the same
  3227. level without recursing. Otherwise, if minimizing, keep trying the rest of
  3228. the expression and advancing one matching character if failing, up to the
  3229. maximum. Alternatively, if maximizing, find the maximum number of
  3230. characters and work backwards. */
  3231. DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max,
  3232. max, (char *)eptr));
  3233. if (op >= OP_NOTSTARI) /* Caseless */
  3234. {
  3235. #ifdef SUPPORT_UTF
  3236. #ifdef SUPPORT_UCP
  3237. if (utf && fc > 127)
  3238. foc = UCD_OTHERCASE(fc);
  3239. #else
  3240. if (utf && fc > 127)
  3241. foc = fc;
  3242. #endif /* SUPPORT_UCP */
  3243. else
  3244. #endif /* SUPPORT_UTF */
  3245. foc = TABLE_GET(fc, md->fcc, fc);
  3246. #ifdef SUPPORT_UTF
  3247. if (utf)
  3248. {
  3249. register pcre_uint32 d;
  3250. for (i = 1; i <= min; i++)
  3251. {
  3252. if (eptr >= md->end_subject)
  3253. {
  3254. SCHECK_PARTIAL();
  3255. RRETURN(MATCH_NOMATCH);
  3256. }
  3257. GETCHARINC(d, eptr);
  3258. if (fc == d || (unsigned int)foc == d) RRETURN(MATCH_NOMATCH);
  3259. }
  3260. }
  3261. else
  3262. #endif /* SUPPORT_UTF */
  3263. /* Not UTF mode */
  3264. {
  3265. for (i = 1; i <= min; i++)
  3266. {
  3267. if (eptr >= md->end_subject)
  3268. {
  3269. SCHECK_PARTIAL();
  3270. RRETURN(MATCH_NOMATCH);
  3271. }
  3272. if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH);
  3273. eptr++;
  3274. }
  3275. }
  3276. if (min == max) continue;
  3277. if (minimize)
  3278. {
  3279. #ifdef SUPPORT_UTF
  3280. if (utf)
  3281. {
  3282. register pcre_uint32 d;
  3283. for (fi = min;; fi++)
  3284. {
  3285. RMATCH(eptr, ecode, offset_top, md, eptrb, RM28);
  3286. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3287. if (fi >= max) RRETURN(MATCH_NOMATCH);
  3288. if (eptr >= md->end_subject)
  3289. {
  3290. SCHECK_PARTIAL();
  3291. RRETURN(MATCH_NOMATCH);
  3292. }
  3293. GETCHARINC(d, eptr);
  3294. if (fc == d || (unsigned int)foc == d) RRETURN(MATCH_NOMATCH);
  3295. }
  3296. }
  3297. else
  3298. #endif /*SUPPORT_UTF */
  3299. /* Not UTF mode */
  3300. {
  3301. for (fi = min;; fi++)
  3302. {
  3303. RMATCH(eptr, ecode, offset_top, md, eptrb, RM29);
  3304. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3305. if (fi >= max) RRETURN(MATCH_NOMATCH);
  3306. if (eptr >= md->end_subject)
  3307. {
  3308. SCHECK_PARTIAL();
  3309. RRETURN(MATCH_NOMATCH);
  3310. }
  3311. if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH);
  3312. eptr++;
  3313. }
  3314. }
  3315. /* Control never gets here */
  3316. }
  3317. /* Maximize case */
  3318. else
  3319. {
  3320. pp = eptr;
  3321. #ifdef SUPPORT_UTF
  3322. if (utf)
  3323. {
  3324. register pcre_uint32 d;
  3325. for (i = min; i < max; i++)
  3326. {
  3327. int len = 1;
  3328. if (eptr >= md->end_subject)
  3329. {
  3330. SCHECK_PARTIAL();
  3331. break;
  3332. }
  3333. GETCHARLEN(d, eptr, len);
  3334. if (fc == d || (unsigned int)foc == d) break;
  3335. eptr += len;
  3336. }
  3337. if (possessive) continue; /* No backtracking */
  3338. for(;;)
  3339. {
  3340. if (eptr <= pp) goto TAIL_RECURSE;
  3341. RMATCH(eptr, ecode, offset_top, md, eptrb, RM30);
  3342. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3343. eptr--;
  3344. BACKCHAR(eptr);
  3345. }
  3346. }
  3347. else
  3348. #endif /* SUPPORT_UTF */
  3349. /* Not UTF mode */
  3350. {
  3351. for (i = min; i < max; i++)
  3352. {
  3353. if (eptr >= md->end_subject)
  3354. {
  3355. SCHECK_PARTIAL();
  3356. break;
  3357. }
  3358. if (fc == *eptr || foc == *eptr) break;
  3359. eptr++;
  3360. }
  3361. if (possessive) continue; /* No backtracking */
  3362. for (;;)
  3363. {
  3364. if (eptr == pp) goto TAIL_RECURSE;
  3365. RMATCH(eptr, ecode, offset_top, md, eptrb, RM31);
  3366. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3367. eptr--;
  3368. }
  3369. }
  3370. /* Control never gets here */
  3371. }
  3372. }
  3373. /* Caseful comparisons */
  3374. else
  3375. {
  3376. #ifdef SUPPORT_UTF
  3377. if (utf)
  3378. {
  3379. register pcre_uint32 d;
  3380. for (i = 1; i <= min; i++)
  3381. {
  3382. if (eptr >= md->end_subject)
  3383. {
  3384. SCHECK_PARTIAL();
  3385. RRETURN(MATCH_NOMATCH);
  3386. }
  3387. GETCHARINC(d, eptr);
  3388. if (fc == d) RRETURN(MATCH_NOMATCH);
  3389. }
  3390. }
  3391. else
  3392. #endif
  3393. /* Not UTF mode */
  3394. {
  3395. for (i = 1; i <= min; i++)
  3396. {
  3397. if (eptr >= md->end_subject)
  3398. {
  3399. SCHECK_PARTIAL();
  3400. RRETURN(MATCH_NOMATCH);
  3401. }
  3402. if (fc == *eptr++) RRETURN(MATCH_NOMATCH);
  3403. }
  3404. }
  3405. if (min == max) continue;
  3406. if (minimize)
  3407. {
  3408. #ifdef SUPPORT_UTF
  3409. if (utf)
  3410. {
  3411. register pcre_uint32 d;
  3412. for (fi = min;; fi++)
  3413. {
  3414. RMATCH(eptr, ecode, offset_top, md, eptrb, RM32);
  3415. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3416. if (fi >= max) RRETURN(MATCH_NOMATCH);
  3417. if (eptr >= md->end_subject)
  3418. {
  3419. SCHECK_PARTIAL();
  3420. RRETURN(MATCH_NOMATCH);
  3421. }
  3422. GETCHARINC(d, eptr);
  3423. if (fc == d) RRETURN(MATCH_NOMATCH);
  3424. }
  3425. }
  3426. else
  3427. #endif
  3428. /* Not UTF mode */
  3429. {
  3430. for (fi = min;; fi++)
  3431. {
  3432. RMATCH(eptr, ecode, offset_top, md, eptrb, RM33);
  3433. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3434. if (fi >= max) RRETURN(MATCH_NOMATCH);
  3435. if (eptr >= md->end_subject)
  3436. {
  3437. SCHECK_PARTIAL();
  3438. RRETURN(MATCH_NOMATCH);
  3439. }
  3440. if (fc == *eptr++) RRETURN(MATCH_NOMATCH);
  3441. }
  3442. }
  3443. /* Control never gets here */
  3444. }
  3445. /* Maximize case */
  3446. else
  3447. {
  3448. pp = eptr;
  3449. #ifdef SUPPORT_UTF
  3450. if (utf)
  3451. {
  3452. register pcre_uint32 d;
  3453. for (i = min; i < max; i++)
  3454. {
  3455. int len = 1;
  3456. if (eptr >= md->end_subject)
  3457. {
  3458. SCHECK_PARTIAL();
  3459. break;
  3460. }
  3461. GETCHARLEN(d, eptr, len);
  3462. if (fc == d) break;
  3463. eptr += len;
  3464. }
  3465. if (possessive) continue; /* No backtracking */
  3466. for(;;)
  3467. {
  3468. if (eptr <= pp) goto TAIL_RECURSE;
  3469. RMATCH(eptr, ecode, offset_top, md, eptrb, RM34);
  3470. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3471. eptr--;
  3472. BACKCHAR(eptr);
  3473. }
  3474. }
  3475. else
  3476. #endif
  3477. /* Not UTF mode */
  3478. {
  3479. for (i = min; i < max; i++)
  3480. {
  3481. if (eptr >= md->end_subject)
  3482. {
  3483. SCHECK_PARTIAL();
  3484. break;
  3485. }
  3486. if (fc == *eptr) break;
  3487. eptr++;
  3488. }
  3489. if (possessive) continue; /* No backtracking */
  3490. for (;;)
  3491. {
  3492. if (eptr == pp) goto TAIL_RECURSE;
  3493. RMATCH(eptr, ecode, offset_top, md, eptrb, RM35);
  3494. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  3495. eptr--;
  3496. }
  3497. }
  3498. /* Control never gets here */
  3499. }
  3500. }
  3501. /* Control never gets here */
  3502. /* Match a single character type repeatedly; several different opcodes
  3503. share code. This is very similar to the code for single characters, but we
  3504. repeat it in the interests of efficiency. */
  3505. case OP_TYPEEXACT:
  3506. min = max = GET2(ecode, 1);
  3507. minimize = TRUE;
  3508. ecode += 1 + IMM2_SIZE;
  3509. goto REPEATTYPE;
  3510. case OP_TYPEUPTO:
  3511. case OP_TYPEMINUPTO:
  3512. min = 0;
  3513. max = GET2(ecode, 1);
  3514. minimize = *ecode == OP_TYPEMINUPTO;
  3515. ecode += 1 + IMM2_SIZE;
  3516. goto REPEATTYPE;
  3517. case OP_TYPEPOSSTAR:
  3518. possessive = TRUE;
  3519. min = 0;
  3520. max = INT_MAX;
  3521. ecode++;
  3522. goto REPEATTYPE;
  3523. case OP_TYPEPOSPLUS:
  3524. possessive = TRUE;
  3525. min = 1;
  3526. max = INT_MAX;
  3527. ecode++;
  3528. goto REPEATTYPE;
  3529. case OP_TYPEPOSQUERY:
  3530. possessive = TRUE;
  3531. min = 0;
  3532. max = 1;
  3533. ecode++;
  3534. goto REPEATTYPE;
  3535. case OP_TYPEPOSUPTO:
  3536. possessive = TRUE;
  3537. min = 0;
  3538. max = GET2(ecode, 1);
  3539. ecode += 1 + IMM2_SIZE;
  3540. goto REPEATTYPE;
  3541. case OP_TYPESTAR:
  3542. case OP_TYPEMINSTAR:
  3543. case OP_TYPEPLUS:
  3544. case OP_TYPEMINPLUS:
  3545. case OP_TYPEQUERY:
  3546. case OP_TYPEMINQUERY:
  3547. c = *ecode++ - OP_TYPESTAR;
  3548. minimize = (c & 1) != 0;
  3549. min = rep_min[c]; /* Pick up values from tables; */
  3550. max = rep_max[c]; /* zero for max => infinity */
  3551. if (max == 0) max = INT_MAX;
  3552. /* Common code for all repeated single character type matches. Note that
  3553. in UTF-8 mode, '.' matches a character of any length, but for the other
  3554. character types, the valid characters are all one-byte long. */
  3555. REPEATTYPE:
  3556. ctype = *ecode++; /* Code for the character type */
  3557. #ifdef SUPPORT_UCP
  3558. if (ctype == OP_PROP || ctype == OP_NOTPROP)
  3559. {
  3560. prop_fail_result = ctype == OP_NOTPROP;
  3561. prop_type = *ecode++;
  3562. prop_value = *ecode++;
  3563. }
  3564. else prop_type = -1;
  3565. #endif
  3566. /* First, ensure the minimum number of matches are present. Use inline
  3567. code for maximizing the speed, and do the type test once at the start
  3568. (i.e. keep it out of the loop). Separate the UTF-8 code completely as that
  3569. is tidier. Also separate the UCP code, which can be the same for both UTF-8
  3570. and single-bytes. */
  3571. if (min > 0)
  3572. {
  3573. #ifdef SUPPORT_UCP
  3574. if (prop_type >= 0)
  3575. {
  3576. switch(prop_type)
  3577. {
  3578. case PT_ANY:
  3579. if (prop_fail_result) RRETURN(MATCH_NOMATCH);
  3580. for (i = 1; i <= min; i++)
  3581. {
  3582. if (eptr >= md->end_subject)
  3583. {
  3584. SCHECK_PARTIAL();
  3585. RRETURN(MATCH_NOMATCH);
  3586. }
  3587. GETCHARINCTEST(c, eptr);
  3588. }
  3589. break;
  3590. case PT_LAMP:
  3591. for (i = 1; i <= min; i++)
  3592. {
  3593. int chartype;
  3594. if (eptr >= md->end_subject)
  3595. {
  3596. SCHECK_PARTIAL();
  3597. RRETURN(MATCH_NOMATCH);
  3598. }
  3599. GETCHARINCTEST(c, eptr);
  3600. chartype = UCD_CHARTYPE(c);
  3601. if ((chartype == ucp_Lu ||
  3602. chartype == ucp_Ll ||
  3603. chartype == ucp_Lt) == prop_fail_result)
  3604. RRETURN(MATCH_NOMATCH);
  3605. }
  3606. break;
  3607. case PT_GC:
  3608. for (i = 1; i <= min; i++)
  3609. {
  3610. if (eptr >= md->end_subject)
  3611. {
  3612. SCHECK_PARTIAL();
  3613. RRETURN(MATCH_NOMATCH);
  3614. }
  3615. GETCHARINCTEST(c, eptr);
  3616. if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result)
  3617. RRETURN(MATCH_NOMATCH);
  3618. }
  3619. break;
  3620. case PT_PC:
  3621. for (i = 1; i <= min; i++)
  3622. {
  3623. if (eptr >= md->end_subject)
  3624. {
  3625. SCHECK_PARTIAL();
  3626. RRETURN(MATCH_NOMATCH);
  3627. }
  3628. GETCHARINCTEST(c, eptr);
  3629. if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result)
  3630. RRETURN(MATCH_NOMATCH);
  3631. }
  3632. break;
  3633. case PT_SC:
  3634. for (i = 1; i <= min; i++)
  3635. {
  3636. if (eptr >= md->end_subject)
  3637. {
  3638. SCHECK_PARTIAL();
  3639. RRETURN(MATCH_NOMATCH);
  3640. }
  3641. GETCHARINCTEST(c, eptr);
  3642. if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result)
  3643. RRETURN(MATCH_NOMATCH);
  3644. }
  3645. break;
  3646. case PT_ALNUM:
  3647. for (i = 1; i <= min; i++)
  3648. {
  3649. int category;
  3650. if (eptr >= md->end_subject)
  3651. {
  3652. SCHECK_PARTIAL();
  3653. RRETURN(MATCH_NOMATCH);
  3654. }
  3655. GETCHARINCTEST(c, eptr);
  3656. category = UCD_CATEGORY(c);
  3657. if ((category == ucp_L || category == ucp_N) == prop_fail_result)
  3658. RRETURN(MATCH_NOMATCH);
  3659. }
  3660. break;
  3661. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  3662. which means that Perl space and POSIX space are now identical. PCRE
  3663. was changed at release 8.34. */
  3664. case PT_SPACE: /* Perl space */
  3665. case PT_PXSPACE: /* POSIX space */
  3666. for (i = 1; i <= min; i++)
  3667. {
  3668. if (eptr >= md->end_subject)
  3669. {
  3670. SCHECK_PARTIAL();
  3671. RRETURN(MATCH_NOMATCH);
  3672. }
  3673. GETCHARINCTEST(c, eptr);
  3674. switch(c)
  3675. {
  3676. HSPACE_CASES:
  3677. VSPACE_CASES:
  3678. if (prop_fail_result) RRETURN(MATCH_NOMATCH);
  3679. break;
  3680. default:
  3681. if ((UCD_CATEGORY(c) == ucp_Z) == prop_fail_result)
  3682. RRETURN(MATCH_NOMATCH);
  3683. break;
  3684. }
  3685. }
  3686. break;
  3687. case PT_WORD:
  3688. for (i = 1; i <= min; i++)
  3689. {
  3690. int category;
  3691. if (eptr >= md->end_subject)
  3692. {
  3693. SCHECK_PARTIAL();
  3694. RRETURN(MATCH_NOMATCH);
  3695. }
  3696. GETCHARINCTEST(c, eptr);
  3697. category = UCD_CATEGORY(c);
  3698. if ((category == ucp_L || category == ucp_N || c == CHAR_UNDERSCORE)
  3699. == prop_fail_result)
  3700. RRETURN(MATCH_NOMATCH);
  3701. }
  3702. break;
  3703. case PT_CLIST:
  3704. for (i = 1; i <= min; i++)
  3705. {
  3706. const pcre_uint32 *cp;
  3707. if (eptr >= md->end_subject)
  3708. {
  3709. SCHECK_PARTIAL();
  3710. RRETURN(MATCH_NOMATCH);
  3711. }
  3712. GETCHARINCTEST(c, eptr);
  3713. cp = PRIV(ucd_caseless_sets) + prop_value;
  3714. for (;;)
  3715. {
  3716. if (c < *cp)
  3717. { if (prop_fail_result) break; else { RRETURN(MATCH_NOMATCH); } }
  3718. if (c == *cp++)
  3719. { if (prop_fail_result) { RRETURN(MATCH_NOMATCH); } else break; }
  3720. }
  3721. }
  3722. break;
  3723. case PT_UCNC:
  3724. for (i = 1; i <= min; i++)
  3725. {
  3726. if (eptr >= md->end_subject)
  3727. {
  3728. SCHECK_PARTIAL();
  3729. RRETURN(MATCH_NOMATCH);
  3730. }
  3731. GETCHARINCTEST(c, eptr);
  3732. if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  3733. c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
  3734. c >= 0xe000) == prop_fail_result)
  3735. RRETURN(MATCH_NOMATCH);
  3736. }
  3737. break;
  3738. /* This should not occur */
  3739. default:
  3740. RRETURN(PCRE_ERROR_INTERNAL);
  3741. }
  3742. }
  3743. /* Match extended Unicode sequences. We will get here only if the
  3744. support is in the binary; otherwise a compile-time error occurs. */
  3745. else if (ctype == OP_EXTUNI)
  3746. {
  3747. for (i = 1; i <= min; i++)
  3748. {
  3749. if (eptr >= md->end_subject)
  3750. {
  3751. SCHECK_PARTIAL();
  3752. RRETURN(MATCH_NOMATCH);
  3753. }
  3754. else
  3755. {
  3756. int lgb, rgb;
  3757. GETCHARINCTEST(c, eptr);
  3758. lgb = UCD_GRAPHBREAK(c);
  3759. while (eptr < md->end_subject)
  3760. {
  3761. int len = 1;
  3762. if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
  3763. rgb = UCD_GRAPHBREAK(c);
  3764. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  3765. lgb = rgb;
  3766. eptr += len;
  3767. }
  3768. }
  3769. CHECK_PARTIAL();
  3770. }
  3771. }
  3772. else
  3773. #endif /* SUPPORT_UCP */
  3774. /* Handle all other cases when the coding is UTF-8 */
  3775. #ifdef SUPPORT_UTF
  3776. if (utf) switch(ctype)
  3777. {
  3778. case OP_ANY:
  3779. for (i = 1; i <= min; i++)
  3780. {
  3781. if (eptr >= md->end_subject)
  3782. {
  3783. SCHECK_PARTIAL();
  3784. RRETURN(MATCH_NOMATCH);
  3785. }
  3786. if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
  3787. if (md->partial != 0 &&
  3788. eptr + 1 >= md->end_subject &&
  3789. NLBLOCK->nltype == NLTYPE_FIXED &&
  3790. NLBLOCK->nllen == 2 &&
  3791. UCHAR21(eptr) == NLBLOCK->nl[0])
  3792. {
  3793. md->hitend = TRUE;
  3794. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  3795. }
  3796. eptr++;
  3797. ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
  3798. }
  3799. break;
  3800. case OP_ALLANY:
  3801. for (i = 1; i <= min; i++)
  3802. {
  3803. if (eptr >= md->end_subject)
  3804. {
  3805. SCHECK_PARTIAL();
  3806. RRETURN(MATCH_NOMATCH);
  3807. }
  3808. eptr++;
  3809. ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
  3810. }
  3811. break;
  3812. case OP_ANYBYTE:
  3813. if (eptr > md->end_subject - min) RRETURN(MATCH_NOMATCH);
  3814. eptr += min;
  3815. break;
  3816. case OP_ANYNL:
  3817. for (i = 1; i <= min; i++)
  3818. {
  3819. if (eptr >= md->end_subject)
  3820. {
  3821. SCHECK_PARTIAL();
  3822. RRETURN(MATCH_NOMATCH);
  3823. }
  3824. GETCHARINC(c, eptr);
  3825. switch(c)
  3826. {
  3827. default: RRETURN(MATCH_NOMATCH);
  3828. case CHAR_CR:
  3829. if (eptr < md->end_subject && UCHAR21(eptr) == CHAR_LF) eptr++;
  3830. break;
  3831. case CHAR_LF:
  3832. break;
  3833. case CHAR_VT:
  3834. case CHAR_FF:
  3835. case CHAR_NEL:
  3836. #ifndef EBCDIC
  3837. case 0x2028:
  3838. case 0x2029:
  3839. #endif /* Not EBCDIC */
  3840. if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
  3841. break;
  3842. }
  3843. }
  3844. break;
  3845. case OP_NOT_HSPACE:
  3846. for (i = 1; i <= min; i++)
  3847. {
  3848. if (eptr >= md->end_subject)
  3849. {
  3850. SCHECK_PARTIAL();
  3851. RRETURN(MATCH_NOMATCH);
  3852. }
  3853. GETCHARINC(c, eptr);
  3854. switch(c)
  3855. {
  3856. HSPACE_CASES: RRETURN(MATCH_NOMATCH); /* Byte and multibyte cases */
  3857. default: break;
  3858. }
  3859. }
  3860. break;
  3861. case OP_HSPACE:
  3862. for (i = 1; i <= min; i++)
  3863. {
  3864. if (eptr >= md->end_subject)
  3865. {
  3866. SCHECK_PARTIAL();
  3867. RRETURN(MATCH_NOMATCH);
  3868. }
  3869. GETCHARINC(c, eptr);
  3870. switch(c)
  3871. {
  3872. HSPACE_CASES: break; /* Byte and multibyte cases */
  3873. default: RRETURN(MATCH_NOMATCH);
  3874. }
  3875. }
  3876. break;
  3877. case OP_NOT_VSPACE:
  3878. for (i = 1; i <= min; i++)
  3879. {
  3880. if (eptr >= md->end_subject)
  3881. {
  3882. SCHECK_PARTIAL();
  3883. RRETURN(MATCH_NOMATCH);
  3884. }
  3885. GETCHARINC(c, eptr);
  3886. switch(c)
  3887. {
  3888. VSPACE_CASES: RRETURN(MATCH_NOMATCH);
  3889. default: break;
  3890. }
  3891. }
  3892. break;
  3893. case OP_VSPACE:
  3894. for (i = 1; i <= min; i++)
  3895. {
  3896. if (eptr >= md->end_subject)
  3897. {
  3898. SCHECK_PARTIAL();
  3899. RRETURN(MATCH_NOMATCH);
  3900. }
  3901. GETCHARINC(c, eptr);
  3902. switch(c)
  3903. {
  3904. VSPACE_CASES: break;
  3905. default: RRETURN(MATCH_NOMATCH);
  3906. }
  3907. }
  3908. break;
  3909. case OP_NOT_DIGIT:
  3910. for (i = 1; i <= min; i++)
  3911. {
  3912. if (eptr >= md->end_subject)
  3913. {
  3914. SCHECK_PARTIAL();
  3915. RRETURN(MATCH_NOMATCH);
  3916. }
  3917. GETCHARINC(c, eptr);
  3918. if (c < 128 && (md->ctypes[c] & ctype_digit) != 0)
  3919. RRETURN(MATCH_NOMATCH);
  3920. }
  3921. break;
  3922. case OP_DIGIT:
  3923. for (i = 1; i <= min; i++)
  3924. {
  3925. pcre_uint32 cc;
  3926. if (eptr >= md->end_subject)
  3927. {
  3928. SCHECK_PARTIAL();
  3929. RRETURN(MATCH_NOMATCH);
  3930. }
  3931. cc = UCHAR21(eptr);
  3932. if (cc >= 128 || (md->ctypes[cc] & ctype_digit) == 0)
  3933. RRETURN(MATCH_NOMATCH);
  3934. eptr++;
  3935. /* No need to skip more bytes - we know it's a 1-byte character */
  3936. }
  3937. break;
  3938. case OP_NOT_WHITESPACE:
  3939. for (i = 1; i <= min; i++)
  3940. {
  3941. pcre_uint32 cc;
  3942. if (eptr >= md->end_subject)
  3943. {
  3944. SCHECK_PARTIAL();
  3945. RRETURN(MATCH_NOMATCH);
  3946. }
  3947. cc = UCHAR21(eptr);
  3948. if (cc < 128 && (md->ctypes[cc] & ctype_space) != 0)
  3949. RRETURN(MATCH_NOMATCH);
  3950. eptr++;
  3951. ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
  3952. }
  3953. break;
  3954. case OP_WHITESPACE:
  3955. for (i = 1; i <= min; i++)
  3956. {
  3957. pcre_uint32 cc;
  3958. if (eptr >= md->end_subject)
  3959. {
  3960. SCHECK_PARTIAL();
  3961. RRETURN(MATCH_NOMATCH);
  3962. }
  3963. cc = UCHAR21(eptr);
  3964. if (cc >= 128 || (md->ctypes[cc] & ctype_space) == 0)
  3965. RRETURN(MATCH_NOMATCH);
  3966. eptr++;
  3967. /* No need to skip more bytes - we know it's a 1-byte character */
  3968. }
  3969. break;
  3970. case OP_NOT_WORDCHAR:
  3971. for (i = 1; i <= min; i++)
  3972. {
  3973. pcre_uint32 cc;
  3974. if (eptr >= md->end_subject)
  3975. {
  3976. SCHECK_PARTIAL();
  3977. RRETURN(MATCH_NOMATCH);
  3978. }
  3979. cc = UCHAR21(eptr);
  3980. if (cc < 128 && (md->ctypes[cc] & ctype_word) != 0)
  3981. RRETURN(MATCH_NOMATCH);
  3982. eptr++;
  3983. ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
  3984. }
  3985. break;
  3986. case OP_WORDCHAR:
  3987. for (i = 1; i <= min; i++)
  3988. {
  3989. pcre_uint32 cc;
  3990. if (eptr >= md->end_subject)
  3991. {
  3992. SCHECK_PARTIAL();
  3993. RRETURN(MATCH_NOMATCH);
  3994. }
  3995. cc = UCHAR21(eptr);
  3996. if (cc >= 128 || (md->ctypes[cc] & ctype_word) == 0)
  3997. RRETURN(MATCH_NOMATCH);
  3998. eptr++;
  3999. /* No need to skip more bytes - we know it's a 1-byte character */
  4000. }
  4001. break;
  4002. default:
  4003. RRETURN(PCRE_ERROR_INTERNAL);
  4004. } /* End switch(ctype) */
  4005. else
  4006. #endif /* SUPPORT_UTF */
  4007. /* Code for the non-UTF-8 case for minimum matching of operators other
  4008. than OP_PROP and OP_NOTPROP. */
  4009. switch(ctype)
  4010. {
  4011. case OP_ANY:
  4012. for (i = 1; i <= min; i++)
  4013. {
  4014. if (eptr >= md->end_subject)
  4015. {
  4016. SCHECK_PARTIAL();
  4017. RRETURN(MATCH_NOMATCH);
  4018. }
  4019. if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
  4020. if (md->partial != 0 &&
  4021. eptr + 1 >= md->end_subject &&
  4022. NLBLOCK->nltype == NLTYPE_FIXED &&
  4023. NLBLOCK->nllen == 2 &&
  4024. *eptr == NLBLOCK->nl[0])
  4025. {
  4026. md->hitend = TRUE;
  4027. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  4028. }
  4029. eptr++;
  4030. }
  4031. break;
  4032. case OP_ALLANY:
  4033. if (eptr > md->end_subject - min)
  4034. {
  4035. SCHECK_PARTIAL();
  4036. RRETURN(MATCH_NOMATCH);
  4037. }
  4038. eptr += min;
  4039. break;
  4040. case OP_ANYBYTE:
  4041. if (eptr > md->end_subject - min)
  4042. {
  4043. SCHECK_PARTIAL();
  4044. RRETURN(MATCH_NOMATCH);
  4045. }
  4046. eptr += min;
  4047. break;
  4048. case OP_ANYNL:
  4049. for (i = 1; i <= min; i++)
  4050. {
  4051. if (eptr >= md->end_subject)
  4052. {
  4053. SCHECK_PARTIAL();
  4054. RRETURN(MATCH_NOMATCH);
  4055. }
  4056. switch(*eptr++)
  4057. {
  4058. default: RRETURN(MATCH_NOMATCH);
  4059. case CHAR_CR:
  4060. if (eptr < md->end_subject && *eptr == CHAR_LF) eptr++;
  4061. break;
  4062. case CHAR_LF:
  4063. break;
  4064. case CHAR_VT:
  4065. case CHAR_FF:
  4066. case CHAR_NEL:
  4067. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4068. case 0x2028:
  4069. case 0x2029:
  4070. #endif
  4071. if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
  4072. break;
  4073. }
  4074. }
  4075. break;
  4076. case OP_NOT_HSPACE:
  4077. for (i = 1; i <= min; i++)
  4078. {
  4079. if (eptr >= md->end_subject)
  4080. {
  4081. SCHECK_PARTIAL();
  4082. RRETURN(MATCH_NOMATCH);
  4083. }
  4084. switch(*eptr++)
  4085. {
  4086. default: break;
  4087. HSPACE_BYTE_CASES:
  4088. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4089. HSPACE_MULTIBYTE_CASES:
  4090. #endif
  4091. RRETURN(MATCH_NOMATCH);
  4092. }
  4093. }
  4094. break;
  4095. case OP_HSPACE:
  4096. for (i = 1; i <= min; i++)
  4097. {
  4098. if (eptr >= md->end_subject)
  4099. {
  4100. SCHECK_PARTIAL();
  4101. RRETURN(MATCH_NOMATCH);
  4102. }
  4103. switch(*eptr++)
  4104. {
  4105. default: RRETURN(MATCH_NOMATCH);
  4106. HSPACE_BYTE_CASES:
  4107. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4108. HSPACE_MULTIBYTE_CASES:
  4109. #endif
  4110. break;
  4111. }
  4112. }
  4113. break;
  4114. case OP_NOT_VSPACE:
  4115. for (i = 1; i <= min; i++)
  4116. {
  4117. if (eptr >= md->end_subject)
  4118. {
  4119. SCHECK_PARTIAL();
  4120. RRETURN(MATCH_NOMATCH);
  4121. }
  4122. switch(*eptr++)
  4123. {
  4124. VSPACE_BYTE_CASES:
  4125. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4126. VSPACE_MULTIBYTE_CASES:
  4127. #endif
  4128. RRETURN(MATCH_NOMATCH);
  4129. default: break;
  4130. }
  4131. }
  4132. break;
  4133. case OP_VSPACE:
  4134. for (i = 1; i <= min; i++)
  4135. {
  4136. if (eptr >= md->end_subject)
  4137. {
  4138. SCHECK_PARTIAL();
  4139. RRETURN(MATCH_NOMATCH);
  4140. }
  4141. switch(*eptr++)
  4142. {
  4143. default: RRETURN(MATCH_NOMATCH);
  4144. VSPACE_BYTE_CASES:
  4145. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4146. VSPACE_MULTIBYTE_CASES:
  4147. #endif
  4148. break;
  4149. }
  4150. }
  4151. break;
  4152. case OP_NOT_DIGIT:
  4153. for (i = 1; i <= min; i++)
  4154. {
  4155. if (eptr >= md->end_subject)
  4156. {
  4157. SCHECK_PARTIAL();
  4158. RRETURN(MATCH_NOMATCH);
  4159. }
  4160. if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_digit) != 0)
  4161. RRETURN(MATCH_NOMATCH);
  4162. eptr++;
  4163. }
  4164. break;
  4165. case OP_DIGIT:
  4166. for (i = 1; i <= min; i++)
  4167. {
  4168. if (eptr >= md->end_subject)
  4169. {
  4170. SCHECK_PARTIAL();
  4171. RRETURN(MATCH_NOMATCH);
  4172. }
  4173. if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_digit) == 0)
  4174. RRETURN(MATCH_NOMATCH);
  4175. eptr++;
  4176. }
  4177. break;
  4178. case OP_NOT_WHITESPACE:
  4179. for (i = 1; i <= min; i++)
  4180. {
  4181. if (eptr >= md->end_subject)
  4182. {
  4183. SCHECK_PARTIAL();
  4184. RRETURN(MATCH_NOMATCH);
  4185. }
  4186. if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_space) != 0)
  4187. RRETURN(MATCH_NOMATCH);
  4188. eptr++;
  4189. }
  4190. break;
  4191. case OP_WHITESPACE:
  4192. for (i = 1; i <= min; i++)
  4193. {
  4194. if (eptr >= md->end_subject)
  4195. {
  4196. SCHECK_PARTIAL();
  4197. RRETURN(MATCH_NOMATCH);
  4198. }
  4199. if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_space) == 0)
  4200. RRETURN(MATCH_NOMATCH);
  4201. eptr++;
  4202. }
  4203. break;
  4204. case OP_NOT_WORDCHAR:
  4205. for (i = 1; i <= min; i++)
  4206. {
  4207. if (eptr >= md->end_subject)
  4208. {
  4209. SCHECK_PARTIAL();
  4210. RRETURN(MATCH_NOMATCH);
  4211. }
  4212. if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_word) != 0)
  4213. RRETURN(MATCH_NOMATCH);
  4214. eptr++;
  4215. }
  4216. break;
  4217. case OP_WORDCHAR:
  4218. for (i = 1; i <= min; i++)
  4219. {
  4220. if (eptr >= md->end_subject)
  4221. {
  4222. SCHECK_PARTIAL();
  4223. RRETURN(MATCH_NOMATCH);
  4224. }
  4225. if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_word) == 0)
  4226. RRETURN(MATCH_NOMATCH);
  4227. eptr++;
  4228. }
  4229. break;
  4230. default:
  4231. RRETURN(PCRE_ERROR_INTERNAL);
  4232. }
  4233. }
  4234. /* If min = max, continue at the same level without recursing */
  4235. if (min == max) continue;
  4236. /* If minimizing, we have to test the rest of the pattern before each
  4237. subsequent match. Again, separate the UTF-8 case for speed, and also
  4238. separate the UCP cases. */
  4239. if (minimize)
  4240. {
  4241. #ifdef SUPPORT_UCP
  4242. if (prop_type >= 0)
  4243. {
  4244. switch(prop_type)
  4245. {
  4246. case PT_ANY:
  4247. for (fi = min;; fi++)
  4248. {
  4249. RMATCH(eptr, ecode, offset_top, md, eptrb, RM36);
  4250. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4251. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4252. if (eptr >= md->end_subject)
  4253. {
  4254. SCHECK_PARTIAL();
  4255. RRETURN(MATCH_NOMATCH);
  4256. }
  4257. GETCHARINCTEST(c, eptr);
  4258. if (prop_fail_result) RRETURN(MATCH_NOMATCH);
  4259. }
  4260. /* Control never gets here */
  4261. case PT_LAMP:
  4262. for (fi = min;; fi++)
  4263. {
  4264. int chartype;
  4265. RMATCH(eptr, ecode, offset_top, md, eptrb, RM37);
  4266. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4267. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4268. if (eptr >= md->end_subject)
  4269. {
  4270. SCHECK_PARTIAL();
  4271. RRETURN(MATCH_NOMATCH);
  4272. }
  4273. GETCHARINCTEST(c, eptr);
  4274. chartype = UCD_CHARTYPE(c);
  4275. if ((chartype == ucp_Lu ||
  4276. chartype == ucp_Ll ||
  4277. chartype == ucp_Lt) == prop_fail_result)
  4278. RRETURN(MATCH_NOMATCH);
  4279. }
  4280. /* Control never gets here */
  4281. case PT_GC:
  4282. for (fi = min;; fi++)
  4283. {
  4284. RMATCH(eptr, ecode, offset_top, md, eptrb, RM38);
  4285. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4286. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4287. if (eptr >= md->end_subject)
  4288. {
  4289. SCHECK_PARTIAL();
  4290. RRETURN(MATCH_NOMATCH);
  4291. }
  4292. GETCHARINCTEST(c, eptr);
  4293. if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result)
  4294. RRETURN(MATCH_NOMATCH);
  4295. }
  4296. /* Control never gets here */
  4297. case PT_PC:
  4298. for (fi = min;; fi++)
  4299. {
  4300. RMATCH(eptr, ecode, offset_top, md, eptrb, RM39);
  4301. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4302. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4303. if (eptr >= md->end_subject)
  4304. {
  4305. SCHECK_PARTIAL();
  4306. RRETURN(MATCH_NOMATCH);
  4307. }
  4308. GETCHARINCTEST(c, eptr);
  4309. if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result)
  4310. RRETURN(MATCH_NOMATCH);
  4311. }
  4312. /* Control never gets here */
  4313. case PT_SC:
  4314. for (fi = min;; fi++)
  4315. {
  4316. RMATCH(eptr, ecode, offset_top, md, eptrb, RM40);
  4317. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4318. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4319. if (eptr >= md->end_subject)
  4320. {
  4321. SCHECK_PARTIAL();
  4322. RRETURN(MATCH_NOMATCH);
  4323. }
  4324. GETCHARINCTEST(c, eptr);
  4325. if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result)
  4326. RRETURN(MATCH_NOMATCH);
  4327. }
  4328. /* Control never gets here */
  4329. case PT_ALNUM:
  4330. for (fi = min;; fi++)
  4331. {
  4332. int category;
  4333. RMATCH(eptr, ecode, offset_top, md, eptrb, RM59);
  4334. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4335. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4336. if (eptr >= md->end_subject)
  4337. {
  4338. SCHECK_PARTIAL();
  4339. RRETURN(MATCH_NOMATCH);
  4340. }
  4341. GETCHARINCTEST(c, eptr);
  4342. category = UCD_CATEGORY(c);
  4343. if ((category == ucp_L || category == ucp_N) == prop_fail_result)
  4344. RRETURN(MATCH_NOMATCH);
  4345. }
  4346. /* Control never gets here */
  4347. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  4348. which means that Perl space and POSIX space are now identical. PCRE
  4349. was changed at release 8.34. */
  4350. case PT_SPACE: /* Perl space */
  4351. case PT_PXSPACE: /* POSIX space */
  4352. for (fi = min;; fi++)
  4353. {
  4354. RMATCH(eptr, ecode, offset_top, md, eptrb, RM61);
  4355. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4356. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4357. if (eptr >= md->end_subject)
  4358. {
  4359. SCHECK_PARTIAL();
  4360. RRETURN(MATCH_NOMATCH);
  4361. }
  4362. GETCHARINCTEST(c, eptr);
  4363. switch(c)
  4364. {
  4365. HSPACE_CASES:
  4366. VSPACE_CASES:
  4367. if (prop_fail_result) RRETURN(MATCH_NOMATCH);
  4368. break;
  4369. default:
  4370. if ((UCD_CATEGORY(c) == ucp_Z) == prop_fail_result)
  4371. RRETURN(MATCH_NOMATCH);
  4372. break;
  4373. }
  4374. }
  4375. /* Control never gets here */
  4376. case PT_WORD:
  4377. for (fi = min;; fi++)
  4378. {
  4379. int category;
  4380. RMATCH(eptr, ecode, offset_top, md, eptrb, RM62);
  4381. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4382. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4383. if (eptr >= md->end_subject)
  4384. {
  4385. SCHECK_PARTIAL();
  4386. RRETURN(MATCH_NOMATCH);
  4387. }
  4388. GETCHARINCTEST(c, eptr);
  4389. category = UCD_CATEGORY(c);
  4390. if ((category == ucp_L ||
  4391. category == ucp_N ||
  4392. c == CHAR_UNDERSCORE)
  4393. == prop_fail_result)
  4394. RRETURN(MATCH_NOMATCH);
  4395. }
  4396. /* Control never gets here */
  4397. case PT_CLIST:
  4398. for (fi = min;; fi++)
  4399. {
  4400. const pcre_uint32 *cp;
  4401. RMATCH(eptr, ecode, offset_top, md, eptrb, RM67);
  4402. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4403. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4404. if (eptr >= md->end_subject)
  4405. {
  4406. SCHECK_PARTIAL();
  4407. RRETURN(MATCH_NOMATCH);
  4408. }
  4409. GETCHARINCTEST(c, eptr);
  4410. cp = PRIV(ucd_caseless_sets) + prop_value;
  4411. for (;;)
  4412. {
  4413. if (c < *cp)
  4414. { if (prop_fail_result) break; else { RRETURN(MATCH_NOMATCH); } }
  4415. if (c == *cp++)
  4416. { if (prop_fail_result) { RRETURN(MATCH_NOMATCH); } else break; }
  4417. }
  4418. }
  4419. /* Control never gets here */
  4420. case PT_UCNC:
  4421. for (fi = min;; fi++)
  4422. {
  4423. RMATCH(eptr, ecode, offset_top, md, eptrb, RM60);
  4424. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4425. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4426. if (eptr >= md->end_subject)
  4427. {
  4428. SCHECK_PARTIAL();
  4429. RRETURN(MATCH_NOMATCH);
  4430. }
  4431. GETCHARINCTEST(c, eptr);
  4432. if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  4433. c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
  4434. c >= 0xe000) == prop_fail_result)
  4435. RRETURN(MATCH_NOMATCH);
  4436. }
  4437. /* Control never gets here */
  4438. /* This should never occur */
  4439. default:
  4440. RRETURN(PCRE_ERROR_INTERNAL);
  4441. }
  4442. }
  4443. /* Match extended Unicode sequences. We will get here only if the
  4444. support is in the binary; otherwise a compile-time error occurs. */
  4445. else if (ctype == OP_EXTUNI)
  4446. {
  4447. for (fi = min;; fi++)
  4448. {
  4449. RMATCH(eptr, ecode, offset_top, md, eptrb, RM41);
  4450. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4451. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4452. if (eptr >= md->end_subject)
  4453. {
  4454. SCHECK_PARTIAL();
  4455. RRETURN(MATCH_NOMATCH);
  4456. }
  4457. else
  4458. {
  4459. int lgb, rgb;
  4460. GETCHARINCTEST(c, eptr);
  4461. lgb = UCD_GRAPHBREAK(c);
  4462. while (eptr < md->end_subject)
  4463. {
  4464. int len = 1;
  4465. if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
  4466. rgb = UCD_GRAPHBREAK(c);
  4467. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  4468. lgb = rgb;
  4469. eptr += len;
  4470. }
  4471. }
  4472. CHECK_PARTIAL();
  4473. }
  4474. }
  4475. else
  4476. #endif /* SUPPORT_UCP */
  4477. #ifdef SUPPORT_UTF
  4478. if (utf)
  4479. {
  4480. for (fi = min;; fi++)
  4481. {
  4482. RMATCH(eptr, ecode, offset_top, md, eptrb, RM42);
  4483. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4484. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4485. if (eptr >= md->end_subject)
  4486. {
  4487. SCHECK_PARTIAL();
  4488. RRETURN(MATCH_NOMATCH);
  4489. }
  4490. if (ctype == OP_ANY && IS_NEWLINE(eptr))
  4491. RRETURN(MATCH_NOMATCH);
  4492. GETCHARINC(c, eptr);
  4493. switch(ctype)
  4494. {
  4495. case OP_ANY: /* This is the non-NL case */
  4496. if (md->partial != 0 && /* Take care with CRLF partial */
  4497. eptr >= md->end_subject &&
  4498. NLBLOCK->nltype == NLTYPE_FIXED &&
  4499. NLBLOCK->nllen == 2 &&
  4500. c == NLBLOCK->nl[0])
  4501. {
  4502. md->hitend = TRUE;
  4503. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  4504. }
  4505. break;
  4506. case OP_ALLANY:
  4507. case OP_ANYBYTE:
  4508. break;
  4509. case OP_ANYNL:
  4510. switch(c)
  4511. {
  4512. default: RRETURN(MATCH_NOMATCH);
  4513. case CHAR_CR:
  4514. if (eptr < md->end_subject && UCHAR21(eptr) == CHAR_LF) eptr++;
  4515. break;
  4516. case CHAR_LF:
  4517. break;
  4518. case CHAR_VT:
  4519. case CHAR_FF:
  4520. case CHAR_NEL:
  4521. #ifndef EBCDIC
  4522. case 0x2028:
  4523. case 0x2029:
  4524. #endif /* Not EBCDIC */
  4525. if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
  4526. break;
  4527. }
  4528. break;
  4529. case OP_NOT_HSPACE:
  4530. switch(c)
  4531. {
  4532. HSPACE_CASES: RRETURN(MATCH_NOMATCH);
  4533. default: break;
  4534. }
  4535. break;
  4536. case OP_HSPACE:
  4537. switch(c)
  4538. {
  4539. HSPACE_CASES: break;
  4540. default: RRETURN(MATCH_NOMATCH);
  4541. }
  4542. break;
  4543. case OP_NOT_VSPACE:
  4544. switch(c)
  4545. {
  4546. VSPACE_CASES: RRETURN(MATCH_NOMATCH);
  4547. default: break;
  4548. }
  4549. break;
  4550. case OP_VSPACE:
  4551. switch(c)
  4552. {
  4553. VSPACE_CASES: break;
  4554. default: RRETURN(MATCH_NOMATCH);
  4555. }
  4556. break;
  4557. case OP_NOT_DIGIT:
  4558. if (c < 256 && (md->ctypes[c] & ctype_digit) != 0)
  4559. RRETURN(MATCH_NOMATCH);
  4560. break;
  4561. case OP_DIGIT:
  4562. if (c >= 256 || (md->ctypes[c] & ctype_digit) == 0)
  4563. RRETURN(MATCH_NOMATCH);
  4564. break;
  4565. case OP_NOT_WHITESPACE:
  4566. if (c < 256 && (md->ctypes[c] & ctype_space) != 0)
  4567. RRETURN(MATCH_NOMATCH);
  4568. break;
  4569. case OP_WHITESPACE:
  4570. if (c >= 256 || (md->ctypes[c] & ctype_space) == 0)
  4571. RRETURN(MATCH_NOMATCH);
  4572. break;
  4573. case OP_NOT_WORDCHAR:
  4574. if (c < 256 && (md->ctypes[c] & ctype_word) != 0)
  4575. RRETURN(MATCH_NOMATCH);
  4576. break;
  4577. case OP_WORDCHAR:
  4578. if (c >= 256 || (md->ctypes[c] & ctype_word) == 0)
  4579. RRETURN(MATCH_NOMATCH);
  4580. break;
  4581. default:
  4582. RRETURN(PCRE_ERROR_INTERNAL);
  4583. }
  4584. }
  4585. }
  4586. else
  4587. #endif
  4588. /* Not UTF mode */
  4589. {
  4590. for (fi = min;; fi++)
  4591. {
  4592. RMATCH(eptr, ecode, offset_top, md, eptrb, RM43);
  4593. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4594. if (fi >= max) RRETURN(MATCH_NOMATCH);
  4595. if (eptr >= md->end_subject)
  4596. {
  4597. SCHECK_PARTIAL();
  4598. RRETURN(MATCH_NOMATCH);
  4599. }
  4600. if (ctype == OP_ANY && IS_NEWLINE(eptr))
  4601. RRETURN(MATCH_NOMATCH);
  4602. c = *eptr++;
  4603. switch(ctype)
  4604. {
  4605. case OP_ANY: /* This is the non-NL case */
  4606. if (md->partial != 0 && /* Take care with CRLF partial */
  4607. eptr >= md->end_subject &&
  4608. NLBLOCK->nltype == NLTYPE_FIXED &&
  4609. NLBLOCK->nllen == 2 &&
  4610. c == NLBLOCK->nl[0])
  4611. {
  4612. md->hitend = TRUE;
  4613. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  4614. }
  4615. break;
  4616. case OP_ALLANY:
  4617. case OP_ANYBYTE:
  4618. break;
  4619. case OP_ANYNL:
  4620. switch(c)
  4621. {
  4622. default: RRETURN(MATCH_NOMATCH);
  4623. case CHAR_CR:
  4624. if (eptr < md->end_subject && *eptr == CHAR_LF) eptr++;
  4625. break;
  4626. case CHAR_LF:
  4627. break;
  4628. case CHAR_VT:
  4629. case CHAR_FF:
  4630. case CHAR_NEL:
  4631. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4632. case 0x2028:
  4633. case 0x2029:
  4634. #endif
  4635. if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
  4636. break;
  4637. }
  4638. break;
  4639. case OP_NOT_HSPACE:
  4640. switch(c)
  4641. {
  4642. default: break;
  4643. HSPACE_BYTE_CASES:
  4644. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4645. HSPACE_MULTIBYTE_CASES:
  4646. #endif
  4647. RRETURN(MATCH_NOMATCH);
  4648. }
  4649. break;
  4650. case OP_HSPACE:
  4651. switch(c)
  4652. {
  4653. default: RRETURN(MATCH_NOMATCH);
  4654. HSPACE_BYTE_CASES:
  4655. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4656. HSPACE_MULTIBYTE_CASES:
  4657. #endif
  4658. break;
  4659. }
  4660. break;
  4661. case OP_NOT_VSPACE:
  4662. switch(c)
  4663. {
  4664. default: break;
  4665. VSPACE_BYTE_CASES:
  4666. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4667. VSPACE_MULTIBYTE_CASES:
  4668. #endif
  4669. RRETURN(MATCH_NOMATCH);
  4670. }
  4671. break;
  4672. case OP_VSPACE:
  4673. switch(c)
  4674. {
  4675. default: RRETURN(MATCH_NOMATCH);
  4676. VSPACE_BYTE_CASES:
  4677. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  4678. VSPACE_MULTIBYTE_CASES:
  4679. #endif
  4680. break;
  4681. }
  4682. break;
  4683. case OP_NOT_DIGIT:
  4684. if (MAX_255(c) && (md->ctypes[c] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH);
  4685. break;
  4686. case OP_DIGIT:
  4687. if (!MAX_255(c) || (md->ctypes[c] & ctype_digit) == 0) RRETURN(MATCH_NOMATCH);
  4688. break;
  4689. case OP_NOT_WHITESPACE:
  4690. if (MAX_255(c) && (md->ctypes[c] & ctype_space) != 0) RRETURN(MATCH_NOMATCH);
  4691. break;
  4692. case OP_WHITESPACE:
  4693. if (!MAX_255(c) || (md->ctypes[c] & ctype_space) == 0) RRETURN(MATCH_NOMATCH);
  4694. break;
  4695. case OP_NOT_WORDCHAR:
  4696. if (MAX_255(c) && (md->ctypes[c] & ctype_word) != 0) RRETURN(MATCH_NOMATCH);
  4697. break;
  4698. case OP_WORDCHAR:
  4699. if (!MAX_255(c) || (md->ctypes[c] & ctype_word) == 0) RRETURN(MATCH_NOMATCH);
  4700. break;
  4701. default:
  4702. RRETURN(PCRE_ERROR_INTERNAL);
  4703. }
  4704. }
  4705. }
  4706. /* Control never gets here */
  4707. }
  4708. /* If maximizing, it is worth using inline code for speed, doing the type
  4709. test once at the start (i.e. keep it out of the loop). Again, keep the
  4710. UTF-8 and UCP stuff separate. */
  4711. else
  4712. {
  4713. pp = eptr; /* Remember where we started */
  4714. #ifdef SUPPORT_UCP
  4715. if (prop_type >= 0)
  4716. {
  4717. switch(prop_type)
  4718. {
  4719. case PT_ANY:
  4720. for (i = min; i < max; i++)
  4721. {
  4722. int len = 1;
  4723. if (eptr >= md->end_subject)
  4724. {
  4725. SCHECK_PARTIAL();
  4726. break;
  4727. }
  4728. GETCHARLENTEST(c, eptr, len);
  4729. if (prop_fail_result) break;
  4730. eptr+= len;
  4731. }
  4732. break;
  4733. case PT_LAMP:
  4734. for (i = min; i < max; i++)
  4735. {
  4736. int chartype;
  4737. int len = 1;
  4738. if (eptr >= md->end_subject)
  4739. {
  4740. SCHECK_PARTIAL();
  4741. break;
  4742. }
  4743. GETCHARLENTEST(c, eptr, len);
  4744. chartype = UCD_CHARTYPE(c);
  4745. if ((chartype == ucp_Lu ||
  4746. chartype == ucp_Ll ||
  4747. chartype == ucp_Lt) == prop_fail_result)
  4748. break;
  4749. eptr+= len;
  4750. }
  4751. break;
  4752. case PT_GC:
  4753. for (i = min; i < max; i++)
  4754. {
  4755. int len = 1;
  4756. if (eptr >= md->end_subject)
  4757. {
  4758. SCHECK_PARTIAL();
  4759. break;
  4760. }
  4761. GETCHARLENTEST(c, eptr, len);
  4762. if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result) break;
  4763. eptr+= len;
  4764. }
  4765. break;
  4766. case PT_PC:
  4767. for (i = min; i < max; i++)
  4768. {
  4769. int len = 1;
  4770. if (eptr >= md->end_subject)
  4771. {
  4772. SCHECK_PARTIAL();
  4773. break;
  4774. }
  4775. GETCHARLENTEST(c, eptr, len);
  4776. if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result) break;
  4777. eptr+= len;
  4778. }
  4779. break;
  4780. case PT_SC:
  4781. for (i = min; i < max; i++)
  4782. {
  4783. int len = 1;
  4784. if (eptr >= md->end_subject)
  4785. {
  4786. SCHECK_PARTIAL();
  4787. break;
  4788. }
  4789. GETCHARLENTEST(c, eptr, len);
  4790. if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result) break;
  4791. eptr+= len;
  4792. }
  4793. break;
  4794. case PT_ALNUM:
  4795. for (i = min; i < max; i++)
  4796. {
  4797. int category;
  4798. int len = 1;
  4799. if (eptr >= md->end_subject)
  4800. {
  4801. SCHECK_PARTIAL();
  4802. break;
  4803. }
  4804. GETCHARLENTEST(c, eptr, len);
  4805. category = UCD_CATEGORY(c);
  4806. if ((category == ucp_L || category == ucp_N) == prop_fail_result)
  4807. break;
  4808. eptr+= len;
  4809. }
  4810. break;
  4811. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  4812. which means that Perl space and POSIX space are now identical. PCRE
  4813. was changed at release 8.34. */
  4814. case PT_SPACE: /* Perl space */
  4815. case PT_PXSPACE: /* POSIX space */
  4816. for (i = min; i < max; i++)
  4817. {
  4818. int len = 1;
  4819. if (eptr >= md->end_subject)
  4820. {
  4821. SCHECK_PARTIAL();
  4822. break;
  4823. }
  4824. GETCHARLENTEST(c, eptr, len);
  4825. switch(c)
  4826. {
  4827. HSPACE_CASES:
  4828. VSPACE_CASES:
  4829. if (prop_fail_result) goto ENDLOOP99; /* Break the loop */
  4830. break;
  4831. default:
  4832. if ((UCD_CATEGORY(c) == ucp_Z) == prop_fail_result)
  4833. goto ENDLOOP99; /* Break the loop */
  4834. break;
  4835. }
  4836. eptr+= len;
  4837. }
  4838. ENDLOOP99:
  4839. break;
  4840. case PT_WORD:
  4841. for (i = min; i < max; i++)
  4842. {
  4843. int category;
  4844. int len = 1;
  4845. if (eptr >= md->end_subject)
  4846. {
  4847. SCHECK_PARTIAL();
  4848. break;
  4849. }
  4850. GETCHARLENTEST(c, eptr, len);
  4851. category = UCD_CATEGORY(c);
  4852. if ((category == ucp_L || category == ucp_N ||
  4853. c == CHAR_UNDERSCORE) == prop_fail_result)
  4854. break;
  4855. eptr+= len;
  4856. }
  4857. break;
  4858. case PT_CLIST:
  4859. for (i = min; i < max; i++)
  4860. {
  4861. const pcre_uint32 *cp;
  4862. int len = 1;
  4863. if (eptr >= md->end_subject)
  4864. {
  4865. SCHECK_PARTIAL();
  4866. break;
  4867. }
  4868. GETCHARLENTEST(c, eptr, len);
  4869. cp = PRIV(ucd_caseless_sets) + prop_value;
  4870. for (;;)
  4871. {
  4872. if (c < *cp)
  4873. { if (prop_fail_result) break; else goto GOT_MAX; }
  4874. if (c == *cp++)
  4875. { if (prop_fail_result) goto GOT_MAX; else break; }
  4876. }
  4877. eptr += len;
  4878. }
  4879. GOT_MAX:
  4880. break;
  4881. case PT_UCNC:
  4882. for (i = min; i < max; i++)
  4883. {
  4884. int len = 1;
  4885. if (eptr >= md->end_subject)
  4886. {
  4887. SCHECK_PARTIAL();
  4888. break;
  4889. }
  4890. GETCHARLENTEST(c, eptr, len);
  4891. if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  4892. c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
  4893. c >= 0xe000) == prop_fail_result)
  4894. break;
  4895. eptr += len;
  4896. }
  4897. break;
  4898. default:
  4899. RRETURN(PCRE_ERROR_INTERNAL);
  4900. }
  4901. /* eptr is now past the end of the maximum run */
  4902. if (possessive) continue; /* No backtracking */
  4903. for(;;)
  4904. {
  4905. if (eptr <= pp) goto TAIL_RECURSE;
  4906. RMATCH(eptr, ecode, offset_top, md, eptrb, RM44);
  4907. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4908. eptr--;
  4909. if (utf) BACKCHAR(eptr);
  4910. }
  4911. }
  4912. /* Match extended Unicode grapheme clusters. We will get here only if the
  4913. support is in the binary; otherwise a compile-time error occurs. */
  4914. else if (ctype == OP_EXTUNI)
  4915. {
  4916. for (i = min; i < max; i++)
  4917. {
  4918. if (eptr >= md->end_subject)
  4919. {
  4920. SCHECK_PARTIAL();
  4921. break;
  4922. }
  4923. else
  4924. {
  4925. int lgb, rgb;
  4926. GETCHARINCTEST(c, eptr);
  4927. lgb = UCD_GRAPHBREAK(c);
  4928. while (eptr < md->end_subject)
  4929. {
  4930. int len = 1;
  4931. if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
  4932. rgb = UCD_GRAPHBREAK(c);
  4933. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  4934. lgb = rgb;
  4935. eptr += len;
  4936. }
  4937. }
  4938. CHECK_PARTIAL();
  4939. }
  4940. /* eptr is now past the end of the maximum run */
  4941. if (possessive) continue; /* No backtracking */
  4942. /* We use <= pp rather than == pp to detect the start of the run while
  4943. backtracking because the use of \C in UTF mode can cause BACKCHAR to
  4944. move back past pp. This is just palliative; the use of \C in UTF mode
  4945. is fraught with danger. */
  4946. for(;;)
  4947. {
  4948. int lgb, rgb;
  4949. PCRE_PUCHAR fptr;
  4950. if (eptr <= pp) goto TAIL_RECURSE; /* At start of char run */
  4951. RMATCH(eptr, ecode, offset_top, md, eptrb, RM45);
  4952. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  4953. /* Backtracking over an extended grapheme cluster involves inspecting
  4954. the previous two characters (if present) to see if a break is
  4955. permitted between them. */
  4956. eptr--;
  4957. if (!utf) c = *eptr; else
  4958. {
  4959. BACKCHAR(eptr);
  4960. GETCHAR(c, eptr);
  4961. }
  4962. rgb = UCD_GRAPHBREAK(c);
  4963. for (;;)
  4964. {
  4965. if (eptr <= pp) goto TAIL_RECURSE; /* At start of char run */
  4966. fptr = eptr - 1;
  4967. if (!utf) c = *fptr; else
  4968. {
  4969. BACKCHAR(fptr);
  4970. GETCHAR(c, fptr);
  4971. }
  4972. lgb = UCD_GRAPHBREAK(c);
  4973. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  4974. eptr = fptr;
  4975. rgb = lgb;
  4976. }
  4977. }
  4978. }
  4979. else
  4980. #endif /* SUPPORT_UCP */
  4981. #ifdef SUPPORT_UTF
  4982. if (utf)
  4983. {
  4984. switch(ctype)
  4985. {
  4986. case OP_ANY:
  4987. for (i = min; i < max; i++)
  4988. {
  4989. if (eptr >= md->end_subject)
  4990. {
  4991. SCHECK_PARTIAL();
  4992. break;
  4993. }
  4994. if (IS_NEWLINE(eptr)) break;
  4995. if (md->partial != 0 && /* Take care with CRLF partial */
  4996. eptr + 1 >= md->end_subject &&
  4997. NLBLOCK->nltype == NLTYPE_FIXED &&
  4998. NLBLOCK->nllen == 2 &&
  4999. UCHAR21(eptr) == NLBLOCK->nl[0])
  5000. {
  5001. md->hitend = TRUE;
  5002. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  5003. }
  5004. eptr++;
  5005. ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
  5006. }
  5007. break;
  5008. case OP_ALLANY:
  5009. if (max < INT_MAX)
  5010. {
  5011. for (i = min; i < max; i++)
  5012. {
  5013. if (eptr >= md->end_subject)
  5014. {
  5015. SCHECK_PARTIAL();
  5016. break;
  5017. }
  5018. eptr++;
  5019. ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
  5020. }
  5021. }
  5022. else
  5023. {
  5024. eptr = md->end_subject; /* Unlimited UTF-8 repeat */
  5025. SCHECK_PARTIAL();
  5026. }
  5027. break;
  5028. /* The byte case is the same as non-UTF8 */
  5029. case OP_ANYBYTE:
  5030. c = max - min;
  5031. if (c > (unsigned int)(md->end_subject - eptr))
  5032. {
  5033. eptr = md->end_subject;
  5034. SCHECK_PARTIAL();
  5035. }
  5036. else eptr += c;
  5037. break;
  5038. case OP_ANYNL:
  5039. for (i = min; i < max; i++)
  5040. {
  5041. int len = 1;
  5042. if (eptr >= md->end_subject)
  5043. {
  5044. SCHECK_PARTIAL();
  5045. break;
  5046. }
  5047. GETCHARLEN(c, eptr, len);
  5048. if (c == CHAR_CR)
  5049. {
  5050. if (++eptr >= md->end_subject) break;
  5051. if (UCHAR21(eptr) == CHAR_LF) eptr++;
  5052. }
  5053. else
  5054. {
  5055. if (c != CHAR_LF &&
  5056. (md->bsr_anycrlf ||
  5057. (c != CHAR_VT && c != CHAR_FF && c != CHAR_NEL
  5058. #ifndef EBCDIC
  5059. && c != 0x2028 && c != 0x2029
  5060. #endif /* Not EBCDIC */
  5061. )))
  5062. break;
  5063. eptr += len;
  5064. }
  5065. }
  5066. break;
  5067. case OP_NOT_HSPACE:
  5068. case OP_HSPACE:
  5069. for (i = min; i < max; i++)
  5070. {
  5071. BOOL gotspace;
  5072. int len = 1;
  5073. if (eptr >= md->end_subject)
  5074. {
  5075. SCHECK_PARTIAL();
  5076. break;
  5077. }
  5078. GETCHARLEN(c, eptr, len);
  5079. switch(c)
  5080. {
  5081. HSPACE_CASES: gotspace = TRUE; break;
  5082. default: gotspace = FALSE; break;
  5083. }
  5084. if (gotspace == (ctype == OP_NOT_HSPACE)) break;
  5085. eptr += len;
  5086. }
  5087. break;
  5088. case OP_NOT_VSPACE:
  5089. case OP_VSPACE:
  5090. for (i = min; i < max; i++)
  5091. {
  5092. BOOL gotspace;
  5093. int len = 1;
  5094. if (eptr >= md->end_subject)
  5095. {
  5096. SCHECK_PARTIAL();
  5097. break;
  5098. }
  5099. GETCHARLEN(c, eptr, len);
  5100. switch(c)
  5101. {
  5102. VSPACE_CASES: gotspace = TRUE; break;
  5103. default: gotspace = FALSE; break;
  5104. }
  5105. if (gotspace == (ctype == OP_NOT_VSPACE)) break;
  5106. eptr += len;
  5107. }
  5108. break;
  5109. case OP_NOT_DIGIT:
  5110. for (i = min; i < max; i++)
  5111. {
  5112. int len = 1;
  5113. if (eptr >= md->end_subject)
  5114. {
  5115. SCHECK_PARTIAL();
  5116. break;
  5117. }
  5118. GETCHARLEN(c, eptr, len);
  5119. if (c < 256 && (md->ctypes[c] & ctype_digit) != 0) break;
  5120. eptr+= len;
  5121. }
  5122. break;
  5123. case OP_DIGIT:
  5124. for (i = min; i < max; i++)
  5125. {
  5126. int len = 1;
  5127. if (eptr >= md->end_subject)
  5128. {
  5129. SCHECK_PARTIAL();
  5130. break;
  5131. }
  5132. GETCHARLEN(c, eptr, len);
  5133. if (c >= 256 ||(md->ctypes[c] & ctype_digit) == 0) break;
  5134. eptr+= len;
  5135. }
  5136. break;
  5137. case OP_NOT_WHITESPACE:
  5138. for (i = min; i < max; i++)
  5139. {
  5140. int len = 1;
  5141. if (eptr >= md->end_subject)
  5142. {
  5143. SCHECK_PARTIAL();
  5144. break;
  5145. }
  5146. GETCHARLEN(c, eptr, len);
  5147. if (c < 256 && (md->ctypes[c] & ctype_space) != 0) break;
  5148. eptr+= len;
  5149. }
  5150. break;
  5151. case OP_WHITESPACE:
  5152. for (i = min; i < max; i++)
  5153. {
  5154. int len = 1;
  5155. if (eptr >= md->end_subject)
  5156. {
  5157. SCHECK_PARTIAL();
  5158. break;
  5159. }
  5160. GETCHARLEN(c, eptr, len);
  5161. if (c >= 256 ||(md->ctypes[c] & ctype_space) == 0) break;
  5162. eptr+= len;
  5163. }
  5164. break;
  5165. case OP_NOT_WORDCHAR:
  5166. for (i = min; i < max; i++)
  5167. {
  5168. int len = 1;
  5169. if (eptr >= md->end_subject)
  5170. {
  5171. SCHECK_PARTIAL();
  5172. break;
  5173. }
  5174. GETCHARLEN(c, eptr, len);
  5175. if (c < 256 && (md->ctypes[c] & ctype_word) != 0) break;
  5176. eptr+= len;
  5177. }
  5178. break;
  5179. case OP_WORDCHAR:
  5180. for (i = min; i < max; i++)
  5181. {
  5182. int len = 1;
  5183. if (eptr >= md->end_subject)
  5184. {
  5185. SCHECK_PARTIAL();
  5186. break;
  5187. }
  5188. GETCHARLEN(c, eptr, len);
  5189. if (c >= 256 || (md->ctypes[c] & ctype_word) == 0) break;
  5190. eptr+= len;
  5191. }
  5192. break;
  5193. default:
  5194. RRETURN(PCRE_ERROR_INTERNAL);
  5195. }
  5196. if (possessive) continue; /* No backtracking */
  5197. for(;;)
  5198. {
  5199. if (eptr <= pp) goto TAIL_RECURSE;
  5200. RMATCH(eptr, ecode, offset_top, md, eptrb, RM46);
  5201. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  5202. eptr--;
  5203. BACKCHAR(eptr);
  5204. if (ctype == OP_ANYNL && eptr > pp && UCHAR21(eptr) == CHAR_NL &&
  5205. UCHAR21(eptr - 1) == CHAR_CR) eptr--;
  5206. }
  5207. }
  5208. else
  5209. #endif /* SUPPORT_UTF */
  5210. /* Not UTF mode */
  5211. {
  5212. switch(ctype)
  5213. {
  5214. case OP_ANY:
  5215. for (i = min; i < max; i++)
  5216. {
  5217. if (eptr >= md->end_subject)
  5218. {
  5219. SCHECK_PARTIAL();
  5220. break;
  5221. }
  5222. if (IS_NEWLINE(eptr)) break;
  5223. if (md->partial != 0 && /* Take care with CRLF partial */
  5224. eptr + 1 >= md->end_subject &&
  5225. NLBLOCK->nltype == NLTYPE_FIXED &&
  5226. NLBLOCK->nllen == 2 &&
  5227. *eptr == NLBLOCK->nl[0])
  5228. {
  5229. md->hitend = TRUE;
  5230. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
  5231. }
  5232. eptr++;
  5233. }
  5234. break;
  5235. case OP_ALLANY:
  5236. case OP_ANYBYTE:
  5237. c = max - min;
  5238. if (c > (unsigned int)(md->end_subject - eptr))
  5239. {
  5240. eptr = md->end_subject;
  5241. SCHECK_PARTIAL();
  5242. }
  5243. else eptr += c;
  5244. break;
  5245. case OP_ANYNL:
  5246. for (i = min; i < max; i++)
  5247. {
  5248. if (eptr >= md->end_subject)
  5249. {
  5250. SCHECK_PARTIAL();
  5251. break;
  5252. }
  5253. c = *eptr;
  5254. if (c == CHAR_CR)
  5255. {
  5256. if (++eptr >= md->end_subject) break;
  5257. if (*eptr == CHAR_LF) eptr++;
  5258. }
  5259. else
  5260. {
  5261. if (c != CHAR_LF && (md->bsr_anycrlf ||
  5262. (c != CHAR_VT && c != CHAR_FF && c != CHAR_NEL
  5263. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  5264. && c != 0x2028 && c != 0x2029
  5265. #endif
  5266. ))) break;
  5267. eptr++;
  5268. }
  5269. }
  5270. break;
  5271. case OP_NOT_HSPACE:
  5272. for (i = min; i < max; i++)
  5273. {
  5274. if (eptr >= md->end_subject)
  5275. {
  5276. SCHECK_PARTIAL();
  5277. break;
  5278. }
  5279. switch(*eptr)
  5280. {
  5281. default: eptr++; break;
  5282. HSPACE_BYTE_CASES:
  5283. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  5284. HSPACE_MULTIBYTE_CASES:
  5285. #endif
  5286. goto ENDLOOP00;
  5287. }
  5288. }
  5289. ENDLOOP00:
  5290. break;
  5291. case OP_HSPACE:
  5292. for (i = min; i < max; i++)
  5293. {
  5294. if (eptr >= md->end_subject)
  5295. {
  5296. SCHECK_PARTIAL();
  5297. break;
  5298. }
  5299. switch(*eptr)
  5300. {
  5301. default: goto ENDLOOP01;
  5302. HSPACE_BYTE_CASES:
  5303. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  5304. HSPACE_MULTIBYTE_CASES:
  5305. #endif
  5306. eptr++; break;
  5307. }
  5308. }
  5309. ENDLOOP01:
  5310. break;
  5311. case OP_NOT_VSPACE:
  5312. for (i = min; i < max; i++)
  5313. {
  5314. if (eptr >= md->end_subject)
  5315. {
  5316. SCHECK_PARTIAL();
  5317. break;
  5318. }
  5319. switch(*eptr)
  5320. {
  5321. default: eptr++; break;
  5322. VSPACE_BYTE_CASES:
  5323. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  5324. VSPACE_MULTIBYTE_CASES:
  5325. #endif
  5326. goto ENDLOOP02;
  5327. }
  5328. }
  5329. ENDLOOP02:
  5330. break;
  5331. case OP_VSPACE:
  5332. for (i = min; i < max; i++)
  5333. {
  5334. if (eptr >= md->end_subject)
  5335. {
  5336. SCHECK_PARTIAL();
  5337. break;
  5338. }
  5339. switch(*eptr)
  5340. {
  5341. default: goto ENDLOOP03;
  5342. VSPACE_BYTE_CASES:
  5343. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  5344. VSPACE_MULTIBYTE_CASES:
  5345. #endif
  5346. eptr++; break;
  5347. }
  5348. }
  5349. ENDLOOP03:
  5350. break;
  5351. case OP_NOT_DIGIT:
  5352. for (i = min; i < max; i++)
  5353. {
  5354. if (eptr >= md->end_subject)
  5355. {
  5356. SCHECK_PARTIAL();
  5357. break;
  5358. }
  5359. if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_digit) != 0) break;
  5360. eptr++;
  5361. }
  5362. break;
  5363. case OP_DIGIT:
  5364. for (i = min; i < max; i++)
  5365. {
  5366. if (eptr >= md->end_subject)
  5367. {
  5368. SCHECK_PARTIAL();
  5369. break;
  5370. }
  5371. if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_digit) == 0) break;
  5372. eptr++;
  5373. }
  5374. break;
  5375. case OP_NOT_WHITESPACE:
  5376. for (i = min; i < max; i++)
  5377. {
  5378. if (eptr >= md->end_subject)
  5379. {
  5380. SCHECK_PARTIAL();
  5381. break;
  5382. }
  5383. if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_space) != 0) break;
  5384. eptr++;
  5385. }
  5386. break;
  5387. case OP_WHITESPACE:
  5388. for (i = min; i < max; i++)
  5389. {
  5390. if (eptr >= md->end_subject)
  5391. {
  5392. SCHECK_PARTIAL();
  5393. break;
  5394. }
  5395. if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_space) == 0) break;
  5396. eptr++;
  5397. }
  5398. break;
  5399. case OP_NOT_WORDCHAR:
  5400. for (i = min; i < max; i++)
  5401. {
  5402. if (eptr >= md->end_subject)
  5403. {
  5404. SCHECK_PARTIAL();
  5405. break;
  5406. }
  5407. if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_word) != 0) break;
  5408. eptr++;
  5409. }
  5410. break;
  5411. case OP_WORDCHAR:
  5412. for (i = min; i < max; i++)
  5413. {
  5414. if (eptr >= md->end_subject)
  5415. {
  5416. SCHECK_PARTIAL();
  5417. break;
  5418. }
  5419. if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_word) == 0) break;
  5420. eptr++;
  5421. }
  5422. break;
  5423. default:
  5424. RRETURN(PCRE_ERROR_INTERNAL);
  5425. }
  5426. if (possessive) continue; /* No backtracking */
  5427. for (;;)
  5428. {
  5429. if (eptr == pp) goto TAIL_RECURSE;
  5430. RMATCH(eptr, ecode, offset_top, md, eptrb, RM47);
  5431. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  5432. eptr--;
  5433. if (ctype == OP_ANYNL && eptr > pp && *eptr == CHAR_LF &&
  5434. eptr[-1] == CHAR_CR) eptr--;
  5435. }
  5436. }
  5437. /* Control never gets here */
  5438. }
  5439. /* There's been some horrible disaster. Arrival here can only mean there is
  5440. something seriously wrong in the code above or the OP_xxx definitions. */
  5441. default:
  5442. DPRINTF(("Unknown opcode %d\n", *ecode));
  5443. RRETURN(PCRE_ERROR_UNKNOWN_OPCODE);
  5444. }
  5445. /* Do not stick any code in here without much thought; it is assumed
  5446. that "continue" in the code above comes out to here to repeat the main
  5447. loop. */
  5448. } /* End of main loop */
  5449. /* Control never reaches here */
  5450. /* When compiling to use the heap rather than the stack for recursive calls to
  5451. match(), the RRETURN() macro jumps here. The number that is saved in
  5452. frame->Xwhere indicates which label we actually want to return to. */
  5453. #ifdef NO_RECURSE
  5454. #define LBL(val) case val: goto L_RM##val;
  5455. HEAP_RETURN:
  5456. switch (frame->Xwhere)
  5457. {
  5458. LBL( 1) LBL( 2) LBL( 3) LBL( 4) LBL( 5) LBL( 6) LBL( 7) LBL( 8)
  5459. LBL( 9) LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(17)
  5460. LBL(19) LBL(24) LBL(25) LBL(26) LBL(27) LBL(29) LBL(31) LBL(33)
  5461. LBL(35) LBL(43) LBL(47) LBL(48) LBL(49) LBL(50) LBL(51) LBL(52)
  5462. LBL(53) LBL(54) LBL(55) LBL(56) LBL(57) LBL(58) LBL(63) LBL(64)
  5463. LBL(65) LBL(66)
  5464. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  5465. LBL(20) LBL(21)
  5466. #endif
  5467. #ifdef SUPPORT_UTF
  5468. LBL(16) LBL(18)
  5469. LBL(22) LBL(23) LBL(28) LBL(30)
  5470. LBL(32) LBL(34) LBL(42) LBL(46)
  5471. #ifdef SUPPORT_UCP
  5472. LBL(36) LBL(37) LBL(38) LBL(39) LBL(40) LBL(41) LBL(44) LBL(45)
  5473. LBL(59) LBL(60) LBL(61) LBL(62) LBL(67)
  5474. #endif /* SUPPORT_UCP */
  5475. #endif /* SUPPORT_UTF */
  5476. default:
  5477. DPRINTF(("jump error in pcre match: label %d non-existent\n", frame->Xwhere));
  5478. return PCRE_ERROR_INTERNAL;
  5479. }
  5480. #undef LBL
  5481. #endif /* NO_RECURSE */
  5482. }
  5483. /***************************************************************************
  5484. ****************************************************************************
  5485. RECURSION IN THE match() FUNCTION
  5486. Undefine all the macros that were defined above to handle this. */
  5487. #ifdef NO_RECURSE
  5488. #undef eptr
  5489. #undef ecode
  5490. #undef mstart
  5491. #undef offset_top
  5492. #undef eptrb
  5493. #undef flags
  5494. #undef callpat
  5495. #undef charptr
  5496. #undef data
  5497. #undef next
  5498. #undef pp
  5499. #undef prev
  5500. #undef saved_eptr
  5501. #undef new_recursive
  5502. #undef cur_is_word
  5503. #undef condition
  5504. #undef prev_is_word
  5505. #undef ctype
  5506. #undef length
  5507. #undef max
  5508. #undef min
  5509. #undef number
  5510. #undef offset
  5511. #undef op
  5512. #undef save_capture_last
  5513. #undef save_offset1
  5514. #undef save_offset2
  5515. #undef save_offset3
  5516. #undef stacksave
  5517. #undef newptrb
  5518. #endif
  5519. /* These two are defined as macros in both cases */
  5520. #undef fc
  5521. #undef fi
  5522. /***************************************************************************
  5523. ***************************************************************************/
  5524. #ifdef NO_RECURSE
  5525. /*************************************************
  5526. * Release allocated heap frames *
  5527. *************************************************/
  5528. /* This function releases all the allocated frames. The base frame is on the
  5529. machine stack, and so must not be freed.
  5530. Argument: the address of the base frame
  5531. Returns: nothing
  5532. */
  5533. static void
  5534. release_match_heapframes (heapframe *frame_base)
  5535. {
  5536. heapframe *nextframe = frame_base->Xnextframe;
  5537. while (nextframe != NULL)
  5538. {
  5539. heapframe *oldframe = nextframe;
  5540. nextframe = nextframe->Xnextframe;
  5541. (PUBL(stack_free))(oldframe);
  5542. }
  5543. }
  5544. #endif
  5545. /*************************************************
  5546. * Execute a Regular Expression *
  5547. *************************************************/
  5548. /* This function applies a compiled re to a subject string and picks out
  5549. portions of the string if it matches. Two elements in the vector are set for
  5550. each substring: the offsets to the start and end of the substring.
  5551. Arguments:
  5552. argument_re points to the compiled expression
  5553. extra_data points to extra data or is NULL
  5554. subject points to the subject string
  5555. length length of subject string (may contain binary zeros)
  5556. start_offset where to start in the subject string
  5557. options option bits
  5558. offsets points to a vector of ints to be filled in with offsets
  5559. offsetcount the number of elements in the vector
  5560. Returns: > 0 => success; value is the number of elements filled in
  5561. = 0 => success, but offsets is not big enough
  5562. -1 => failed to match
  5563. < -1 => some kind of unexpected problem
  5564. */
  5565. #if defined COMPILE_PCRE8
  5566. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  5567. pcre_exec(const pcre *argument_re, const pcre_extra *extra_data,
  5568. PCRE_SPTR subject, int length, int start_offset, int options, int *offsets,
  5569. int offsetcount)
  5570. #elif defined COMPILE_PCRE16
  5571. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  5572. pcre16_exec(const pcre16 *argument_re, const pcre16_extra *extra_data,
  5573. PCRE_SPTR16 subject, int length, int start_offset, int options, int *offsets,
  5574. int offsetcount)
  5575. #elif defined COMPILE_PCRE32
  5576. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  5577. pcre32_exec(const pcre32 *argument_re, const pcre32_extra *extra_data,
  5578. PCRE_SPTR32 subject, int length, int start_offset, int options, int *offsets,
  5579. int offsetcount)
  5580. #endif
  5581. {
  5582. int rc, ocount, arg_offset_max;
  5583. int newline;
  5584. BOOL using_temporary_offsets = FALSE;
  5585. BOOL anchored;
  5586. BOOL startline;
  5587. BOOL firstline;
  5588. BOOL utf;
  5589. BOOL has_first_char = FALSE;
  5590. BOOL has_req_char = FALSE;
  5591. pcre_uchar first_char = 0;
  5592. pcre_uchar first_char2 = 0;
  5593. pcre_uchar req_char = 0;
  5594. pcre_uchar req_char2 = 0;
  5595. match_data match_block;
  5596. match_data *md = &match_block;
  5597. const pcre_uint8 *tables;
  5598. const pcre_uint8 *start_bits = NULL;
  5599. PCRE_PUCHAR start_match = (PCRE_PUCHAR)subject + start_offset;
  5600. PCRE_PUCHAR end_subject;
  5601. PCRE_PUCHAR start_partial = NULL;
  5602. PCRE_PUCHAR match_partial = NULL;
  5603. PCRE_PUCHAR req_char_ptr = start_match - 1;
  5604. const pcre_study_data *study;
  5605. const REAL_PCRE *re = (const REAL_PCRE *)argument_re;
  5606. #ifdef NO_RECURSE
  5607. heapframe frame_zero;
  5608. frame_zero.Xprevframe = NULL; /* Marks the top level */
  5609. frame_zero.Xnextframe = NULL; /* None are allocated yet */
  5610. md->match_frames_base = &frame_zero;
  5611. #endif
  5612. /* Check for the special magic call that measures the size of the stack used
  5613. per recursive call of match(). Without the funny casting for sizeof, a Windows
  5614. compiler gave this error: "unary minus operator applied to unsigned type,
  5615. result still unsigned". Hopefully the cast fixes that. */
  5616. if (re == NULL && extra_data == NULL && subject == NULL && length == -999 &&
  5617. start_offset == -999)
  5618. #ifdef NO_RECURSE
  5619. return -((int)sizeof(heapframe));
  5620. #else
  5621. return match(NULL, NULL, NULL, 0, NULL, NULL, 0);
  5622. #endif
  5623. /* Plausibility checks */
  5624. if ((options & ~PUBLIC_EXEC_OPTIONS) != 0) return PCRE_ERROR_BADOPTION;
  5625. if (re == NULL || subject == NULL || (offsets == NULL && offsetcount > 0))
  5626. return PCRE_ERROR_NULL;
  5627. if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
  5628. if (length < 0) return PCRE_ERROR_BADLENGTH;
  5629. if (start_offset < 0 || start_offset > length) return PCRE_ERROR_BADOFFSET;
  5630. /* Check that the first field in the block is the magic number. If it is not,
  5631. return with PCRE_ERROR_BADMAGIC. However, if the magic number is equal to
  5632. REVERSED_MAGIC_NUMBER we return with PCRE_ERROR_BADENDIANNESS, which
  5633. means that the pattern is likely compiled with different endianness. */
  5634. if (re->magic_number != MAGIC_NUMBER)
  5635. return re->magic_number == REVERSED_MAGIC_NUMBER?
  5636. PCRE_ERROR_BADENDIANNESS:PCRE_ERROR_BADMAGIC;
  5637. if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
  5638. /* These two settings are used in the code for checking a UTF-8 string that
  5639. follows immediately afterwards. Other values in the md block are used only
  5640. during "normal" pcre_exec() processing, not when the JIT support is in use,
  5641. so they are set up later. */
  5642. /* PCRE_UTF16 has the same value as PCRE_UTF8. */
  5643. utf = md->utf = (re->options & PCRE_UTF8) != 0;
  5644. md->partial = ((options & PCRE_PARTIAL_HARD) != 0)? 2 :
  5645. ((options & PCRE_PARTIAL_SOFT) != 0)? 1 : 0;
  5646. /* Check a UTF-8 string if required. Pass back the character offset and error
  5647. code for an invalid string if a results vector is available. */
  5648. #ifdef SUPPORT_UTF
  5649. if (utf && (options & PCRE_NO_UTF8_CHECK) == 0)
  5650. {
  5651. int erroroffset;
  5652. int errorcode = PRIV(valid_utf)((PCRE_PUCHAR)subject, length, &erroroffset);
  5653. if (errorcode != 0)
  5654. {
  5655. if (offsetcount >= 2)
  5656. {
  5657. offsets[0] = erroroffset;
  5658. offsets[1] = errorcode;
  5659. }
  5660. #if defined COMPILE_PCRE8
  5661. return (errorcode <= PCRE_UTF8_ERR5 && md->partial > 1)?
  5662. PCRE_ERROR_SHORTUTF8 : PCRE_ERROR_BADUTF8;
  5663. #elif defined COMPILE_PCRE16
  5664. return (errorcode <= PCRE_UTF16_ERR1 && md->partial > 1)?
  5665. PCRE_ERROR_SHORTUTF16 : PCRE_ERROR_BADUTF16;
  5666. #elif defined COMPILE_PCRE32
  5667. return PCRE_ERROR_BADUTF32;
  5668. #endif
  5669. }
  5670. #if defined COMPILE_PCRE8 || defined COMPILE_PCRE16
  5671. /* Check that a start_offset points to the start of a UTF character. */
  5672. if (start_offset > 0 && start_offset < length &&
  5673. NOT_FIRSTCHAR(((PCRE_PUCHAR)subject)[start_offset]))
  5674. return PCRE_ERROR_BADUTF8_OFFSET;
  5675. #endif
  5676. }
  5677. #endif
  5678. /* If the pattern was successfully studied with JIT support, run the JIT
  5679. executable instead of the rest of this function. Most options must be set at
  5680. compile time for the JIT code to be usable. Fallback to the normal code path if
  5681. an unsupported flag is set. */
  5682. #ifdef SUPPORT_JIT
  5683. if (extra_data != NULL
  5684. && (extra_data->flags & (PCRE_EXTRA_EXECUTABLE_JIT |
  5685. PCRE_EXTRA_TABLES)) == PCRE_EXTRA_EXECUTABLE_JIT
  5686. && extra_data->executable_jit != NULL
  5687. && (options & ~PUBLIC_JIT_EXEC_OPTIONS) == 0)
  5688. {
  5689. rc = PRIV(jit_exec)(extra_data, (const pcre_uchar *)subject, length,
  5690. start_offset, options, offsets, offsetcount);
  5691. /* PCRE_ERROR_NULL means that the selected normal or partial matching
  5692. mode is not compiled. In this case we simply fallback to interpreter. */
  5693. if (rc != PCRE_ERROR_JIT_BADOPTION) return rc;
  5694. }
  5695. #endif
  5696. /* Carry on with non-JIT matching. This information is for finding all the
  5697. numbers associated with a given name, for condition testing. */
  5698. md->name_table = (pcre_uchar *)re + re->name_table_offset;
  5699. md->name_count = re->name_count;
  5700. md->name_entry_size = re->name_entry_size;
  5701. /* Fish out the optional data from the extra_data structure, first setting
  5702. the default values. */
  5703. study = NULL;
  5704. md->match_limit = MATCH_LIMIT;
  5705. md->match_limit_recursion = MATCH_LIMIT_RECURSION;
  5706. md->callout_data = NULL;
  5707. /* The table pointer is always in native byte order. */
  5708. tables = re->tables;
  5709. /* The two limit values override the defaults, whatever their value. */
  5710. if (extra_data != NULL)
  5711. {
  5712. unsigned long int flags = extra_data->flags;
  5713. if ((flags & PCRE_EXTRA_STUDY_DATA) != 0)
  5714. study = (const pcre_study_data *)extra_data->study_data;
  5715. if ((flags & PCRE_EXTRA_MATCH_LIMIT) != 0)
  5716. md->match_limit = extra_data->match_limit;
  5717. if ((flags & PCRE_EXTRA_MATCH_LIMIT_RECURSION) != 0)
  5718. md->match_limit_recursion = extra_data->match_limit_recursion;
  5719. if ((flags & PCRE_EXTRA_CALLOUT_DATA) != 0)
  5720. md->callout_data = extra_data->callout_data;
  5721. if ((flags & PCRE_EXTRA_TABLES) != 0) tables = extra_data->tables;
  5722. }
  5723. /* Limits in the regex override only if they are smaller. */
  5724. if ((re->flags & PCRE_MLSET) != 0 && re->limit_match < md->match_limit)
  5725. md->match_limit = re->limit_match;
  5726. if ((re->flags & PCRE_RLSET) != 0 &&
  5727. re->limit_recursion < md->match_limit_recursion)
  5728. md->match_limit_recursion = re->limit_recursion;
  5729. /* If the exec call supplied NULL for tables, use the inbuilt ones. This
  5730. is a feature that makes it possible to save compiled regex and re-use them
  5731. in other programs later. */
  5732. if (tables == NULL) tables = PRIV(default_tables);
  5733. /* Set up other data */
  5734. anchored = ((re->options | options) & PCRE_ANCHORED) != 0;
  5735. startline = (re->flags & PCRE_STARTLINE) != 0;
  5736. firstline = (re->options & PCRE_FIRSTLINE) != 0;
  5737. /* The code starts after the real_pcre block and the capture name table. */
  5738. md->start_code = (const pcre_uchar *)re + re->name_table_offset +
  5739. re->name_count * re->name_entry_size;
  5740. md->start_subject = (PCRE_PUCHAR)subject;
  5741. md->start_offset = start_offset;
  5742. md->end_subject = md->start_subject + length;
  5743. end_subject = md->end_subject;
  5744. md->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;
  5745. md->use_ucp = (re->options & PCRE_UCP) != 0;
  5746. md->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;
  5747. md->ignore_skip_arg = 0;
  5748. /* Some options are unpacked into BOOL variables in the hope that testing
  5749. them will be faster than individual option bits. */
  5750. md->notbol = (options & PCRE_NOTBOL) != 0;
  5751. md->noteol = (options & PCRE_NOTEOL) != 0;
  5752. md->notempty = (options & PCRE_NOTEMPTY) != 0;
  5753. md->notempty_atstart = (options & PCRE_NOTEMPTY_ATSTART) != 0;
  5754. md->hitend = FALSE;
  5755. md->mark = md->nomatch_mark = NULL; /* In case never set */
  5756. md->recursive = NULL; /* No recursion at top level */
  5757. md->hasthen = (re->flags & PCRE_HASTHEN) != 0;
  5758. md->lcc = tables + lcc_offset;
  5759. md->fcc = tables + fcc_offset;
  5760. md->ctypes = tables + ctypes_offset;
  5761. /* Handle different \R options. */
  5762. switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
  5763. {
  5764. case 0:
  5765. if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0)
  5766. md->bsr_anycrlf = (re->options & PCRE_BSR_ANYCRLF) != 0;
  5767. else
  5768. #ifdef BSR_ANYCRLF
  5769. md->bsr_anycrlf = TRUE;
  5770. #else
  5771. md->bsr_anycrlf = FALSE;
  5772. #endif
  5773. break;
  5774. case PCRE_BSR_ANYCRLF:
  5775. md->bsr_anycrlf = TRUE;
  5776. break;
  5777. case PCRE_BSR_UNICODE:
  5778. md->bsr_anycrlf = FALSE;
  5779. break;
  5780. default: return PCRE_ERROR_BADNEWLINE;
  5781. }
  5782. /* Handle different types of newline. The three bits give eight cases. If
  5783. nothing is set at run time, whatever was used at compile time applies. */
  5784. switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options :
  5785. (pcre_uint32)options) & PCRE_NEWLINE_BITS)
  5786. {
  5787. case 0: newline = NEWLINE; break; /* Compile-time default */
  5788. case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
  5789. case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
  5790. case PCRE_NEWLINE_CR+
  5791. PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
  5792. case PCRE_NEWLINE_ANY: newline = -1; break;
  5793. case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
  5794. default: return PCRE_ERROR_BADNEWLINE;
  5795. }
  5796. if (newline == -2)
  5797. {
  5798. md->nltype = NLTYPE_ANYCRLF;
  5799. }
  5800. else if (newline < 0)
  5801. {
  5802. md->nltype = NLTYPE_ANY;
  5803. }
  5804. else
  5805. {
  5806. md->nltype = NLTYPE_FIXED;
  5807. if (newline > 255)
  5808. {
  5809. md->nllen = 2;
  5810. md->nl[0] = (newline >> 8) & 255;
  5811. md->nl[1] = newline & 255;
  5812. }
  5813. else
  5814. {
  5815. md->nllen = 1;
  5816. md->nl[0] = newline;
  5817. }
  5818. }
  5819. /* Partial matching was originally supported only for a restricted set of
  5820. regexes; from release 8.00 there are no restrictions, but the bits are still
  5821. defined (though never set). So there's no harm in leaving this code. */
  5822. if (md->partial && (re->flags & PCRE_NOPARTIAL) != 0)
  5823. return PCRE_ERROR_BADPARTIAL;
  5824. /* If the expression has got more back references than the offsets supplied can
  5825. hold, we get a temporary chunk of working store to use during the matching.
  5826. Otherwise, we can use the vector supplied, rounding down its size to a multiple
  5827. of 3. */
  5828. ocount = offsetcount - (offsetcount % 3);
  5829. arg_offset_max = (2*ocount)/3;
  5830. if (re->top_backref > 0 && re->top_backref >= ocount/3)
  5831. {
  5832. ocount = re->top_backref * 3 + 3;
  5833. md->offset_vector = (int *)(PUBL(malloc))(ocount * sizeof(int));
  5834. if (md->offset_vector == NULL) return PCRE_ERROR_NOMEMORY;
  5835. using_temporary_offsets = TRUE;
  5836. DPRINTF(("Got memory to hold back references\n"));
  5837. }
  5838. else md->offset_vector = offsets;
  5839. md->offset_end = ocount;
  5840. md->offset_max = (2*ocount)/3;
  5841. md->capture_last = 0;
  5842. /* Reset the working variable associated with each extraction. These should
  5843. never be used unless previously set, but they get saved and restored, and so we
  5844. initialize them to avoid reading uninitialized locations. Also, unset the
  5845. offsets for the matched string. This is really just for tidiness with callouts,
  5846. in case they inspect these fields. */
  5847. if (md->offset_vector != NULL)
  5848. {
  5849. register int *iptr = md->offset_vector + ocount;
  5850. register int *iend = iptr - re->top_bracket;
  5851. if (iend < md->offset_vector + 2) iend = md->offset_vector + 2;
  5852. while (--iptr >= iend) *iptr = -1;
  5853. if (offsetcount > 0) md->offset_vector[0] = -1;
  5854. if (offsetcount > 1) md->offset_vector[1] = -1;
  5855. }
  5856. /* Set up the first character to match, if available. The first_char value is
  5857. never set for an anchored regular expression, but the anchoring may be forced
  5858. at run time, so we have to test for anchoring. The first char may be unset for
  5859. an unanchored pattern, of course. If there's no first char and the pattern was
  5860. studied, there may be a bitmap of possible first characters. */
  5861. if (!anchored)
  5862. {
  5863. if ((re->flags & PCRE_FIRSTSET) != 0)
  5864. {
  5865. has_first_char = TRUE;
  5866. first_char = first_char2 = (pcre_uchar)(re->first_char);
  5867. if ((re->flags & PCRE_FCH_CASELESS) != 0)
  5868. {
  5869. first_char2 = TABLE_GET(first_char, md->fcc, first_char);
  5870. #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
  5871. if (utf && first_char > 127)
  5872. first_char2 = UCD_OTHERCASE(first_char);
  5873. #endif
  5874. }
  5875. }
  5876. else
  5877. if (!startline && study != NULL &&
  5878. (study->flags & PCRE_STUDY_MAPPED) != 0)
  5879. start_bits = study->start_bits;
  5880. }
  5881. /* For anchored or unanchored matches, there may be a "last known required
  5882. character" set. */
  5883. if ((re->flags & PCRE_REQCHSET) != 0)
  5884. {
  5885. has_req_char = TRUE;
  5886. req_char = req_char2 = (pcre_uchar)(re->req_char);
  5887. if ((re->flags & PCRE_RCH_CASELESS) != 0)
  5888. {
  5889. req_char2 = TABLE_GET(req_char, md->fcc, req_char);
  5890. #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
  5891. if (utf && req_char > 127)
  5892. req_char2 = UCD_OTHERCASE(req_char);
  5893. #endif
  5894. }
  5895. }
  5896. /* ==========================================================================*/
  5897. /* Loop for handling unanchored repeated matching attempts; for anchored regexs
  5898. the loop runs just once. */
  5899. for(;;)
  5900. {
  5901. PCRE_PUCHAR save_end_subject = end_subject;
  5902. PCRE_PUCHAR new_start_match;
  5903. /* If firstline is TRUE, the start of the match is constrained to the first
  5904. line of a multiline string. That is, the match must be before or at the first
  5905. newline. Implement this by temporarily adjusting end_subject so that we stop
  5906. scanning at a newline. If the match fails at the newline, later code breaks
  5907. this loop. */
  5908. if (firstline)
  5909. {
  5910. PCRE_PUCHAR t = start_match;
  5911. #ifdef SUPPORT_UTF
  5912. if (utf)
  5913. {
  5914. while (t < md->end_subject && !IS_NEWLINE(t))
  5915. {
  5916. t++;
  5917. ACROSSCHAR(t < end_subject, *t, t++);
  5918. }
  5919. }
  5920. else
  5921. #endif
  5922. while (t < md->end_subject && !IS_NEWLINE(t)) t++;
  5923. end_subject = t;
  5924. }
  5925. /* There are some optimizations that avoid running the match if a known
  5926. starting point is not found, or if a known later character is not present.
  5927. However, there is an option that disables these, for testing and for ensuring
  5928. that all callouts do actually occur. The option can be set in the regex by
  5929. (*NO_START_OPT) or passed in match-time options. */
  5930. if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0)
  5931. {
  5932. /* Advance to a unique first char if there is one. */
  5933. if (has_first_char)
  5934. {
  5935. pcre_uchar smc;
  5936. if (first_char != first_char2)
  5937. while (start_match < end_subject &&
  5938. (smc = UCHAR21TEST(start_match)) != first_char && smc != first_char2)
  5939. start_match++;
  5940. else
  5941. while (start_match < end_subject && UCHAR21TEST(start_match) != first_char)
  5942. start_match++;
  5943. }
  5944. /* Or to just after a linebreak for a multiline match */
  5945. else if (startline)
  5946. {
  5947. if (start_match > md->start_subject + start_offset)
  5948. {
  5949. #ifdef SUPPORT_UTF
  5950. if (utf)
  5951. {
  5952. while (start_match < end_subject && !WAS_NEWLINE(start_match))
  5953. {
  5954. start_match++;
  5955. ACROSSCHAR(start_match < end_subject, *start_match,
  5956. start_match++);
  5957. }
  5958. }
  5959. else
  5960. #endif
  5961. while (start_match < end_subject && !WAS_NEWLINE(start_match))
  5962. start_match++;
  5963. /* If we have just passed a CR and the newline option is ANY or ANYCRLF,
  5964. and we are now at a LF, advance the match position by one more character.
  5965. */
  5966. if (start_match[-1] == CHAR_CR &&
  5967. (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&
  5968. start_match < end_subject &&
  5969. UCHAR21TEST(start_match) == CHAR_NL)
  5970. start_match++;
  5971. }
  5972. }
  5973. /* Or to a non-unique first byte after study */
  5974. else if (start_bits != NULL)
  5975. {
  5976. while (start_match < end_subject)
  5977. {
  5978. register pcre_uint32 c = UCHAR21TEST(start_match);
  5979. #ifndef COMPILE_PCRE8
  5980. if (c > 255) c = 255;
  5981. #endif
  5982. if ((start_bits[c/8] & (1 << (c&7))) != 0) break;
  5983. start_match++;
  5984. }
  5985. }
  5986. } /* Starting optimizations */
  5987. /* Restore fudged end_subject */
  5988. end_subject = save_end_subject;
  5989. /* The following two optimizations are disabled for partial matching or if
  5990. disabling is explicitly requested. */
  5991. if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0 && !md->partial)
  5992. {
  5993. /* If the pattern was studied, a minimum subject length may be set. This is
  5994. a lower bound; no actual string of that length may actually match the
  5995. pattern. Although the value is, strictly, in characters, we treat it as
  5996. bytes to avoid spending too much time in this optimization. */
  5997. if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 &&
  5998. (pcre_uint32)(end_subject - start_match) < study->minlength)
  5999. {
  6000. rc = MATCH_NOMATCH;
  6001. break;
  6002. }
  6003. /* If req_char is set, we know that that character must appear in the
  6004. subject for the match to succeed. If the first character is set, req_char
  6005. must be later in the subject; otherwise the test starts at the match point.
  6006. This optimization can save a huge amount of backtracking in patterns with
  6007. nested unlimited repeats that aren't going to match. Writing separate code
  6008. for cased/caseless versions makes it go faster, as does using an
  6009. autoincrement and backing off on a match.
  6010. HOWEVER: when the subject string is very, very long, searching to its end
  6011. can take a long time, and give bad performance on quite ordinary patterns.
  6012. This showed up when somebody was matching something like /^\d+C/ on a
  6013. 32-megabyte string... so we don't do this when the string is sufficiently
  6014. long. */
  6015. if (has_req_char && end_subject - start_match < REQ_BYTE_MAX)
  6016. {
  6017. register PCRE_PUCHAR p = start_match + (has_first_char? 1:0);
  6018. /* We don't need to repeat the search if we haven't yet reached the
  6019. place we found it at last time. */
  6020. if (p > req_char_ptr)
  6021. {
  6022. if (req_char != req_char2)
  6023. {
  6024. while (p < end_subject)
  6025. {
  6026. register pcre_uint32 pp = UCHAR21INCTEST(p);
  6027. if (pp == req_char || pp == req_char2) { p--; break; }
  6028. }
  6029. }
  6030. else
  6031. {
  6032. while (p < end_subject)
  6033. {
  6034. if (UCHAR21INCTEST(p) == req_char) { p--; break; }
  6035. }
  6036. }
  6037. /* If we can't find the required character, break the matching loop,
  6038. forcing a match failure. */
  6039. if (p >= end_subject)
  6040. {
  6041. rc = MATCH_NOMATCH;
  6042. break;
  6043. }
  6044. /* If we have found the required character, save the point where we
  6045. found it, so that we don't search again next time round the loop if
  6046. the start hasn't passed this character yet. */
  6047. req_char_ptr = p;
  6048. }
  6049. }
  6050. }
  6051. #ifdef PCRE_DEBUG /* Sigh. Some compilers never learn. */
  6052. printf(">>>> Match against: ");
  6053. pchars(start_match, end_subject - start_match, TRUE, md);
  6054. printf("\n");
  6055. #endif
  6056. /* OK, we can now run the match. If "hitend" is set afterwards, remember the
  6057. first starting point for which a partial match was found. */
  6058. md->start_match_ptr = start_match;
  6059. md->start_used_ptr = start_match;
  6060. md->match_call_count = 0;
  6061. md->match_function_type = 0;
  6062. md->end_offset_top = 0;
  6063. md->skip_arg_count = 0;
  6064. rc = match(start_match, md->start_code, start_match, 2, md, NULL, 0);
  6065. if (md->hitend && start_partial == NULL)
  6066. {
  6067. start_partial = md->start_used_ptr;
  6068. match_partial = start_match;
  6069. }
  6070. switch(rc)
  6071. {
  6072. /* If MATCH_SKIP_ARG reaches this level it means that a MARK that matched
  6073. the SKIP's arg was not found. In this circumstance, Perl ignores the SKIP
  6074. entirely. The only way we can do that is to re-do the match at the same
  6075. point, with a flag to force SKIP with an argument to be ignored. Just
  6076. treating this case as NOMATCH does not work because it does not check other
  6077. alternatives in patterns such as A(*SKIP:A)B|AC when the subject is AC. */
  6078. case MATCH_SKIP_ARG:
  6079. new_start_match = start_match;
  6080. md->ignore_skip_arg = md->skip_arg_count;
  6081. break;
  6082. /* SKIP passes back the next starting point explicitly, but if it is no
  6083. greater than the match we have just done, treat it as NOMATCH. */
  6084. case MATCH_SKIP:
  6085. if (md->start_match_ptr > start_match)
  6086. {
  6087. new_start_match = md->start_match_ptr;
  6088. break;
  6089. }
  6090. /* Fall through */
  6091. /* NOMATCH and PRUNE advance by one character. THEN at this level acts
  6092. exactly like PRUNE. Unset ignore SKIP-with-argument. */
  6093. case MATCH_NOMATCH:
  6094. case MATCH_PRUNE:
  6095. case MATCH_THEN:
  6096. md->ignore_skip_arg = 0;
  6097. new_start_match = start_match + 1;
  6098. #ifdef SUPPORT_UTF
  6099. if (utf)
  6100. ACROSSCHAR(new_start_match < end_subject, *new_start_match,
  6101. new_start_match++);
  6102. #endif
  6103. break;
  6104. /* COMMIT disables the bumpalong, but otherwise behaves as NOMATCH. */
  6105. case MATCH_COMMIT:
  6106. rc = MATCH_NOMATCH;
  6107. goto ENDLOOP;
  6108. /* Any other return is either a match, or some kind of error. */
  6109. default:
  6110. goto ENDLOOP;
  6111. }
  6112. /* Control reaches here for the various types of "no match at this point"
  6113. result. Reset the code to MATCH_NOMATCH for subsequent checking. */
  6114. rc = MATCH_NOMATCH;
  6115. /* If PCRE_FIRSTLINE is set, the match must happen before or at the first
  6116. newline in the subject (though it may continue over the newline). Therefore,
  6117. if we have just failed to match, starting at a newline, do not continue. */
  6118. if (firstline && IS_NEWLINE(start_match)) break;
  6119. /* Advance to new matching position */
  6120. start_match = new_start_match;
  6121. /* Break the loop if the pattern is anchored or if we have passed the end of
  6122. the subject. */
  6123. if (anchored || start_match > end_subject) break;
  6124. /* If we have just passed a CR and we are now at a LF, and the pattern does
  6125. not contain any explicit matches for \r or \n, and the newline option is CRLF
  6126. or ANY or ANYCRLF, advance the match position by one more character. In
  6127. normal matching start_match will aways be greater than the first position at
  6128. this stage, but a failed *SKIP can cause a return at the same point, which is
  6129. why the first test exists. */
  6130. if (start_match > (PCRE_PUCHAR)subject + start_offset &&
  6131. start_match[-1] == CHAR_CR &&
  6132. start_match < end_subject &&
  6133. *start_match == CHAR_NL &&
  6134. (re->flags & PCRE_HASCRORLF) == 0 &&
  6135. (md->nltype == NLTYPE_ANY ||
  6136. md->nltype == NLTYPE_ANYCRLF ||
  6137. md->nllen == 2))
  6138. start_match++;
  6139. md->mark = NULL; /* Reset for start of next match attempt */
  6140. } /* End of for(;;) "bumpalong" loop */
  6141. /* ==========================================================================*/
  6142. /* We reach here when rc is not MATCH_NOMATCH, or if one of the stopping
  6143. conditions is true:
  6144. (1) The pattern is anchored or the match was failed by (*COMMIT);
  6145. (2) We are past the end of the subject;
  6146. (3) PCRE_FIRSTLINE is set and we have failed to match at a newline, because
  6147. this option requests that a match occur at or before the first newline in
  6148. the subject.
  6149. When we have a match and the offset vector is big enough to deal with any
  6150. backreferences, captured substring offsets will already be set up. In the case
  6151. where we had to get some local store to hold offsets for backreference
  6152. processing, copy those that we can. In this case there need not be overflow if
  6153. certain parts of the pattern were not used, even though there are more
  6154. capturing parentheses than vector slots. */
  6155. ENDLOOP:
  6156. if (rc == MATCH_MATCH || rc == MATCH_ACCEPT)
  6157. {
  6158. if (using_temporary_offsets)
  6159. {
  6160. if (arg_offset_max >= 4)
  6161. {
  6162. memcpy(offsets + 2, md->offset_vector + 2,
  6163. (arg_offset_max - 2) * sizeof(int));
  6164. DPRINTF(("Copied offsets from temporary memory\n"));
  6165. }
  6166. if (md->end_offset_top > arg_offset_max) md->capture_last |= OVFLBIT;
  6167. DPRINTF(("Freeing temporary memory\n"));
  6168. (PUBL(free))(md->offset_vector);
  6169. }
  6170. /* Set the return code to the number of captured strings, or 0 if there were
  6171. too many to fit into the vector. */
  6172. rc = ((md->capture_last & OVFLBIT) != 0 &&
  6173. md->end_offset_top >= arg_offset_max)?
  6174. 0 : md->end_offset_top/2;
  6175. /* If there is space in the offset vector, set any unused pairs at the end of
  6176. the pattern to -1 for backwards compatibility. It is documented that this
  6177. happens. In earlier versions, the whole set of potential capturing offsets
  6178. was set to -1 each time round the loop, but this is handled differently now.
  6179. "Gaps" are set to -1 dynamically instead (this fixes a bug). Thus, it is only
  6180. those at the end that need unsetting here. We can't just unset them all at
  6181. the start of the whole thing because they may get set in one branch that is
  6182. not the final matching branch. */
  6183. if (md->end_offset_top/2 <= re->top_bracket && offsets != NULL)
  6184. {
  6185. register int *iptr, *iend;
  6186. int resetcount = 2 + re->top_bracket * 2;
  6187. if (resetcount > offsetcount) resetcount = offsetcount;
  6188. iptr = offsets + md->end_offset_top;
  6189. iend = offsets + resetcount;
  6190. while (iptr < iend) *iptr++ = -1;
  6191. }
  6192. /* If there is space, set up the whole thing as substring 0. The value of
  6193. md->start_match_ptr might be modified if \K was encountered on the success
  6194. matching path. */
  6195. if (offsetcount < 2) rc = 0; else
  6196. {
  6197. offsets[0] = (int)(md->start_match_ptr - md->start_subject);
  6198. offsets[1] = (int)(md->end_match_ptr - md->start_subject);
  6199. }
  6200. /* Return MARK data if requested */
  6201. if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_MARK) != 0)
  6202. *(extra_data->mark) = (pcre_uchar *)md->mark;
  6203. DPRINTF((">>>> returning %d\n", rc));
  6204. #ifdef NO_RECURSE
  6205. release_match_heapframes(&frame_zero);
  6206. #endif
  6207. return rc;
  6208. }
  6209. /* Control gets here if there has been an error, or if the overall match
  6210. attempt has failed at all permitted starting positions. */
  6211. if (using_temporary_offsets)
  6212. {
  6213. DPRINTF(("Freeing temporary memory\n"));
  6214. (PUBL(free))(md->offset_vector);
  6215. }
  6216. /* For anything other than nomatch or partial match, just return the code. */
  6217. if (rc != MATCH_NOMATCH && rc != PCRE_ERROR_PARTIAL)
  6218. {
  6219. DPRINTF((">>>> error: returning %d\n", rc));
  6220. #ifdef NO_RECURSE
  6221. release_match_heapframes(&frame_zero);
  6222. #endif
  6223. return rc;
  6224. }
  6225. /* Handle partial matches - disable any mark data */
  6226. if (match_partial != NULL)
  6227. {
  6228. DPRINTF((">>>> returning PCRE_ERROR_PARTIAL\n"));
  6229. md->mark = NULL;
  6230. if (offsetcount > 1)
  6231. {
  6232. offsets[0] = (int)(start_partial - (PCRE_PUCHAR)subject);
  6233. offsets[1] = (int)(end_subject - (PCRE_PUCHAR)subject);
  6234. if (offsetcount > 2)
  6235. offsets[2] = (int)(match_partial - (PCRE_PUCHAR)subject);
  6236. }
  6237. rc = PCRE_ERROR_PARTIAL;
  6238. }
  6239. /* This is the classic nomatch case */
  6240. else
  6241. {
  6242. DPRINTF((">>>> returning PCRE_ERROR_NOMATCH\n"));
  6243. rc = PCRE_ERROR_NOMATCH;
  6244. }
  6245. /* Return the MARK data if it has been requested. */
  6246. if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_MARK) != 0)
  6247. *(extra_data->mark) = (pcre_uchar *)md->nomatch_mark;
  6248. #ifdef NO_RECURSE
  6249. release_match_heapframes(&frame_zero);
  6250. #endif
  6251. return rc;
  6252. }
  6253. /* End of pcre_exec.c */