pcre_exec.c 213 KB

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