pcre_compile.c 315 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810
  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 the external function pcre_compile(), along with
  33. supporting internal functions that are not used by other modules. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "pcre_config.h"
  36. #endif
  37. #define NLBLOCK cd /* Block containing newline information */
  38. #define PSSTART start_pattern /* Field containing pattern start */
  39. #define PSEND end_pattern /* Field containing pattern end */
  40. #include "pcre_internal.h"
  41. /* When PCRE_DEBUG is defined, we need the pcre(16|32)_printint() function, which
  42. is also used by pcretest. PCRE_DEBUG is not defined when building a production
  43. library. We do not need to select pcre16_printint.c specially, because the
  44. COMPILE_PCREx macro will already be appropriately set. */
  45. #ifdef PCRE_DEBUG
  46. /* pcre_printint.c should not include any headers */
  47. #define PCRE_INCLUDED
  48. #include "pcre_printint.c"
  49. #undef PCRE_INCLUDED
  50. #endif
  51. /* Macro for setting individual bits in class bitmaps. */
  52. #define SETBIT(a,b) a[(b)/8] |= (1U << ((b)&7))
  53. /* Maximum length value to check against when making sure that the integer that
  54. holds the compiled pattern length does not overflow. We make it a bit less than
  55. INT_MAX to allow for adding in group terminating bytes, so that we don't have
  56. to check them every time. */
  57. #define OFLOW_MAX (INT_MAX - 20)
  58. /* Definitions to allow mutual recursion */
  59. static int
  60. add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
  61. const pcre_uint32 *, unsigned int);
  62. static BOOL
  63. compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
  64. pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,
  65. compile_data *, int *);
  66. /*************************************************
  67. * Code parameters and static tables *
  68. *************************************************/
  69. /* This value specifies the size of stack workspace that is used during the
  70. first pre-compile phase that determines how much memory is required. The regex
  71. is partly compiled into this space, but the compiled parts are discarded as
  72. soon as they can be, so that hopefully there will never be an overrun. The code
  73. does, however, check for an overrun. The largest amount I've seen used is 218,
  74. so this number is very generous.
  75. The same workspace is used during the second, actual compile phase for
  76. remembering forward references to groups so that they can be filled in at the
  77. end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
  78. is 4 there is plenty of room for most patterns. However, the memory can get
  79. filled up by repetitions of forward references, for example patterns like
  80. /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
  81. that the workspace is expanded using malloc() in this situation. The value
  82. below is therefore a minimum, and we put a maximum on it for safety. The
  83. minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
  84. kicks in at the same number of forward references in all cases. */
  85. #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
  86. #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
  87. /* This value determines the size of the initial vector that is used for
  88. remembering named groups during the pre-compile. It is allocated on the stack,
  89. but if it is too small, it is expanded using malloc(), in a similar way to the
  90. workspace. The value is the number of slots in the list. */
  91. #define NAMED_GROUP_LIST_SIZE 20
  92. /* The overrun tests check for a slightly smaller size so that they detect the
  93. overrun before it actually does run off the end of the data block. */
  94. #define WORK_SIZE_SAFETY_MARGIN (100)
  95. /* Private flags added to firstchar and reqchar. */
  96. #define REQ_CASELESS (1U << 0) /* Indicates caselessness */
  97. #define REQ_VARY (1U << 1) /* Reqchar followed non-literal item */
  98. /* Negative values for the firstchar and reqchar flags */
  99. #define REQ_UNSET (-2)
  100. #define REQ_NONE (-1)
  101. /* Repeated character flags. */
  102. #define UTF_LENGTH 0x10000000l /* The char contains its length. */
  103. /* Table for handling escaped characters in the range '0'-'z'. Positive returns
  104. are simple data values; negative values are for special things like \d and so
  105. on. Zero means further processing is needed (for things like \x), or the escape
  106. is invalid. */
  107. #ifndef EBCDIC
  108. /* This is the "normal" table for ASCII systems or for EBCDIC systems running
  109. in UTF-8 mode. */
  110. static const short int escapes[] = {
  111. 0, 0,
  112. 0, 0,
  113. 0, 0,
  114. 0, 0,
  115. 0, 0,
  116. CHAR_COLON, CHAR_SEMICOLON,
  117. CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN,
  118. CHAR_GREATER_THAN_SIGN, CHAR_QUESTION_MARK,
  119. CHAR_COMMERCIAL_AT, -ESC_A,
  120. -ESC_B, -ESC_C,
  121. -ESC_D, -ESC_E,
  122. 0, -ESC_G,
  123. -ESC_H, 0,
  124. 0, -ESC_K,
  125. 0, 0,
  126. -ESC_N, 0,
  127. -ESC_P, -ESC_Q,
  128. -ESC_R, -ESC_S,
  129. 0, 0,
  130. -ESC_V, -ESC_W,
  131. -ESC_X, 0,
  132. -ESC_Z, CHAR_LEFT_SQUARE_BRACKET,
  133. CHAR_BACKSLASH, CHAR_RIGHT_SQUARE_BRACKET,
  134. CHAR_CIRCUMFLEX_ACCENT, CHAR_UNDERSCORE,
  135. CHAR_GRAVE_ACCENT, ESC_a,
  136. -ESC_b, 0,
  137. -ESC_d, ESC_e,
  138. ESC_f, 0,
  139. -ESC_h, 0,
  140. 0, -ESC_k,
  141. 0, 0,
  142. ESC_n, 0,
  143. -ESC_p, 0,
  144. ESC_r, -ESC_s,
  145. ESC_tee, 0,
  146. -ESC_v, -ESC_w,
  147. 0, 0,
  148. -ESC_z
  149. };
  150. #else
  151. /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
  152. static const short int escapes[] = {
  153. /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
  154. /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
  155. /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
  156. /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
  157. /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
  158. /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
  159. /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
  160. /* 80 */ 0, ESC_a, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0,
  161. /* 88 */-ESC_h, 0, 0, '{', 0, 0, 0, 0,
  162. /* 90 */ 0, 0, -ESC_k, 0, 0, ESC_n, 0, -ESC_p,
  163. /* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0,
  164. /* A0 */ 0, '~', -ESC_s, ESC_tee, 0,-ESC_v, -ESC_w, 0,
  165. /* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0,
  166. /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  167. /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
  168. /* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G,
  169. /* C8 */-ESC_H, 0, 0, 0, 0, 0, 0, 0,
  170. /* D0 */ '}', 0, -ESC_K, 0, 0,-ESC_N, 0, -ESC_P,
  171. /* D8 */-ESC_Q,-ESC_R, 0, 0, 0, 0, 0, 0,
  172. /* E0 */ '\\', 0, -ESC_S, 0, 0,-ESC_V, -ESC_W, -ESC_X,
  173. /* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0,
  174. /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  175. /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
  176. };
  177. /* We also need a table of characters that may follow \c in an EBCDIC
  178. environment for characters 0-31. */
  179. static unsigned char ebcdic_escape_c[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
  180. #endif
  181. /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
  182. searched linearly. Put all the names into a single string, in order to reduce
  183. the number of relocations when a shared library is dynamically linked. The
  184. string is built from string macros so that it works in UTF-8 mode on EBCDIC
  185. platforms. */
  186. typedef struct verbitem {
  187. int len; /* Length of verb name */
  188. int op; /* Op when no arg, or -1 if arg mandatory */
  189. int op_arg; /* Op when arg present, or -1 if not allowed */
  190. } verbitem;
  191. static const char verbnames[] =
  192. "\0" /* Empty name is a shorthand for MARK */
  193. STRING_MARK0
  194. STRING_ACCEPT0
  195. STRING_COMMIT0
  196. STRING_F0
  197. STRING_FAIL0
  198. STRING_PRUNE0
  199. STRING_SKIP0
  200. STRING_THEN;
  201. static const verbitem verbs[] = {
  202. { 0, -1, OP_MARK },
  203. { 4, -1, OP_MARK },
  204. { 6, OP_ACCEPT, -1 },
  205. { 6, OP_COMMIT, -1 },
  206. { 1, OP_FAIL, -1 },
  207. { 4, OP_FAIL, -1 },
  208. { 5, OP_PRUNE, OP_PRUNE_ARG },
  209. { 4, OP_SKIP, OP_SKIP_ARG },
  210. { 4, OP_THEN, OP_THEN_ARG }
  211. };
  212. static const int verbcount = sizeof(verbs)/sizeof(verbitem);
  213. /* Substitutes for [[:<:]] and [[:>:]], which mean start and end of word in
  214. another regex library. */
  215. static const pcre_uchar sub_start_of_word[] = {
  216. CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
  217. CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w, CHAR_RIGHT_PARENTHESIS, '\0' };
  218. static const pcre_uchar sub_end_of_word[] = {
  219. CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
  220. CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w,
  221. CHAR_RIGHT_PARENTHESIS, '\0' };
  222. /* Tables of names of POSIX character classes and their lengths. The names are
  223. now all in a single string, to reduce the number of relocations when a shared
  224. library is dynamically loaded. The list of lengths is terminated by a zero
  225. length entry. The first three must be alpha, lower, upper, as this is assumed
  226. for handling case independence. The indices for graph, print, and punct are
  227. needed, so identify them. */
  228. static const char posix_names[] =
  229. STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
  230. STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
  231. STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
  232. STRING_word0 STRING_xdigit;
  233. static const pcre_uint8 posix_name_lengths[] = {
  234. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
  235. #define PC_GRAPH 8
  236. #define PC_PRINT 9
  237. #define PC_PUNCT 10
  238. /* Table of class bit maps for each POSIX class. Each class is formed from a
  239. base map, with an optional addition or removal of another map. Then, for some
  240. classes, there is some additional tweaking: for [:blank:] the vertical space
  241. characters are removed, and for [:alpha:] and [:alnum:] the underscore
  242. character is removed. The triples in the table consist of the base map offset,
  243. second map offset or -1 if no second map, and a non-negative value for map
  244. addition or a negative value for map subtraction (if there are two maps). The
  245. absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
  246. remove vertical space characters, 2 => remove underscore. */
  247. static const int posix_class_maps[] = {
  248. cbit_word, cbit_digit, -2, /* alpha */
  249. cbit_lower, -1, 0, /* lower */
  250. cbit_upper, -1, 0, /* upper */
  251. cbit_word, -1, 2, /* alnum - word without underscore */
  252. cbit_print, cbit_cntrl, 0, /* ascii */
  253. cbit_space, -1, 1, /* blank - a GNU extension */
  254. cbit_cntrl, -1, 0, /* cntrl */
  255. cbit_digit, -1, 0, /* digit */
  256. cbit_graph, -1, 0, /* graph */
  257. cbit_print, -1, 0, /* print */
  258. cbit_punct, -1, 0, /* punct */
  259. cbit_space, -1, 0, /* space */
  260. cbit_word, -1, 0, /* word - a Perl extension */
  261. cbit_xdigit,-1, 0 /* xdigit */
  262. };
  263. /* Table of substitutes for \d etc when PCRE_UCP is set. They are replaced by
  264. Unicode property escapes. */
  265. #ifdef SUPPORT_UCP
  266. static const pcre_uchar string_PNd[] = {
  267. CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
  268. CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  269. static const pcre_uchar string_pNd[] = {
  270. CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
  271. CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  272. static const pcre_uchar string_PXsp[] = {
  273. CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
  274. CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  275. static const pcre_uchar string_pXsp[] = {
  276. CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
  277. CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  278. static const pcre_uchar string_PXwd[] = {
  279. CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
  280. CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  281. static const pcre_uchar string_pXwd[] = {
  282. CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
  283. CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  284. static const pcre_uchar *substitutes[] = {
  285. string_PNd, /* \D */
  286. string_pNd, /* \d */
  287. string_PXsp, /* \S */ /* Xsp is Perl space, but from 8.34, Perl */
  288. string_pXsp, /* \s */ /* space and POSIX space are the same. */
  289. string_PXwd, /* \W */
  290. string_pXwd /* \w */
  291. };
  292. /* The POSIX class substitutes must be in the order of the POSIX class names,
  293. defined above, and there are both positive and negative cases. NULL means no
  294. general substitute of a Unicode property escape (\p or \P). However, for some
  295. POSIX classes (e.g. graph, print, punct) a special property code is compiled
  296. directly. */
  297. static const pcre_uchar string_pL[] = {
  298. CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
  299. CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  300. static const pcre_uchar string_pLl[] = {
  301. CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
  302. CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  303. static const pcre_uchar string_pLu[] = {
  304. CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
  305. CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  306. static const pcre_uchar string_pXan[] = {
  307. CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
  308. CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  309. static const pcre_uchar string_h[] = {
  310. CHAR_BACKSLASH, CHAR_h, '\0' };
  311. static const pcre_uchar string_pXps[] = {
  312. CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
  313. CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  314. static const pcre_uchar string_PL[] = {
  315. CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
  316. CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  317. static const pcre_uchar string_PLl[] = {
  318. CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
  319. CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  320. static const pcre_uchar string_PLu[] = {
  321. CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
  322. CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  323. static const pcre_uchar string_PXan[] = {
  324. CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
  325. CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  326. static const pcre_uchar string_H[] = {
  327. CHAR_BACKSLASH, CHAR_H, '\0' };
  328. static const pcre_uchar string_PXps[] = {
  329. CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
  330. CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
  331. static const pcre_uchar *posix_substitutes[] = {
  332. string_pL, /* alpha */
  333. string_pLl, /* lower */
  334. string_pLu, /* upper */
  335. string_pXan, /* alnum */
  336. NULL, /* ascii */
  337. string_h, /* blank */
  338. NULL, /* cntrl */
  339. string_pNd, /* digit */
  340. NULL, /* graph */
  341. NULL, /* print */
  342. NULL, /* punct */
  343. string_pXps, /* space */ /* Xps is POSIX space, but from 8.34 */
  344. string_pXwd, /* word */ /* Perl and POSIX space are the same */
  345. NULL, /* xdigit */
  346. /* Negated cases */
  347. string_PL, /* ^alpha */
  348. string_PLl, /* ^lower */
  349. string_PLu, /* ^upper */
  350. string_PXan, /* ^alnum */
  351. NULL, /* ^ascii */
  352. string_H, /* ^blank */
  353. NULL, /* ^cntrl */
  354. string_PNd, /* ^digit */
  355. NULL, /* ^graph */
  356. NULL, /* ^print */
  357. NULL, /* ^punct */
  358. string_PXps, /* ^space */ /* Xps is POSIX space, but from 8.34 */
  359. string_PXwd, /* ^word */ /* Perl and POSIX space are the same */
  360. NULL /* ^xdigit */
  361. };
  362. #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
  363. #endif
  364. #define STRING(a) # a
  365. #define XSTRING(s) STRING(s)
  366. /* The texts of compile-time error messages. These are "char *" because they
  367. are passed to the outside world. Do not ever re-use any error number, because
  368. they are documented. Always add a new error instead. Messages marked DEAD below
  369. are no longer used. This used to be a table of strings, but in order to reduce
  370. the number of relocations needed when a shared library is loaded dynamically,
  371. it is now one long string. We cannot use a table of offsets, because the
  372. lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
  373. simply count through to the one we want - this isn't a performance issue
  374. because these strings are used only when there is a compilation error.
  375. Each substring ends with \0 to insert a null character. This includes the final
  376. substring, so that the whole string ends with \0\0, which can be detected when
  377. counting through. */
  378. static const char error_texts[] =
  379. "no error\0"
  380. "\\ at end of pattern\0"
  381. "\\c at end of pattern\0"
  382. "unrecognized character follows \\\0"
  383. "numbers out of order in {} quantifier\0"
  384. /* 5 */
  385. "number too big in {} quantifier\0"
  386. "missing terminating ] for character class\0"
  387. "invalid escape sequence in character class\0"
  388. "range out of order in character class\0"
  389. "nothing to repeat\0"
  390. /* 10 */
  391. "internal error: invalid forward reference offset\0"
  392. "internal error: unexpected repeat\0"
  393. "unrecognized character after (? or (?-\0"
  394. "POSIX named classes are supported only within a class\0"
  395. "missing )\0"
  396. /* 15 */
  397. "reference to non-existent subpattern\0"
  398. "erroffset passed as NULL\0"
  399. "unknown option bit(s) set\0"
  400. "missing ) after comment\0"
  401. "parentheses nested too deeply\0" /** DEAD **/
  402. /* 20 */
  403. "regular expression is too large\0"
  404. "failed to get memory\0"
  405. "unmatched parentheses\0"
  406. "internal error: code overflow\0"
  407. "unrecognized character after (?<\0"
  408. /* 25 */
  409. "lookbehind assertion is not fixed length\0"
  410. "malformed number or name after (?(\0"
  411. "conditional group contains more than two branches\0"
  412. "assertion expected after (?( or (?(?C)\0"
  413. "(?R or (?[+-]digits must be followed by )\0"
  414. /* 30 */
  415. "unknown POSIX class name\0"
  416. "POSIX collating elements are not supported\0"
  417. "this version of PCRE is compiled without UTF support\0"
  418. "spare error\0" /** DEAD **/
  419. "character value in \\x{} or \\o{} is too large\0"
  420. /* 35 */
  421. "invalid condition (?(0)\0"
  422. "\\C not allowed in lookbehind assertion\0"
  423. "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
  424. "number after (?C is > 255\0"
  425. "closing ) for (?C expected\0"
  426. /* 40 */
  427. "recursive call could loop indefinitely\0"
  428. "unrecognized character after (?P\0"
  429. "syntax error in subpattern name (missing terminator)\0"
  430. "two named subpatterns have the same name\0"
  431. "invalid UTF-8 string\0"
  432. /* 45 */
  433. "support for \\P, \\p, and \\X has not been compiled\0"
  434. "malformed \\P or \\p sequence\0"
  435. "unknown property name after \\P or \\p\0"
  436. "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
  437. "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
  438. /* 50 */
  439. "repeated subpattern is too long\0" /** DEAD **/
  440. "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
  441. "internal error: overran compiling workspace\0"
  442. "internal error: previously-checked referenced subpattern not found\0"
  443. "DEFINE group contains more than one branch\0"
  444. /* 55 */
  445. "repeating a DEFINE group is not allowed\0" /** DEAD **/
  446. "inconsistent NEWLINE options\0"
  447. "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
  448. "a numbered reference must not be zero\0"
  449. "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
  450. /* 60 */
  451. "(*VERB) not recognized or malformed\0"
  452. "number is too big\0"
  453. "subpattern name expected\0"
  454. "digit expected after (?+\0"
  455. "] is an invalid data character in JavaScript compatibility mode\0"
  456. /* 65 */
  457. "different names for subpatterns of the same number are not allowed\0"
  458. "(*MARK) must have an argument\0"
  459. "this version of PCRE is not compiled with Unicode property support\0"
  460. #ifndef EBCDIC
  461. "\\c must be followed by an ASCII character\0"
  462. #else
  463. "\\c must be followed by a letter or one of [\\]^_?\0"
  464. #endif
  465. "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
  466. /* 70 */
  467. "internal error: unknown opcode in find_fixedlength()\0"
  468. "\\N is not supported in a class\0"
  469. "too many forward references\0"
  470. "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
  471. "invalid UTF-16 string\0"
  472. /* 75 */
  473. "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
  474. "character value in \\u.... sequence is too large\0"
  475. "invalid UTF-32 string\0"
  476. "setting UTF is disabled by the application\0"
  477. "non-hex character in \\x{} (closing brace missing?)\0"
  478. /* 80 */
  479. "non-octal character in \\o{} (closing brace missing?)\0"
  480. "missing opening brace after \\o\0"
  481. "parentheses are too deeply nested\0"
  482. "invalid range in character class\0"
  483. "group name must start with a non-digit\0"
  484. /* 85 */
  485. "parentheses are too deeply nested (stack check)\0"
  486. "digits missing in \\x{} or \\o{}\0"
  487. "regular expression is too complicated\0"
  488. ;
  489. /* Table to identify digits and hex digits. This is used when compiling
  490. patterns. Note that the tables in chartables are dependent on the locale, and
  491. may mark arbitrary characters as digits - but the PCRE compiling code expects
  492. to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
  493. a private table here. It costs 256 bytes, but it is a lot faster than doing
  494. character value tests (at least in some simple cases I timed), and in some
  495. applications one wants PCRE to compile efficiently as well as match
  496. efficiently.
  497. For convenience, we use the same bit definitions as in chartables:
  498. 0x04 decimal digit
  499. 0x08 hexadecimal digit
  500. Then we can use ctype_digit and ctype_xdigit in the code. */
  501. /* Using a simple comparison for decimal numbers rather than a memory read
  502. is much faster, and the resulting code is simpler (the compiler turns it
  503. into a subtraction and unsigned comparison). */
  504. #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
  505. #ifndef EBCDIC
  506. /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
  507. UTF-8 mode. */
  508. static const pcre_uint8 digitab[] =
  509. {
  510. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
  511. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  512. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
  513. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  514. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
  515. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
  516. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
  517. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
  518. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
  519. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
  520. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
  521. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
  522. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
  523. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
  524. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
  525. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
  526. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
  527. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
  528. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
  529. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
  530. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
  531. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
  532. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
  533. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  534. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
  535. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
  536. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
  537. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
  538. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
  539. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
  540. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
  541. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
  542. #else
  543. /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
  544. static const pcre_uint8 digitab[] =
  545. {
  546. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
  547. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  548. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
  549. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  550. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
  551. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  552. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
  553. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  554. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
  555. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
  556. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
  557. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */
  558. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
  559. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
  560. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
  561. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  562. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
  563. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  564. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
  565. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  566. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
  567. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  568. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
  569. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  570. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
  571. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  572. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
  573. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  574. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
  575. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  576. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
  577. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  578. static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
  579. 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
  580. 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
  581. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
  582. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  583. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
  584. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  585. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
  586. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  587. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
  588. 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
  589. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
  590. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */
  591. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
  592. 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
  593. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
  594. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  595. 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
  596. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  597. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
  598. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  599. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
  600. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  601. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
  602. 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  603. 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
  604. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  605. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
  606. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  607. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
  608. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  609. 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
  610. 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  611. #endif
  612. /* This table is used to check whether auto-possessification is possible
  613. between adjacent character-type opcodes. The left-hand (repeated) opcode is
  614. used to select the row, and the right-hand opcode is use to select the column.
  615. A value of 1 means that auto-possessification is OK. For example, the second
  616. value in the first row means that \D+\d can be turned into \D++\d.
  617. The Unicode property types (\P and \p) have to be present to fill out the table
  618. because of what their opcode values are, but the table values should always be
  619. zero because property types are handled separately in the code. The last four
  620. columns apply to items that cannot be repeated, so there is no need to have
  621. rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
  622. *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
  623. #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
  624. #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
  625. static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
  626. /* \D \d \S \s \W \w . .+ \C \P \p \R \H \h \V \v \X \Z \z $ $M */
  627. { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \D */
  628. { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \d */
  629. { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \S */
  630. { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \s */
  631. { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \W */
  632. { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \w */
  633. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* . */
  634. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* .+ */
  635. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \C */
  636. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \P */
  637. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \p */
  638. { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \R */
  639. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \H */
  640. { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \h */
  641. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \V */
  642. { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 }, /* \v */
  643. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 } /* \X */
  644. };
  645. /* This table is used to check whether auto-possessification is possible
  646. between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
  647. left-hand (repeated) opcode is used to select the row, and the right-hand
  648. opcode is used to select the column. The values are as follows:
  649. 0 Always return FALSE (never auto-possessify)
  650. 1 Character groups are distinct (possessify if both are OP_PROP)
  651. 2 Check character categories in the same group (general or particular)
  652. 3 TRUE if the two opcodes are not the same (PROP vs NOTPROP)
  653. 4 Check left general category vs right particular category
  654. 5 Check right general category vs left particular category
  655. 6 Left alphanum vs right general category
  656. 7 Left space vs right general category
  657. 8 Left word vs right general category
  658. 9 Right alphanum vs left general category
  659. 10 Right space vs left general category
  660. 11 Right word vs left general category
  661. 12 Left alphanum vs right particular category
  662. 13 Left space vs right particular category
  663. 14 Left word vs right particular category
  664. 15 Right alphanum vs left particular category
  665. 16 Right space vs left particular category
  666. 17 Right word vs left particular category
  667. */
  668. static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
  669. /* ANY LAMP GC PC SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
  670. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_ANY */
  671. { 0, 3, 0, 0, 0, 3, 1, 1, 0, 0, 0 }, /* PT_LAMP */
  672. { 0, 0, 2, 4, 0, 9, 10, 10, 11, 0, 0 }, /* PT_GC */
  673. { 0, 0, 5, 2, 0, 15, 16, 16, 17, 0, 0 }, /* PT_PC */
  674. { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 }, /* PT_SC */
  675. { 0, 3, 6, 12, 0, 3, 1, 1, 0, 0, 0 }, /* PT_ALNUM */
  676. { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_SPACE */
  677. { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_PXSPACE */
  678. { 0, 0, 8, 14, 0, 0, 1, 1, 3, 0, 0 }, /* PT_WORD */
  679. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_CLIST */
  680. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 } /* PT_UCNC */
  681. };
  682. /* This table is used to check whether auto-possessification is possible
  683. between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
  684. specifies a general category and the other specifies a particular category. The
  685. row is selected by the general category and the column by the particular
  686. category. The value is 1 if the particular category is not part of the general
  687. category. */
  688. static const pcre_uint8 catposstab[7][30] = {
  689. /* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
  690. { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* C */
  691. { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* L */
  692. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* M */
  693. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* N */
  694. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, /* P */
  695. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 }, /* S */
  696. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 } /* Z */
  697. };
  698. /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
  699. a general or particular category. The properties in each row are those
  700. that apply to the character set in question. Duplication means that a little
  701. unnecessary work is done when checking, but this keeps things much simpler
  702. because they can all use the same code. For more details see the comment where
  703. this table is used.
  704. Note: SPACE and PXSPACE used to be different because Perl excluded VT from
  705. "space", but from Perl 5.18 it's included, so both categories are treated the
  706. same here. */
  707. static const pcre_uint8 posspropstab[3][4] = {
  708. { ucp_L, ucp_N, ucp_N, ucp_Nl }, /* ALNUM, 3rd and 4th values redundant */
  709. { ucp_Z, ucp_Z, ucp_C, ucp_Cc }, /* SPACE and PXSPACE, 2nd value redundant */
  710. { ucp_L, ucp_N, ucp_P, ucp_Po } /* WORD */
  711. };
  712. /* This table is used when converting repeating opcodes into possessified
  713. versions as a result of an explicit possessive quantifier such as ++. A zero
  714. value means there is no possessified version - in those cases the item in
  715. question must be wrapped in ONCE brackets. The table is truncated at OP_CALLOUT
  716. because all relevant opcodes are less than that. */
  717. static const pcre_uint8 opcode_possessify[] = {
  718. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 15 */
  719. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 - 31 */
  720. 0, /* NOTI */
  721. OP_POSSTAR, 0, /* STAR, MINSTAR */
  722. OP_POSPLUS, 0, /* PLUS, MINPLUS */
  723. OP_POSQUERY, 0, /* QUERY, MINQUERY */
  724. OP_POSUPTO, 0, /* UPTO, MINUPTO */
  725. 0, /* EXACT */
  726. 0, 0, 0, 0, /* POS{STAR,PLUS,QUERY,UPTO} */
  727. OP_POSSTARI, 0, /* STARI, MINSTARI */
  728. OP_POSPLUSI, 0, /* PLUSI, MINPLUSI */
  729. OP_POSQUERYI, 0, /* QUERYI, MINQUERYI */
  730. OP_POSUPTOI, 0, /* UPTOI, MINUPTOI */
  731. 0, /* EXACTI */
  732. 0, 0, 0, 0, /* POS{STARI,PLUSI,QUERYI,UPTOI} */
  733. OP_NOTPOSSTAR, 0, /* NOTSTAR, NOTMINSTAR */
  734. OP_NOTPOSPLUS, 0, /* NOTPLUS, NOTMINPLUS */
  735. OP_NOTPOSQUERY, 0, /* NOTQUERY, NOTMINQUERY */
  736. OP_NOTPOSUPTO, 0, /* NOTUPTO, NOTMINUPTO */
  737. 0, /* NOTEXACT */
  738. 0, 0, 0, 0, /* NOTPOS{STAR,PLUS,QUERY,UPTO} */
  739. OP_NOTPOSSTARI, 0, /* NOTSTARI, NOTMINSTARI */
  740. OP_NOTPOSPLUSI, 0, /* NOTPLUSI, NOTMINPLUSI */
  741. OP_NOTPOSQUERYI, 0, /* NOTQUERYI, NOTMINQUERYI */
  742. OP_NOTPOSUPTOI, 0, /* NOTUPTOI, NOTMINUPTOI */
  743. 0, /* NOTEXACTI */
  744. 0, 0, 0, 0, /* NOTPOS{STARI,PLUSI,QUERYI,UPTOI} */
  745. OP_TYPEPOSSTAR, 0, /* TYPESTAR, TYPEMINSTAR */
  746. OP_TYPEPOSPLUS, 0, /* TYPEPLUS, TYPEMINPLUS */
  747. OP_TYPEPOSQUERY, 0, /* TYPEQUERY, TYPEMINQUERY */
  748. OP_TYPEPOSUPTO, 0, /* TYPEUPTO, TYPEMINUPTO */
  749. 0, /* TYPEEXACT */
  750. 0, 0, 0, 0, /* TYPEPOS{STAR,PLUS,QUERY,UPTO} */
  751. OP_CRPOSSTAR, 0, /* CRSTAR, CRMINSTAR */
  752. OP_CRPOSPLUS, 0, /* CRPLUS, CRMINPLUS */
  753. OP_CRPOSQUERY, 0, /* CRQUERY, CRMINQUERY */
  754. OP_CRPOSRANGE, 0, /* CRRANGE, CRMINRANGE */
  755. 0, 0, 0, 0, /* CRPOS{STAR,PLUS,QUERY,RANGE} */
  756. 0, 0, 0, /* CLASS, NCLASS, XCLASS */
  757. 0, 0, /* REF, REFI */
  758. 0, 0, /* DNREF, DNREFI */
  759. 0, 0 /* RECURSE, CALLOUT */
  760. };
  761. /*************************************************
  762. * Find an error text *
  763. *************************************************/
  764. /* The error texts are now all in one long string, to save on relocations. As
  765. some of the text is of unknown length, we can't use a table of offsets.
  766. Instead, just count through the strings. This is not a performance issue
  767. because it happens only when there has been a compilation error.
  768. Argument: the error number
  769. Returns: pointer to the error string
  770. */
  771. static const char *
  772. find_error_text(int n)
  773. {
  774. const char *s = error_texts;
  775. for (; n > 0; n--)
  776. {
  777. while (*s++ != CHAR_NULL) {};
  778. if (*s == CHAR_NULL) return "Error text not found (please report)";
  779. }
  780. return s;
  781. }
  782. /*************************************************
  783. * Expand the workspace *
  784. *************************************************/
  785. /* This function is called during the second compiling phase, if the number of
  786. forward references fills the existing workspace, which is originally a block on
  787. the stack. A larger block is obtained from malloc() unless the ultimate limit
  788. has been reached or the increase will be rather small.
  789. Argument: pointer to the compile data block
  790. Returns: 0 if all went well, else an error number
  791. */
  792. static int
  793. expand_workspace(compile_data *cd)
  794. {
  795. pcre_uchar *newspace;
  796. int newsize = cd->workspace_size * 2;
  797. if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
  798. if (cd->workspace_size >= COMPILE_WORK_SIZE_MAX ||
  799. newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
  800. return ERR72;
  801. newspace = (PUBL(malloc))(IN_UCHARS(newsize));
  802. if (newspace == NULL) return ERR21;
  803. memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
  804. cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
  805. if (cd->workspace_size > COMPILE_WORK_SIZE)
  806. (PUBL(free))((void *)cd->start_workspace);
  807. cd->start_workspace = newspace;
  808. cd->workspace_size = newsize;
  809. return 0;
  810. }
  811. /*************************************************
  812. * Check for counted repeat *
  813. *************************************************/
  814. /* This function is called when a '{' is encountered in a place where it might
  815. start a quantifier. It looks ahead to see if it really is a quantifier or not.
  816. It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
  817. where the ddds are digits.
  818. Arguments:
  819. p pointer to the first char after '{'
  820. Returns: TRUE or FALSE
  821. */
  822. static BOOL
  823. is_counted_repeat(const pcre_uchar *p)
  824. {
  825. if (!IS_DIGIT(*p)) return FALSE;
  826. p++;
  827. while (IS_DIGIT(*p)) p++;
  828. if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
  829. if (*p++ != CHAR_COMMA) return FALSE;
  830. if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
  831. if (!IS_DIGIT(*p)) return FALSE;
  832. p++;
  833. while (IS_DIGIT(*p)) p++;
  834. return (*p == CHAR_RIGHT_CURLY_BRACKET);
  835. }
  836. /*************************************************
  837. * Handle escapes *
  838. *************************************************/
  839. /* This function is called when a \ has been encountered. It either returns a
  840. positive value for a simple escape such as \n, or 0 for a data character which
  841. will be placed in chptr. A backreference to group n is returned as negative n.
  842. When UTF-8 is enabled, a positive value greater than 255 may be returned in
  843. chptr. On entry, ptr is pointing at the \. On exit, it is on the final
  844. character of the escape sequence.
  845. Arguments:
  846. ptrptr points to the pattern position pointer
  847. chptr points to a returned data character
  848. errorcodeptr points to the errorcode variable
  849. bracount number of previous extracting brackets
  850. options the options bits
  851. isclass TRUE if inside a character class
  852. Returns: zero => a data character
  853. positive => a special escape sequence
  854. negative => a back reference
  855. on error, errorcodeptr is set
  856. */
  857. static int
  858. check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
  859. int bracount, int options, BOOL isclass)
  860. {
  861. /* PCRE_UTF16 has the same value as PCRE_UTF8. */
  862. BOOL utf = (options & PCRE_UTF8) != 0;
  863. const pcre_uchar *ptr = *ptrptr + 1;
  864. pcre_uint32 c;
  865. int escape = 0;
  866. int i;
  867. GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
  868. ptr--; /* Set pointer back to the last byte */
  869. /* If backslash is at the end of the pattern, it's an error. */
  870. if (c == CHAR_NULL) *errorcodeptr = ERR1;
  871. /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
  872. in a table. A non-zero result is something that can be returned immediately.
  873. Otherwise further processing may be required. */
  874. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  875. /* Not alphanumeric */
  876. else if (c < CHAR_0 || c > CHAR_z) {}
  877. else if ((i = escapes[c - CHAR_0]) != 0)
  878. { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
  879. #else /* EBCDIC coding */
  880. /* Not alphanumeric */
  881. else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
  882. else if ((i = escapes[c - 0x48]) != 0) { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
  883. #endif
  884. /* Escapes that need further processing, or are illegal. */
  885. else
  886. {
  887. const pcre_uchar *oldptr;
  888. BOOL braced, negated, overflow;
  889. int s;
  890. switch (c)
  891. {
  892. /* A number of Perl escapes are not handled by PCRE. We give an explicit
  893. error. */
  894. case CHAR_l:
  895. case CHAR_L:
  896. *errorcodeptr = ERR37;
  897. break;
  898. case CHAR_u:
  899. if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
  900. {
  901. /* In JavaScript, \u must be followed by four hexadecimal numbers.
  902. Otherwise it is a lowercase u letter. */
  903. if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
  904. && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
  905. && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
  906. && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
  907. {
  908. c = 0;
  909. for (i = 0; i < 4; ++i)
  910. {
  911. register pcre_uint32 cc = *(++ptr);
  912. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  913. if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
  914. c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
  915. #else /* EBCDIC coding */
  916. if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
  917. c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
  918. #endif
  919. }
  920. #if defined COMPILE_PCRE8
  921. if (c > (utf ? 0x10ffffU : 0xffU))
  922. #elif defined COMPILE_PCRE16
  923. if (c > (utf ? 0x10ffffU : 0xffffU))
  924. #elif defined COMPILE_PCRE32
  925. if (utf && c > 0x10ffffU)
  926. #endif
  927. {
  928. *errorcodeptr = ERR76;
  929. }
  930. else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
  931. }
  932. }
  933. else
  934. *errorcodeptr = ERR37;
  935. break;
  936. case CHAR_U:
  937. /* In JavaScript, \U is an uppercase U letter. */
  938. if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
  939. break;
  940. /* In a character class, \g is just a literal "g". Outside a character
  941. class, \g must be followed by one of a number of specific things:
  942. (1) A number, either plain or braced. If positive, it is an absolute
  943. backreference. If negative, it is a relative backreference. This is a Perl
  944. 5.10 feature.
  945. (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
  946. is part of Perl's movement towards a unified syntax for back references. As
  947. this is synonymous with \k{name}, we fudge it up by pretending it really
  948. was \k.
  949. (3) For Oniguruma compatibility we also support \g followed by a name or a
  950. number either in angle brackets or in single quotes. However, these are
  951. (possibly recursive) subroutine calls, _not_ backreferences. Just return
  952. the ESC_g code (cf \k). */
  953. case CHAR_g:
  954. if (isclass) break;
  955. if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
  956. {
  957. escape = ESC_g;
  958. break;
  959. }
  960. /* Handle the Perl-compatible cases */
  961. if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
  962. {
  963. const pcre_uchar *p;
  964. for (p = ptr+2; *p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
  965. if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
  966. if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
  967. {
  968. escape = ESC_k;
  969. break;
  970. }
  971. braced = TRUE;
  972. ptr++;
  973. }
  974. else braced = FALSE;
  975. if (ptr[1] == CHAR_MINUS)
  976. {
  977. negated = TRUE;
  978. ptr++;
  979. }
  980. else negated = FALSE;
  981. /* The integer range is limited by the machine's int representation. */
  982. s = 0;
  983. overflow = FALSE;
  984. while (IS_DIGIT(ptr[1]))
  985. {
  986. if (s > INT_MAX / 10 - 1) /* Integer overflow */
  987. {
  988. overflow = TRUE;
  989. break;
  990. }
  991. s = s * 10 + (int)(*(++ptr) - CHAR_0);
  992. }
  993. if (overflow) /* Integer overflow */
  994. {
  995. while (IS_DIGIT(ptr[1]))
  996. ptr++;
  997. *errorcodeptr = ERR61;
  998. break;
  999. }
  1000. if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET)
  1001. {
  1002. *errorcodeptr = ERR57;
  1003. break;
  1004. }
  1005. if (s == 0)
  1006. {
  1007. *errorcodeptr = ERR58;
  1008. break;
  1009. }
  1010. if (negated)
  1011. {
  1012. if (s > bracount)
  1013. {
  1014. *errorcodeptr = ERR15;
  1015. break;
  1016. }
  1017. s = bracount - (s - 1);
  1018. }
  1019. escape = -s;
  1020. break;
  1021. /* The handling of escape sequences consisting of a string of digits
  1022. starting with one that is not zero is not straightforward. Perl has changed
  1023. over the years. Nowadays \g{} for backreferences and \o{} for octal are
  1024. recommended to avoid the ambiguities in the old syntax.
  1025. Outside a character class, the digits are read as a decimal number. If the
  1026. number is less than 8 (used to be 10), or if there are that many previous
  1027. extracting left brackets, then it is a back reference. Otherwise, up to
  1028. three octal digits are read to form an escaped byte. Thus \123 is likely to
  1029. be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If
  1030. the octal value is greater than 377, the least significant 8 bits are
  1031. taken. \8 and \9 are treated as the literal characters 8 and 9.
  1032. Inside a character class, \ followed by a digit is always either a literal
  1033. 8 or 9 or an octal number. */
  1034. case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
  1035. case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
  1036. if (!isclass)
  1037. {
  1038. oldptr = ptr;
  1039. /* The integer range is limited by the machine's int representation. */
  1040. s = (int)(c -CHAR_0);
  1041. overflow = FALSE;
  1042. while (IS_DIGIT(ptr[1]))
  1043. {
  1044. if (s > INT_MAX / 10 - 1) /* Integer overflow */
  1045. {
  1046. overflow = TRUE;
  1047. break;
  1048. }
  1049. s = s * 10 + (int)(*(++ptr) - CHAR_0);
  1050. }
  1051. if (overflow) /* Integer overflow */
  1052. {
  1053. while (IS_DIGIT(ptr[1]))
  1054. ptr++;
  1055. *errorcodeptr = ERR61;
  1056. break;
  1057. }
  1058. if (s < 8 || s <= bracount) /* Check for back reference */
  1059. {
  1060. escape = -s;
  1061. break;
  1062. }
  1063. ptr = oldptr; /* Put the pointer back and fall through */
  1064. }
  1065. /* Handle a digit following \ when the number is not a back reference. If
  1066. the first digit is 8 or 9, Perl used to generate a binary zero byte and
  1067. then treat the digit as a following literal. At least by Perl 5.18 this
  1068. changed so as not to insert the binary zero. */
  1069. if ((c = *ptr) >= CHAR_8) break;
  1070. /* Fall through with a digit less than 8 */
  1071. /* \0 always starts an octal number, but we may drop through to here with a
  1072. larger first octal digit. The original code used just to take the least
  1073. significant 8 bits of octal numbers (I think this is what early Perls used
  1074. to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode,
  1075. but no more than 3 octal digits. */
  1076. case CHAR_0:
  1077. c -= CHAR_0;
  1078. while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
  1079. c = c * 8 + *(++ptr) - CHAR_0;
  1080. #ifdef COMPILE_PCRE8
  1081. if (!utf && c > 0xff) *errorcodeptr = ERR51;
  1082. #endif
  1083. break;
  1084. /* \o is a relatively new Perl feature, supporting a more general way of
  1085. specifying character codes in octal. The only supported form is \o{ddd}. */
  1086. case CHAR_o:
  1087. if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
  1088. if (ptr[2] == CHAR_RIGHT_CURLY_BRACKET) *errorcodeptr = ERR86; else
  1089. {
  1090. ptr += 2;
  1091. c = 0;
  1092. overflow = FALSE;
  1093. while (*ptr >= CHAR_0 && *ptr <= CHAR_7)
  1094. {
  1095. register pcre_uint32 cc = *ptr++;
  1096. if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */
  1097. #ifdef COMPILE_PCRE32
  1098. if (c >= 0x20000000l) { overflow = TRUE; break; }
  1099. #endif
  1100. c = (c << 3) + cc - CHAR_0 ;
  1101. #if defined COMPILE_PCRE8
  1102. if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
  1103. #elif defined COMPILE_PCRE16
  1104. if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
  1105. #elif defined COMPILE_PCRE32
  1106. if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
  1107. #endif
  1108. }
  1109. if (overflow)
  1110. {
  1111. while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++;
  1112. *errorcodeptr = ERR34;
  1113. }
  1114. else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
  1115. {
  1116. if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
  1117. }
  1118. else *errorcodeptr = ERR80;
  1119. }
  1120. break;
  1121. /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
  1122. numbers. Otherwise it is a lowercase x letter. */
  1123. case CHAR_x:
  1124. if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
  1125. {
  1126. if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
  1127. && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
  1128. {
  1129. c = 0;
  1130. for (i = 0; i < 2; ++i)
  1131. {
  1132. register pcre_uint32 cc = *(++ptr);
  1133. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  1134. if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
  1135. c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
  1136. #else /* EBCDIC coding */
  1137. if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
  1138. c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
  1139. #endif
  1140. }
  1141. }
  1142. } /* End JavaScript handling */
  1143. /* Handle \x in Perl's style. \x{ddd} is a character number which can be
  1144. greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
  1145. digits. If not, { used to be treated as a data character. However, Perl
  1146. seems to read hex digits up to the first non-such, and ignore the rest, so
  1147. that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
  1148. now gives an error. */
  1149. else
  1150. {
  1151. if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
  1152. {
  1153. ptr += 2;
  1154. if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
  1155. {
  1156. *errorcodeptr = ERR86;
  1157. break;
  1158. }
  1159. c = 0;
  1160. overflow = FALSE;
  1161. while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)
  1162. {
  1163. register pcre_uint32 cc = *ptr++;
  1164. if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */
  1165. #ifdef COMPILE_PCRE32
  1166. if (c >= 0x10000000l) { overflow = TRUE; break; }
  1167. #endif
  1168. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  1169. if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
  1170. c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
  1171. #else /* EBCDIC coding */
  1172. if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
  1173. c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
  1174. #endif
  1175. #if defined COMPILE_PCRE8
  1176. if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
  1177. #elif defined COMPILE_PCRE16
  1178. if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
  1179. #elif defined COMPILE_PCRE32
  1180. if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
  1181. #endif
  1182. }
  1183. if (overflow)
  1184. {
  1185. while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++;
  1186. *errorcodeptr = ERR34;
  1187. }
  1188. else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
  1189. {
  1190. if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
  1191. }
  1192. /* If the sequence of hex digits does not end with '}', give an error.
  1193. We used just to recognize this construct and fall through to the normal
  1194. \x handling, but nowadays Perl gives an error, which seems much more
  1195. sensible, so we do too. */
  1196. else *errorcodeptr = ERR79;
  1197. } /* End of \x{} processing */
  1198. /* Read a single-byte hex-defined char (up to two hex digits after \x) */
  1199. else
  1200. {
  1201. c = 0;
  1202. while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
  1203. {
  1204. pcre_uint32 cc; /* Some compilers don't like */
  1205. cc = *(++ptr); /* ++ in initializers */
  1206. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  1207. if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
  1208. c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
  1209. #else /* EBCDIC coding */
  1210. if (cc <= CHAR_z) cc += 64; /* Convert to upper case */
  1211. c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
  1212. #endif
  1213. }
  1214. } /* End of \xdd handling */
  1215. } /* End of Perl-style \x handling */
  1216. break;
  1217. /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
  1218. An error is given if the byte following \c is not an ASCII character. This
  1219. coding is ASCII-specific, but then the whole concept of \cx is
  1220. ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
  1221. case CHAR_c:
  1222. c = *(++ptr);
  1223. if (c == CHAR_NULL)
  1224. {
  1225. *errorcodeptr = ERR2;
  1226. break;
  1227. }
  1228. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  1229. if (c > 127) /* Excludes all non-ASCII in either mode */
  1230. {
  1231. *errorcodeptr = ERR68;
  1232. break;
  1233. }
  1234. if (c >= CHAR_a && c <= CHAR_z) c -= 32;
  1235. c ^= 0x40;
  1236. #else /* EBCDIC coding */
  1237. if (c >= CHAR_a && c <= CHAR_z) c += 64;
  1238. if (c == CHAR_QUESTION_MARK)
  1239. c = ('\\' == 188 && '`' == 74)? 0x5f : 0xff;
  1240. else
  1241. {
  1242. for (i = 0; i < 32; i++)
  1243. {
  1244. if (c == ebcdic_escape_c[i]) break;
  1245. }
  1246. if (i < 32) c = i; else *errorcodeptr = ERR68;
  1247. }
  1248. #endif
  1249. break;
  1250. /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
  1251. other alphanumeric following \ is an error if PCRE_EXTRA was set;
  1252. otherwise, for Perl compatibility, it is a literal. This code looks a bit
  1253. odd, but there used to be some cases other than the default, and there may
  1254. be again in future, so I haven't "optimized" it. */
  1255. default:
  1256. if ((options & PCRE_EXTRA) != 0) switch(c)
  1257. {
  1258. default:
  1259. *errorcodeptr = ERR3;
  1260. break;
  1261. }
  1262. break;
  1263. }
  1264. }
  1265. /* Perl supports \N{name} for character names, as well as plain \N for "not
  1266. newline". PCRE does not support \N{name}. However, it does support
  1267. quantification such as \N{2,3}. */
  1268. if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
  1269. !is_counted_repeat(ptr+2))
  1270. *errorcodeptr = ERR37;
  1271. /* If PCRE_UCP is set, we change the values for \d etc. */
  1272. if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
  1273. escape += (ESC_DU - ESC_D);
  1274. /* Set the pointer to the final character before returning. */
  1275. *ptrptr = ptr;
  1276. *chptr = c;
  1277. return escape;
  1278. }
  1279. #ifdef SUPPORT_UCP
  1280. /*************************************************
  1281. * Handle \P and \p *
  1282. *************************************************/
  1283. /* This function is called after \P or \p has been encountered, provided that
  1284. PCRE is compiled with support for Unicode properties. On entry, ptrptr is
  1285. pointing at the P or p. On exit, it is pointing at the final character of the
  1286. escape sequence.
  1287. Argument:
  1288. ptrptr points to the pattern position pointer
  1289. negptr points to a boolean that is set TRUE for negation else FALSE
  1290. ptypeptr points to an unsigned int that is set to the type value
  1291. pdataptr points to an unsigned int that is set to the detailed property value
  1292. errorcodeptr points to the error code variable
  1293. Returns: TRUE if the type value was found, or FALSE for an invalid type
  1294. */
  1295. static BOOL
  1296. get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
  1297. unsigned int *pdataptr, int *errorcodeptr)
  1298. {
  1299. pcre_uchar c;
  1300. int i, bot, top;
  1301. const pcre_uchar *ptr = *ptrptr;
  1302. pcre_uchar name[32];
  1303. c = *(++ptr);
  1304. if (c == CHAR_NULL) goto ERROR_RETURN;
  1305. *negptr = FALSE;
  1306. /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
  1307. negation. */
  1308. if (c == CHAR_LEFT_CURLY_BRACKET)
  1309. {
  1310. if (ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
  1311. {
  1312. *negptr = TRUE;
  1313. ptr++;
  1314. }
  1315. for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
  1316. {
  1317. c = *(++ptr);
  1318. if (c == CHAR_NULL) goto ERROR_RETURN;
  1319. if (c == CHAR_RIGHT_CURLY_BRACKET) break;
  1320. name[i] = c;
  1321. }
  1322. if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN;
  1323. name[i] = 0;
  1324. }
  1325. /* Otherwise there is just one following character */
  1326. else
  1327. {
  1328. name[0] = c;
  1329. name[1] = 0;
  1330. }
  1331. *ptrptr = ptr;
  1332. /* Search for a recognized property name using binary chop */
  1333. bot = 0;
  1334. top = PRIV(utt_size);
  1335. while (bot < top)
  1336. {
  1337. int r;
  1338. i = (bot + top) >> 1;
  1339. r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
  1340. if (r == 0)
  1341. {
  1342. *ptypeptr = PRIV(utt)[i].type;
  1343. *pdataptr = PRIV(utt)[i].value;
  1344. return TRUE;
  1345. }
  1346. if (r > 0) bot = i + 1; else top = i;
  1347. }
  1348. *errorcodeptr = ERR47;
  1349. *ptrptr = ptr;
  1350. return FALSE;
  1351. ERROR_RETURN:
  1352. *errorcodeptr = ERR46;
  1353. *ptrptr = ptr;
  1354. return FALSE;
  1355. }
  1356. #endif
  1357. /*************************************************
  1358. * Read repeat counts *
  1359. *************************************************/
  1360. /* Read an item of the form {n,m} and return the values. This is called only
  1361. after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
  1362. so the syntax is guaranteed to be correct, but we need to check the values.
  1363. Arguments:
  1364. p pointer to first char after '{'
  1365. minp pointer to int for min
  1366. maxp pointer to int for max
  1367. returned as -1 if no max
  1368. errorcodeptr points to error code variable
  1369. Returns: pointer to '}' on success;
  1370. current ptr on error, with errorcodeptr set non-zero
  1371. */
  1372. static const pcre_uchar *
  1373. read_repeat_counts(const pcre_uchar *p, int *minp, int *maxp, int *errorcodeptr)
  1374. {
  1375. int min = 0;
  1376. int max = -1;
  1377. while (IS_DIGIT(*p))
  1378. {
  1379. min = min * 10 + (int)(*p++ - CHAR_0);
  1380. if (min > 65535)
  1381. {
  1382. *errorcodeptr = ERR5;
  1383. return p;
  1384. }
  1385. }
  1386. if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
  1387. {
  1388. if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
  1389. {
  1390. max = 0;
  1391. while(IS_DIGIT(*p))
  1392. {
  1393. max = max * 10 + (int)(*p++ - CHAR_0);
  1394. if (max > 65535)
  1395. {
  1396. *errorcodeptr = ERR5;
  1397. return p;
  1398. }
  1399. }
  1400. if (max < min)
  1401. {
  1402. *errorcodeptr = ERR4;
  1403. return p;
  1404. }
  1405. }
  1406. }
  1407. *minp = min;
  1408. *maxp = max;
  1409. return p;
  1410. }
  1411. /*************************************************
  1412. * Find first significant op code *
  1413. *************************************************/
  1414. /* This is called by several functions that scan a compiled expression looking
  1415. for a fixed first character, or an anchoring op code etc. It skips over things
  1416. that do not influence this. For some calls, it makes sense to skip negative
  1417. forward and all backward assertions, and also the \b assertion; for others it
  1418. does not.
  1419. Arguments:
  1420. code pointer to the start of the group
  1421. skipassert TRUE if certain assertions are to be skipped
  1422. Returns: pointer to the first significant opcode
  1423. */
  1424. static const pcre_uchar*
  1425. first_significant_code(const pcre_uchar *code, BOOL skipassert)
  1426. {
  1427. for (;;)
  1428. {
  1429. switch ((int)*code)
  1430. {
  1431. case OP_ASSERT_NOT:
  1432. case OP_ASSERTBACK:
  1433. case OP_ASSERTBACK_NOT:
  1434. if (!skipassert) return code;
  1435. do code += GET(code, 1); while (*code == OP_ALT);
  1436. code += PRIV(OP_lengths)[*code];
  1437. break;
  1438. case OP_WORD_BOUNDARY:
  1439. case OP_NOT_WORD_BOUNDARY:
  1440. if (!skipassert) return code;
  1441. /* Fall through */
  1442. case OP_CALLOUT:
  1443. case OP_CREF:
  1444. case OP_DNCREF:
  1445. case OP_RREF:
  1446. case OP_DNRREF:
  1447. case OP_DEF:
  1448. code += PRIV(OP_lengths)[*code];
  1449. break;
  1450. default:
  1451. return code;
  1452. }
  1453. }
  1454. /* Control never reaches here */
  1455. }
  1456. /*************************************************
  1457. * Find the fixed length of a branch *
  1458. *************************************************/
  1459. /* Scan a branch and compute the fixed length of subject that will match it,
  1460. if the length is fixed. This is needed for dealing with backward assertions.
  1461. In UTF8 mode, the result is in characters rather than bytes. The branch is
  1462. temporarily terminated with OP_END when this function is called.
  1463. This function is called when a backward assertion is encountered, so that if it
  1464. fails, the error message can point to the correct place in the pattern.
  1465. However, we cannot do this when the assertion contains subroutine calls,
  1466. because they can be forward references. We solve this by remembering this case
  1467. and doing the check at the end; a flag specifies which mode we are running in.
  1468. Arguments:
  1469. code points to the start of the pattern (the bracket)
  1470. utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
  1471. atend TRUE if called when the pattern is complete
  1472. cd the "compile data" structure
  1473. recurses chain of recurse_check to catch mutual recursion
  1474. Returns: the fixed length,
  1475. or -1 if there is no fixed length,
  1476. or -2 if \C was encountered (in UTF-8 mode only)
  1477. or -3 if an OP_RECURSE item was encountered and atend is FALSE
  1478. or -4 if an unknown opcode was encountered (internal error)
  1479. */
  1480. static int
  1481. find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd,
  1482. recurse_check *recurses)
  1483. {
  1484. int length = -1;
  1485. recurse_check this_recurse;
  1486. register int branchlength = 0;
  1487. register pcre_uchar *cc = code + 1 + LINK_SIZE;
  1488. /* Scan along the opcodes for this branch. If we get to the end of the
  1489. branch, check the length against that of the other branches. */
  1490. for (;;)
  1491. {
  1492. int d;
  1493. pcre_uchar *ce, *cs;
  1494. register pcre_uchar op = *cc;
  1495. switch (op)
  1496. {
  1497. /* We only need to continue for OP_CBRA (normal capturing bracket) and
  1498. OP_BRA (normal non-capturing bracket) because the other variants of these
  1499. opcodes are all concerned with unlimited repeated groups, which of course
  1500. are not of fixed length. */
  1501. case OP_CBRA:
  1502. case OP_BRA:
  1503. case OP_ONCE:
  1504. case OP_ONCE_NC:
  1505. case OP_COND:
  1506. d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd,
  1507. recurses);
  1508. if (d < 0) return d;
  1509. branchlength += d;
  1510. do cc += GET(cc, 1); while (*cc == OP_ALT);
  1511. cc += 1 + LINK_SIZE;
  1512. break;
  1513. /* Reached end of a branch; if it's a ket it is the end of a nested call.
  1514. If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
  1515. an ALT. If it is END it's the end of the outer call. All can be handled by
  1516. the same code. Note that we must not include the OP_KETRxxx opcodes here,
  1517. because they all imply an unlimited repeat. */
  1518. case OP_ALT:
  1519. case OP_KET:
  1520. case OP_END:
  1521. case OP_ACCEPT:
  1522. case OP_ASSERT_ACCEPT:
  1523. if (length < 0) length = branchlength;
  1524. else if (length != branchlength) return -1;
  1525. if (*cc != OP_ALT) return length;
  1526. cc += 1 + LINK_SIZE;
  1527. branchlength = 0;
  1528. break;
  1529. /* A true recursion implies not fixed length, but a subroutine call may
  1530. be OK. If the subroutine is a forward reference, we can't deal with
  1531. it until the end of the pattern, so return -3. */
  1532. case OP_RECURSE:
  1533. if (!atend) return -3;
  1534. cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1); /* Start subpattern */
  1535. do ce += GET(ce, 1); while (*ce == OP_ALT); /* End subpattern */
  1536. if (cc > cs && cc < ce) return -1; /* Recursion */
  1537. else /* Check for mutual recursion */
  1538. {
  1539. recurse_check *r = recurses;
  1540. for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
  1541. if (r != NULL) return -1; /* Mutual recursion */
  1542. }
  1543. this_recurse.prev = recurses;
  1544. this_recurse.group = cs;
  1545. d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd, &this_recurse);
  1546. if (d < 0) return d;
  1547. branchlength += d;
  1548. cc += 1 + LINK_SIZE;
  1549. break;
  1550. /* Skip over assertive subpatterns */
  1551. case OP_ASSERT:
  1552. case OP_ASSERT_NOT:
  1553. case OP_ASSERTBACK:
  1554. case OP_ASSERTBACK_NOT:
  1555. do cc += GET(cc, 1); while (*cc == OP_ALT);
  1556. cc += 1 + LINK_SIZE;
  1557. break;
  1558. /* Skip over things that don't match chars */
  1559. case OP_MARK:
  1560. case OP_PRUNE_ARG:
  1561. case OP_SKIP_ARG:
  1562. case OP_THEN_ARG:
  1563. cc += cc[1] + PRIV(OP_lengths)[*cc];
  1564. break;
  1565. case OP_CALLOUT:
  1566. case OP_CIRC:
  1567. case OP_CIRCM:
  1568. case OP_CLOSE:
  1569. case OP_COMMIT:
  1570. case OP_CREF:
  1571. case OP_DEF:
  1572. case OP_DNCREF:
  1573. case OP_DNRREF:
  1574. case OP_DOLL:
  1575. case OP_DOLLM:
  1576. case OP_EOD:
  1577. case OP_EODN:
  1578. case OP_FAIL:
  1579. case OP_NOT_WORD_BOUNDARY:
  1580. case OP_PRUNE:
  1581. case OP_REVERSE:
  1582. case OP_RREF:
  1583. case OP_SET_SOM:
  1584. case OP_SKIP:
  1585. case OP_SOD:
  1586. case OP_SOM:
  1587. case OP_THEN:
  1588. case OP_WORD_BOUNDARY:
  1589. cc += PRIV(OP_lengths)[*cc];
  1590. break;
  1591. /* Handle literal characters */
  1592. case OP_CHAR:
  1593. case OP_CHARI:
  1594. case OP_NOT:
  1595. case OP_NOTI:
  1596. branchlength++;
  1597. cc += 2;
  1598. #ifdef SUPPORT_UTF
  1599. if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
  1600. #endif
  1601. break;
  1602. /* Handle exact repetitions. The count is already in characters, but we
  1603. need to skip over a multibyte character in UTF8 mode. */
  1604. case OP_EXACT:
  1605. case OP_EXACTI:
  1606. case OP_NOTEXACT:
  1607. case OP_NOTEXACTI:
  1608. branchlength += (int)GET2(cc,1);
  1609. cc += 2 + IMM2_SIZE;
  1610. #ifdef SUPPORT_UTF
  1611. if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
  1612. #endif
  1613. break;
  1614. case OP_TYPEEXACT:
  1615. branchlength += GET2(cc,1);
  1616. if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
  1617. cc += 2;
  1618. cc += 1 + IMM2_SIZE + 1;
  1619. break;
  1620. /* Handle single-char matchers */
  1621. case OP_PROP:
  1622. case OP_NOTPROP:
  1623. cc += 2;
  1624. /* Fall through */
  1625. case OP_HSPACE:
  1626. case OP_VSPACE:
  1627. case OP_NOT_HSPACE:
  1628. case OP_NOT_VSPACE:
  1629. case OP_NOT_DIGIT:
  1630. case OP_DIGIT:
  1631. case OP_NOT_WHITESPACE:
  1632. case OP_WHITESPACE:
  1633. case OP_NOT_WORDCHAR:
  1634. case OP_WORDCHAR:
  1635. case OP_ANY:
  1636. case OP_ALLANY:
  1637. branchlength++;
  1638. cc++;
  1639. break;
  1640. /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
  1641. otherwise \C is coded as OP_ALLANY. */
  1642. case OP_ANYBYTE:
  1643. return -2;
  1644. /* Check a class for variable quantification */
  1645. case OP_CLASS:
  1646. case OP_NCLASS:
  1647. #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  1648. case OP_XCLASS:
  1649. /* The original code caused an unsigned overflow in 64 bit systems,
  1650. so now we use a conditional statement. */
  1651. if (op == OP_XCLASS)
  1652. cc += GET(cc, 1);
  1653. else
  1654. cc += PRIV(OP_lengths)[OP_CLASS];
  1655. #else
  1656. cc += PRIV(OP_lengths)[OP_CLASS];
  1657. #endif
  1658. switch (*cc)
  1659. {
  1660. case OP_CRSTAR:
  1661. case OP_CRMINSTAR:
  1662. case OP_CRPLUS:
  1663. case OP_CRMINPLUS:
  1664. case OP_CRQUERY:
  1665. case OP_CRMINQUERY:
  1666. case OP_CRPOSSTAR:
  1667. case OP_CRPOSPLUS:
  1668. case OP_CRPOSQUERY:
  1669. return -1;
  1670. case OP_CRRANGE:
  1671. case OP_CRMINRANGE:
  1672. case OP_CRPOSRANGE:
  1673. if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
  1674. branchlength += (int)GET2(cc,1);
  1675. cc += 1 + 2 * IMM2_SIZE;
  1676. break;
  1677. default:
  1678. branchlength++;
  1679. }
  1680. break;
  1681. /* Anything else is variable length */
  1682. case OP_ANYNL:
  1683. case OP_BRAMINZERO:
  1684. case OP_BRAPOS:
  1685. case OP_BRAPOSZERO:
  1686. case OP_BRAZERO:
  1687. case OP_CBRAPOS:
  1688. case OP_EXTUNI:
  1689. case OP_KETRMAX:
  1690. case OP_KETRMIN:
  1691. case OP_KETRPOS:
  1692. case OP_MINPLUS:
  1693. case OP_MINPLUSI:
  1694. case OP_MINQUERY:
  1695. case OP_MINQUERYI:
  1696. case OP_MINSTAR:
  1697. case OP_MINSTARI:
  1698. case OP_MINUPTO:
  1699. case OP_MINUPTOI:
  1700. case OP_NOTMINPLUS:
  1701. case OP_NOTMINPLUSI:
  1702. case OP_NOTMINQUERY:
  1703. case OP_NOTMINQUERYI:
  1704. case OP_NOTMINSTAR:
  1705. case OP_NOTMINSTARI:
  1706. case OP_NOTMINUPTO:
  1707. case OP_NOTMINUPTOI:
  1708. case OP_NOTPLUS:
  1709. case OP_NOTPLUSI:
  1710. case OP_NOTPOSPLUS:
  1711. case OP_NOTPOSPLUSI:
  1712. case OP_NOTPOSQUERY:
  1713. case OP_NOTPOSQUERYI:
  1714. case OP_NOTPOSSTAR:
  1715. case OP_NOTPOSSTARI:
  1716. case OP_NOTPOSUPTO:
  1717. case OP_NOTPOSUPTOI:
  1718. case OP_NOTQUERY:
  1719. case OP_NOTQUERYI:
  1720. case OP_NOTSTAR:
  1721. case OP_NOTSTARI:
  1722. case OP_NOTUPTO:
  1723. case OP_NOTUPTOI:
  1724. case OP_PLUS:
  1725. case OP_PLUSI:
  1726. case OP_POSPLUS:
  1727. case OP_POSPLUSI:
  1728. case OP_POSQUERY:
  1729. case OP_POSQUERYI:
  1730. case OP_POSSTAR:
  1731. case OP_POSSTARI:
  1732. case OP_POSUPTO:
  1733. case OP_POSUPTOI:
  1734. case OP_QUERY:
  1735. case OP_QUERYI:
  1736. case OP_REF:
  1737. case OP_REFI:
  1738. case OP_DNREF:
  1739. case OP_DNREFI:
  1740. case OP_SBRA:
  1741. case OP_SBRAPOS:
  1742. case OP_SCBRA:
  1743. case OP_SCBRAPOS:
  1744. case OP_SCOND:
  1745. case OP_SKIPZERO:
  1746. case OP_STAR:
  1747. case OP_STARI:
  1748. case OP_TYPEMINPLUS:
  1749. case OP_TYPEMINQUERY:
  1750. case OP_TYPEMINSTAR:
  1751. case OP_TYPEMINUPTO:
  1752. case OP_TYPEPLUS:
  1753. case OP_TYPEPOSPLUS:
  1754. case OP_TYPEPOSQUERY:
  1755. case OP_TYPEPOSSTAR:
  1756. case OP_TYPEPOSUPTO:
  1757. case OP_TYPEQUERY:
  1758. case OP_TYPESTAR:
  1759. case OP_TYPEUPTO:
  1760. case OP_UPTO:
  1761. case OP_UPTOI:
  1762. return -1;
  1763. /* Catch unrecognized opcodes so that when new ones are added they
  1764. are not forgotten, as has happened in the past. */
  1765. default:
  1766. return -4;
  1767. }
  1768. }
  1769. /* Control never gets here */
  1770. }
  1771. /*************************************************
  1772. * Scan compiled regex for specific bracket *
  1773. *************************************************/
  1774. /* This little function scans through a compiled pattern until it finds a
  1775. capturing bracket with the given number, or, if the number is negative, an
  1776. instance of OP_REVERSE for a lookbehind. The function is global in the C sense
  1777. so that it can be called from pcre_study() when finding the minimum matching
  1778. length.
  1779. Arguments:
  1780. code points to start of expression
  1781. utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
  1782. number the required bracket number or negative to find a lookbehind
  1783. Returns: pointer to the opcode for the bracket, or NULL if not found
  1784. */
  1785. const pcre_uchar *
  1786. PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
  1787. {
  1788. for (;;)
  1789. {
  1790. register pcre_uchar c = *code;
  1791. if (c == OP_END) return NULL;
  1792. /* XCLASS is used for classes that cannot be represented just by a bit
  1793. map. This includes negated single high-valued characters. The length in
  1794. the table is zero; the actual length is stored in the compiled code. */
  1795. if (c == OP_XCLASS) code += GET(code, 1);
  1796. /* Handle recursion */
  1797. else if (c == OP_REVERSE)
  1798. {
  1799. if (number < 0) return (pcre_uchar *)code;
  1800. code += PRIV(OP_lengths)[c];
  1801. }
  1802. /* Handle capturing bracket */
  1803. else if (c == OP_CBRA || c == OP_SCBRA ||
  1804. c == OP_CBRAPOS || c == OP_SCBRAPOS)
  1805. {
  1806. int n = (int)GET2(code, 1+LINK_SIZE);
  1807. if (n == number) return (pcre_uchar *)code;
  1808. code += PRIV(OP_lengths)[c];
  1809. }
  1810. /* Otherwise, we can get the item's length from the table, except that for
  1811. repeated character types, we have to test for \p and \P, which have an extra
  1812. two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
  1813. must add in its length. */
  1814. else
  1815. {
  1816. switch(c)
  1817. {
  1818. case OP_TYPESTAR:
  1819. case OP_TYPEMINSTAR:
  1820. case OP_TYPEPLUS:
  1821. case OP_TYPEMINPLUS:
  1822. case OP_TYPEQUERY:
  1823. case OP_TYPEMINQUERY:
  1824. case OP_TYPEPOSSTAR:
  1825. case OP_TYPEPOSPLUS:
  1826. case OP_TYPEPOSQUERY:
  1827. if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
  1828. break;
  1829. case OP_TYPEUPTO:
  1830. case OP_TYPEMINUPTO:
  1831. case OP_TYPEEXACT:
  1832. case OP_TYPEPOSUPTO:
  1833. if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
  1834. code += 2;
  1835. break;
  1836. case OP_MARK:
  1837. case OP_PRUNE_ARG:
  1838. case OP_SKIP_ARG:
  1839. case OP_THEN_ARG:
  1840. code += code[1];
  1841. break;
  1842. }
  1843. /* Add in the fixed length from the table */
  1844. code += PRIV(OP_lengths)[c];
  1845. /* In UTF-8 mode, opcodes that are followed by a character may be followed by
  1846. a multi-byte character. The length in the table is a minimum, so we have to
  1847. arrange to skip the extra bytes. */
  1848. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  1849. if (utf) switch(c)
  1850. {
  1851. case OP_CHAR:
  1852. case OP_CHARI:
  1853. case OP_NOT:
  1854. case OP_NOTI:
  1855. case OP_EXACT:
  1856. case OP_EXACTI:
  1857. case OP_NOTEXACT:
  1858. case OP_NOTEXACTI:
  1859. case OP_UPTO:
  1860. case OP_UPTOI:
  1861. case OP_NOTUPTO:
  1862. case OP_NOTUPTOI:
  1863. case OP_MINUPTO:
  1864. case OP_MINUPTOI:
  1865. case OP_NOTMINUPTO:
  1866. case OP_NOTMINUPTOI:
  1867. case OP_POSUPTO:
  1868. case OP_POSUPTOI:
  1869. case OP_NOTPOSUPTO:
  1870. case OP_NOTPOSUPTOI:
  1871. case OP_STAR:
  1872. case OP_STARI:
  1873. case OP_NOTSTAR:
  1874. case OP_NOTSTARI:
  1875. case OP_MINSTAR:
  1876. case OP_MINSTARI:
  1877. case OP_NOTMINSTAR:
  1878. case OP_NOTMINSTARI:
  1879. case OP_POSSTAR:
  1880. case OP_POSSTARI:
  1881. case OP_NOTPOSSTAR:
  1882. case OP_NOTPOSSTARI:
  1883. case OP_PLUS:
  1884. case OP_PLUSI:
  1885. case OP_NOTPLUS:
  1886. case OP_NOTPLUSI:
  1887. case OP_MINPLUS:
  1888. case OP_MINPLUSI:
  1889. case OP_NOTMINPLUS:
  1890. case OP_NOTMINPLUSI:
  1891. case OP_POSPLUS:
  1892. case OP_POSPLUSI:
  1893. case OP_NOTPOSPLUS:
  1894. case OP_NOTPOSPLUSI:
  1895. case OP_QUERY:
  1896. case OP_QUERYI:
  1897. case OP_NOTQUERY:
  1898. case OP_NOTQUERYI:
  1899. case OP_MINQUERY:
  1900. case OP_MINQUERYI:
  1901. case OP_NOTMINQUERY:
  1902. case OP_NOTMINQUERYI:
  1903. case OP_POSQUERY:
  1904. case OP_POSQUERYI:
  1905. case OP_NOTPOSQUERY:
  1906. case OP_NOTPOSQUERYI:
  1907. if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
  1908. break;
  1909. }
  1910. #else
  1911. (void)(utf); /* Keep compiler happy by referencing function argument */
  1912. #endif
  1913. }
  1914. }
  1915. }
  1916. /*************************************************
  1917. * Scan compiled regex for recursion reference *
  1918. *************************************************/
  1919. /* This little function scans through a compiled pattern until it finds an
  1920. instance of OP_RECURSE.
  1921. Arguments:
  1922. code points to start of expression
  1923. utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
  1924. Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
  1925. */
  1926. static const pcre_uchar *
  1927. find_recurse(const pcre_uchar *code, BOOL utf)
  1928. {
  1929. for (;;)
  1930. {
  1931. register pcre_uchar c = *code;
  1932. if (c == OP_END) return NULL;
  1933. if (c == OP_RECURSE) return code;
  1934. /* XCLASS is used for classes that cannot be represented just by a bit
  1935. map. This includes negated single high-valued characters. The length in
  1936. the table is zero; the actual length is stored in the compiled code. */
  1937. if (c == OP_XCLASS) code += GET(code, 1);
  1938. /* Otherwise, we can get the item's length from the table, except that for
  1939. repeated character types, we have to test for \p and \P, which have an extra
  1940. two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
  1941. must add in its length. */
  1942. else
  1943. {
  1944. switch(c)
  1945. {
  1946. case OP_TYPESTAR:
  1947. case OP_TYPEMINSTAR:
  1948. case OP_TYPEPLUS:
  1949. case OP_TYPEMINPLUS:
  1950. case OP_TYPEQUERY:
  1951. case OP_TYPEMINQUERY:
  1952. case OP_TYPEPOSSTAR:
  1953. case OP_TYPEPOSPLUS:
  1954. case OP_TYPEPOSQUERY:
  1955. if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
  1956. break;
  1957. case OP_TYPEPOSUPTO:
  1958. case OP_TYPEUPTO:
  1959. case OP_TYPEMINUPTO:
  1960. case OP_TYPEEXACT:
  1961. if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
  1962. code += 2;
  1963. break;
  1964. case OP_MARK:
  1965. case OP_PRUNE_ARG:
  1966. case OP_SKIP_ARG:
  1967. case OP_THEN_ARG:
  1968. code += code[1];
  1969. break;
  1970. }
  1971. /* Add in the fixed length from the table */
  1972. code += PRIV(OP_lengths)[c];
  1973. /* In UTF-8 mode, opcodes that are followed by a character may be followed
  1974. by a multi-byte character. The length in the table is a minimum, so we have
  1975. to arrange to skip the extra bytes. */
  1976. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  1977. if (utf) switch(c)
  1978. {
  1979. case OP_CHAR:
  1980. case OP_CHARI:
  1981. case OP_NOT:
  1982. case OP_NOTI:
  1983. case OP_EXACT:
  1984. case OP_EXACTI:
  1985. case OP_NOTEXACT:
  1986. case OP_NOTEXACTI:
  1987. case OP_UPTO:
  1988. case OP_UPTOI:
  1989. case OP_NOTUPTO:
  1990. case OP_NOTUPTOI:
  1991. case OP_MINUPTO:
  1992. case OP_MINUPTOI:
  1993. case OP_NOTMINUPTO:
  1994. case OP_NOTMINUPTOI:
  1995. case OP_POSUPTO:
  1996. case OP_POSUPTOI:
  1997. case OP_NOTPOSUPTO:
  1998. case OP_NOTPOSUPTOI:
  1999. case OP_STAR:
  2000. case OP_STARI:
  2001. case OP_NOTSTAR:
  2002. case OP_NOTSTARI:
  2003. case OP_MINSTAR:
  2004. case OP_MINSTARI:
  2005. case OP_NOTMINSTAR:
  2006. case OP_NOTMINSTARI:
  2007. case OP_POSSTAR:
  2008. case OP_POSSTARI:
  2009. case OP_NOTPOSSTAR:
  2010. case OP_NOTPOSSTARI:
  2011. case OP_PLUS:
  2012. case OP_PLUSI:
  2013. case OP_NOTPLUS:
  2014. case OP_NOTPLUSI:
  2015. case OP_MINPLUS:
  2016. case OP_MINPLUSI:
  2017. case OP_NOTMINPLUS:
  2018. case OP_NOTMINPLUSI:
  2019. case OP_POSPLUS:
  2020. case OP_POSPLUSI:
  2021. case OP_NOTPOSPLUS:
  2022. case OP_NOTPOSPLUSI:
  2023. case OP_QUERY:
  2024. case OP_QUERYI:
  2025. case OP_NOTQUERY:
  2026. case OP_NOTQUERYI:
  2027. case OP_MINQUERY:
  2028. case OP_MINQUERYI:
  2029. case OP_NOTMINQUERY:
  2030. case OP_NOTMINQUERYI:
  2031. case OP_POSQUERY:
  2032. case OP_POSQUERYI:
  2033. case OP_NOTPOSQUERY:
  2034. case OP_NOTPOSQUERYI:
  2035. if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
  2036. break;
  2037. }
  2038. #else
  2039. (void)(utf); /* Keep compiler happy by referencing function argument */
  2040. #endif
  2041. }
  2042. }
  2043. }
  2044. /*************************************************
  2045. * Scan compiled branch for non-emptiness *
  2046. *************************************************/
  2047. /* This function scans through a branch of a compiled pattern to see whether it
  2048. can match the empty string or not. It is called from could_be_empty()
  2049. below and from compile_branch() when checking for an unlimited repeat of a
  2050. group that can match nothing. Note that first_significant_code() skips over
  2051. backward and negative forward assertions when its final argument is TRUE. If we
  2052. hit an unclosed bracket, we return "empty" - this means we've struck an inner
  2053. bracket whose current branch will already have been scanned.
  2054. Arguments:
  2055. code points to start of search
  2056. endcode points to where to stop
  2057. utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
  2058. cd contains pointers to tables etc.
  2059. recurses chain of recurse_check to catch mutual recursion
  2060. Returns: TRUE if what is matched could be empty
  2061. */
  2062. static BOOL
  2063. could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
  2064. BOOL utf, compile_data *cd, recurse_check *recurses)
  2065. {
  2066. register pcre_uchar c;
  2067. recurse_check this_recurse;
  2068. for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
  2069. code < endcode;
  2070. code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
  2071. {
  2072. const pcre_uchar *ccode;
  2073. c = *code;
  2074. /* Skip over forward assertions; the other assertions are skipped by
  2075. first_significant_code() with a TRUE final argument. */
  2076. if (c == OP_ASSERT)
  2077. {
  2078. do code += GET(code, 1); while (*code == OP_ALT);
  2079. c = *code;
  2080. continue;
  2081. }
  2082. /* For a recursion/subroutine call, if its end has been reached, which
  2083. implies a backward reference subroutine call, we can scan it. If it's a
  2084. forward reference subroutine call, we can't. To detect forward reference
  2085. we have to scan up the list that is kept in the workspace. This function is
  2086. called only when doing the real compile, not during the pre-compile that
  2087. measures the size of the compiled pattern. */
  2088. if (c == OP_RECURSE)
  2089. {
  2090. const pcre_uchar *scode = cd->start_code + GET(code, 1);
  2091. const pcre_uchar *endgroup = scode;
  2092. BOOL empty_branch;
  2093. /* Test for forward reference or uncompleted reference. This is disabled
  2094. when called to scan a completed pattern by setting cd->start_workspace to
  2095. NULL. */
  2096. if (cd->start_workspace != NULL)
  2097. {
  2098. const pcre_uchar *tcode;
  2099. for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
  2100. if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
  2101. if (GET(scode, 1) == 0) return TRUE; /* Unclosed */
  2102. }
  2103. /* If the reference is to a completed group, we need to detect whether this
  2104. is a recursive call, as otherwise there will be an infinite loop. If it is
  2105. a recursion, just skip over it. Simple recursions are easily detected. For
  2106. mutual recursions we keep a chain on the stack. */
  2107. do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
  2108. if (code >= scode && code <= endgroup) continue; /* Simple recursion */
  2109. else
  2110. {
  2111. recurse_check *r = recurses;
  2112. for (r = recurses; r != NULL; r = r->prev)
  2113. if (r->group == scode) break;
  2114. if (r != NULL) continue; /* Mutual recursion */
  2115. }
  2116. /* Completed reference; scan the referenced group, remembering it on the
  2117. stack chain to detect mutual recursions. */
  2118. empty_branch = FALSE;
  2119. this_recurse.prev = recurses;
  2120. this_recurse.group = scode;
  2121. do
  2122. {
  2123. if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
  2124. {
  2125. empty_branch = TRUE;
  2126. break;
  2127. }
  2128. scode += GET(scode, 1);
  2129. }
  2130. while (*scode == OP_ALT);
  2131. if (!empty_branch) return FALSE; /* All branches are non-empty */
  2132. continue;
  2133. }
  2134. /* Groups with zero repeats can of course be empty; skip them. */
  2135. if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
  2136. c == OP_BRAPOSZERO)
  2137. {
  2138. code += PRIV(OP_lengths)[c];
  2139. do code += GET(code, 1); while (*code == OP_ALT);
  2140. c = *code;
  2141. continue;
  2142. }
  2143. /* A nested group that is already marked as "could be empty" can just be
  2144. skipped. */
  2145. if (c == OP_SBRA || c == OP_SBRAPOS ||
  2146. c == OP_SCBRA || c == OP_SCBRAPOS)
  2147. {
  2148. do code += GET(code, 1); while (*code == OP_ALT);
  2149. c = *code;
  2150. continue;
  2151. }
  2152. /* For other groups, scan the branches. */
  2153. if (c == OP_BRA || c == OP_BRAPOS ||
  2154. c == OP_CBRA || c == OP_CBRAPOS ||
  2155. c == OP_ONCE || c == OP_ONCE_NC ||
  2156. c == OP_COND || c == OP_SCOND)
  2157. {
  2158. BOOL empty_branch;
  2159. if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */
  2160. /* If a conditional group has only one branch, there is a second, implied,
  2161. empty branch, so just skip over the conditional, because it could be empty.
  2162. Otherwise, scan the individual branches of the group. */
  2163. if (c == OP_COND && code[GET(code, 1)] != OP_ALT)
  2164. code += GET(code, 1);
  2165. else
  2166. {
  2167. empty_branch = FALSE;
  2168. do
  2169. {
  2170. if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd,
  2171. recurses)) empty_branch = TRUE;
  2172. code += GET(code, 1);
  2173. }
  2174. while (*code == OP_ALT);
  2175. if (!empty_branch) return FALSE; /* All branches are non-empty */
  2176. }
  2177. c = *code;
  2178. continue;
  2179. }
  2180. /* Handle the other opcodes */
  2181. switch (c)
  2182. {
  2183. /* Check for quantifiers after a class. XCLASS is used for classes that
  2184. cannot be represented just by a bit map. This includes negated single
  2185. high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
  2186. actual length is stored in the compiled code, so we must update "code"
  2187. here. */
  2188. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  2189. case OP_XCLASS:
  2190. ccode = code += GET(code, 1);
  2191. goto CHECK_CLASS_REPEAT;
  2192. #endif
  2193. case OP_CLASS:
  2194. case OP_NCLASS:
  2195. ccode = code + PRIV(OP_lengths)[OP_CLASS];
  2196. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  2197. CHECK_CLASS_REPEAT:
  2198. #endif
  2199. switch (*ccode)
  2200. {
  2201. case OP_CRSTAR: /* These could be empty; continue */
  2202. case OP_CRMINSTAR:
  2203. case OP_CRQUERY:
  2204. case OP_CRMINQUERY:
  2205. case OP_CRPOSSTAR:
  2206. case OP_CRPOSQUERY:
  2207. break;
  2208. default: /* Non-repeat => class must match */
  2209. case OP_CRPLUS: /* These repeats aren't empty */
  2210. case OP_CRMINPLUS:
  2211. case OP_CRPOSPLUS:
  2212. return FALSE;
  2213. case OP_CRRANGE:
  2214. case OP_CRMINRANGE:
  2215. case OP_CRPOSRANGE:
  2216. if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */
  2217. break;
  2218. }
  2219. break;
  2220. /* Opcodes that must match a character */
  2221. case OP_ANY:
  2222. case OP_ALLANY:
  2223. case OP_ANYBYTE:
  2224. case OP_PROP:
  2225. case OP_NOTPROP:
  2226. case OP_ANYNL:
  2227. case OP_NOT_HSPACE:
  2228. case OP_HSPACE:
  2229. case OP_NOT_VSPACE:
  2230. case OP_VSPACE:
  2231. case OP_EXTUNI:
  2232. case OP_NOT_DIGIT:
  2233. case OP_DIGIT:
  2234. case OP_NOT_WHITESPACE:
  2235. case OP_WHITESPACE:
  2236. case OP_NOT_WORDCHAR:
  2237. case OP_WORDCHAR:
  2238. case OP_CHAR:
  2239. case OP_CHARI:
  2240. case OP_NOT:
  2241. case OP_NOTI:
  2242. case OP_PLUS:
  2243. case OP_PLUSI:
  2244. case OP_MINPLUS:
  2245. case OP_MINPLUSI:
  2246. case OP_NOTPLUS:
  2247. case OP_NOTPLUSI:
  2248. case OP_NOTMINPLUS:
  2249. case OP_NOTMINPLUSI:
  2250. case OP_POSPLUS:
  2251. case OP_POSPLUSI:
  2252. case OP_NOTPOSPLUS:
  2253. case OP_NOTPOSPLUSI:
  2254. case OP_EXACT:
  2255. case OP_EXACTI:
  2256. case OP_NOTEXACT:
  2257. case OP_NOTEXACTI:
  2258. case OP_TYPEPLUS:
  2259. case OP_TYPEMINPLUS:
  2260. case OP_TYPEPOSPLUS:
  2261. case OP_TYPEEXACT:
  2262. return FALSE;
  2263. /* These are going to continue, as they may be empty, but we have to
  2264. fudge the length for the \p and \P cases. */
  2265. case OP_TYPESTAR:
  2266. case OP_TYPEMINSTAR:
  2267. case OP_TYPEPOSSTAR:
  2268. case OP_TYPEQUERY:
  2269. case OP_TYPEMINQUERY:
  2270. case OP_TYPEPOSQUERY:
  2271. if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
  2272. break;
  2273. /* Same for these */
  2274. case OP_TYPEUPTO:
  2275. case OP_TYPEMINUPTO:
  2276. case OP_TYPEPOSUPTO:
  2277. if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
  2278. code += 2;
  2279. break;
  2280. /* End of branch */
  2281. case OP_KET:
  2282. case OP_KETRMAX:
  2283. case OP_KETRMIN:
  2284. case OP_KETRPOS:
  2285. case OP_ALT:
  2286. return TRUE;
  2287. /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
  2288. MINUPTO, and POSUPTO and their caseless and negative versions may be
  2289. followed by a multibyte character. */
  2290. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  2291. case OP_STAR:
  2292. case OP_STARI:
  2293. case OP_NOTSTAR:
  2294. case OP_NOTSTARI:
  2295. case OP_MINSTAR:
  2296. case OP_MINSTARI:
  2297. case OP_NOTMINSTAR:
  2298. case OP_NOTMINSTARI:
  2299. case OP_POSSTAR:
  2300. case OP_POSSTARI:
  2301. case OP_NOTPOSSTAR:
  2302. case OP_NOTPOSSTARI:
  2303. case OP_QUERY:
  2304. case OP_QUERYI:
  2305. case OP_NOTQUERY:
  2306. case OP_NOTQUERYI:
  2307. case OP_MINQUERY:
  2308. case OP_MINQUERYI:
  2309. case OP_NOTMINQUERY:
  2310. case OP_NOTMINQUERYI:
  2311. case OP_POSQUERY:
  2312. case OP_POSQUERYI:
  2313. case OP_NOTPOSQUERY:
  2314. case OP_NOTPOSQUERYI:
  2315. if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
  2316. break;
  2317. case OP_UPTO:
  2318. case OP_UPTOI:
  2319. case OP_NOTUPTO:
  2320. case OP_NOTUPTOI:
  2321. case OP_MINUPTO:
  2322. case OP_MINUPTOI:
  2323. case OP_NOTMINUPTO:
  2324. case OP_NOTMINUPTOI:
  2325. case OP_POSUPTO:
  2326. case OP_POSUPTOI:
  2327. case OP_NOTPOSUPTO:
  2328. case OP_NOTPOSUPTOI:
  2329. if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
  2330. break;
  2331. #endif
  2332. /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
  2333. string. */
  2334. case OP_MARK:
  2335. case OP_PRUNE_ARG:
  2336. case OP_SKIP_ARG:
  2337. case OP_THEN_ARG:
  2338. code += code[1];
  2339. break;
  2340. /* None of the remaining opcodes are required to match a character. */
  2341. default:
  2342. break;
  2343. }
  2344. }
  2345. return TRUE;
  2346. }
  2347. /*************************************************
  2348. * Scan compiled regex for non-emptiness *
  2349. *************************************************/
  2350. /* This function is called to check for left recursive calls. We want to check
  2351. the current branch of the current pattern to see if it could match the empty
  2352. string. If it could, we must look outwards for branches at other levels,
  2353. stopping when we pass beyond the bracket which is the subject of the recursion.
  2354. This function is called only during the real compile, not during the
  2355. pre-compile.
  2356. Arguments:
  2357. code points to start of the recursion
  2358. endcode points to where to stop (current RECURSE item)
  2359. bcptr points to the chain of current (unclosed) branch starts
  2360. utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
  2361. cd pointers to tables etc
  2362. Returns: TRUE if what is matched could be empty
  2363. */
  2364. static BOOL
  2365. could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
  2366. branch_chain *bcptr, BOOL utf, compile_data *cd)
  2367. {
  2368. while (bcptr != NULL && bcptr->current_branch >= code)
  2369. {
  2370. if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
  2371. return FALSE;
  2372. bcptr = bcptr->outer;
  2373. }
  2374. return TRUE;
  2375. }
  2376. /*************************************************
  2377. * Base opcode of repeated opcodes *
  2378. *************************************************/
  2379. /* Returns the base opcode for repeated single character type opcodes. If the
  2380. opcode is not a repeated character type, it returns with the original value.
  2381. Arguments: c opcode
  2382. Returns: base opcode for the type
  2383. */
  2384. static pcre_uchar
  2385. get_repeat_base(pcre_uchar c)
  2386. {
  2387. return (c > OP_TYPEPOSUPTO)? c :
  2388. (c >= OP_TYPESTAR)? OP_TYPESTAR :
  2389. (c >= OP_NOTSTARI)? OP_NOTSTARI :
  2390. (c >= OP_NOTSTAR)? OP_NOTSTAR :
  2391. (c >= OP_STARI)? OP_STARI :
  2392. OP_STAR;
  2393. }
  2394. #ifdef SUPPORT_UCP
  2395. /*************************************************
  2396. * Check a character and a property *
  2397. *************************************************/
  2398. /* This function is called by check_auto_possessive() when a property item
  2399. is adjacent to a fixed character.
  2400. Arguments:
  2401. c the character
  2402. ptype the property type
  2403. pdata the data for the type
  2404. negated TRUE if it's a negated property (\P or \p{^)
  2405. Returns: TRUE if auto-possessifying is OK
  2406. */
  2407. static BOOL
  2408. check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
  2409. BOOL negated)
  2410. {
  2411. const pcre_uint32 *p;
  2412. const ucd_record *prop = GET_UCD(c);
  2413. switch(ptype)
  2414. {
  2415. case PT_LAMP:
  2416. return (prop->chartype == ucp_Lu ||
  2417. prop->chartype == ucp_Ll ||
  2418. prop->chartype == ucp_Lt) == negated;
  2419. case PT_GC:
  2420. return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
  2421. case PT_PC:
  2422. return (pdata == prop->chartype) == negated;
  2423. case PT_SC:
  2424. return (pdata == prop->script) == negated;
  2425. /* These are specials */
  2426. case PT_ALNUM:
  2427. return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  2428. PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
  2429. /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
  2430. means that Perl space and POSIX space are now identical. PCRE was changed
  2431. at release 8.34. */
  2432. case PT_SPACE: /* Perl space */
  2433. case PT_PXSPACE: /* POSIX space */
  2434. switch(c)
  2435. {
  2436. HSPACE_CASES:
  2437. VSPACE_CASES:
  2438. return negated;
  2439. default:
  2440. return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
  2441. }
  2442. break; /* Control never reaches here */
  2443. case PT_WORD:
  2444. return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  2445. PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
  2446. c == CHAR_UNDERSCORE) == negated;
  2447. case PT_CLIST:
  2448. p = PRIV(ucd_caseless_sets) + prop->caseset;
  2449. for (;;)
  2450. {
  2451. if (c < *p) return !negated;
  2452. if (c == *p++) return negated;
  2453. }
  2454. break; /* Control never reaches here */
  2455. }
  2456. return FALSE;
  2457. }
  2458. #endif /* SUPPORT_UCP */
  2459. /*************************************************
  2460. * Fill the character property list *
  2461. *************************************************/
  2462. /* Checks whether the code points to an opcode that can take part in auto-
  2463. possessification, and if so, fills a list with its properties.
  2464. Arguments:
  2465. code points to start of expression
  2466. utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
  2467. fcc points to case-flipping table
  2468. list points to output list
  2469. list[0] will be filled with the opcode
  2470. list[1] will be non-zero if this opcode
  2471. can match an empty character string
  2472. list[2..7] depends on the opcode
  2473. Returns: points to the start of the next opcode if *code is accepted
  2474. NULL if *code is not accepted
  2475. */
  2476. static const pcre_uchar *
  2477. get_chr_property_list(const pcre_uchar *code, BOOL utf,
  2478. const pcre_uint8 *fcc, pcre_uint32 *list)
  2479. {
  2480. pcre_uchar c = *code;
  2481. pcre_uchar base;
  2482. const pcre_uchar *end;
  2483. pcre_uint32 chr;
  2484. #ifdef SUPPORT_UCP
  2485. pcre_uint32 *clist_dest;
  2486. const pcre_uint32 *clist_src;
  2487. #else
  2488. utf = utf; /* Suppress "unused parameter" compiler warning */
  2489. #endif
  2490. list[0] = c;
  2491. list[1] = FALSE;
  2492. code++;
  2493. if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
  2494. {
  2495. base = get_repeat_base(c);
  2496. c -= (base - OP_STAR);
  2497. if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
  2498. code += IMM2_SIZE;
  2499. list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
  2500. switch(base)
  2501. {
  2502. case OP_STAR:
  2503. list[0] = OP_CHAR;
  2504. break;
  2505. case OP_STARI:
  2506. list[0] = OP_CHARI;
  2507. break;
  2508. case OP_NOTSTAR:
  2509. list[0] = OP_NOT;
  2510. break;
  2511. case OP_NOTSTARI:
  2512. list[0] = OP_NOTI;
  2513. break;
  2514. case OP_TYPESTAR:
  2515. list[0] = *code;
  2516. code++;
  2517. break;
  2518. }
  2519. c = list[0];
  2520. }
  2521. switch(c)
  2522. {
  2523. case OP_NOT_DIGIT:
  2524. case OP_DIGIT:
  2525. case OP_NOT_WHITESPACE:
  2526. case OP_WHITESPACE:
  2527. case OP_NOT_WORDCHAR:
  2528. case OP_WORDCHAR:
  2529. case OP_ANY:
  2530. case OP_ALLANY:
  2531. case OP_ANYNL:
  2532. case OP_NOT_HSPACE:
  2533. case OP_HSPACE:
  2534. case OP_NOT_VSPACE:
  2535. case OP_VSPACE:
  2536. case OP_EXTUNI:
  2537. case OP_EODN:
  2538. case OP_EOD:
  2539. case OP_DOLL:
  2540. case OP_DOLLM:
  2541. return code;
  2542. case OP_CHAR:
  2543. case OP_NOT:
  2544. GETCHARINCTEST(chr, code);
  2545. list[2] = chr;
  2546. list[3] = NOTACHAR;
  2547. return code;
  2548. case OP_CHARI:
  2549. case OP_NOTI:
  2550. list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
  2551. GETCHARINCTEST(chr, code);
  2552. list[2] = chr;
  2553. #ifdef SUPPORT_UCP
  2554. if (chr < 128 || (chr < 256 && !utf))
  2555. list[3] = fcc[chr];
  2556. else
  2557. list[3] = UCD_OTHERCASE(chr);
  2558. #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
  2559. list[3] = (chr < 256) ? fcc[chr] : chr;
  2560. #else
  2561. list[3] = fcc[chr];
  2562. #endif
  2563. /* The othercase might be the same value. */
  2564. if (chr == list[3])
  2565. list[3] = NOTACHAR;
  2566. else
  2567. list[4] = NOTACHAR;
  2568. return code;
  2569. #ifdef SUPPORT_UCP
  2570. case OP_PROP:
  2571. case OP_NOTPROP:
  2572. if (code[0] != PT_CLIST)
  2573. {
  2574. list[2] = code[0];
  2575. list[3] = code[1];
  2576. return code + 2;
  2577. }
  2578. /* Convert only if we have enough space. */
  2579. clist_src = PRIV(ucd_caseless_sets) + code[1];
  2580. clist_dest = list + 2;
  2581. code += 2;
  2582. do {
  2583. if (clist_dest >= list + 8)
  2584. {
  2585. /* Early return if there is not enough space. This should never
  2586. happen, since all clists are shorter than 5 character now. */
  2587. list[2] = code[0];
  2588. list[3] = code[1];
  2589. return code;
  2590. }
  2591. *clist_dest++ = *clist_src;
  2592. }
  2593. while(*clist_src++ != NOTACHAR);
  2594. /* All characters are stored. The terminating NOTACHAR
  2595. is copied form the clist itself. */
  2596. list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
  2597. return code;
  2598. #endif
  2599. case OP_NCLASS:
  2600. case OP_CLASS:
  2601. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  2602. case OP_XCLASS:
  2603. if (c == OP_XCLASS)
  2604. end = code + GET(code, 0) - 1;
  2605. else
  2606. #endif
  2607. end = code + 32 / sizeof(pcre_uchar);
  2608. switch(*end)
  2609. {
  2610. case OP_CRSTAR:
  2611. case OP_CRMINSTAR:
  2612. case OP_CRQUERY:
  2613. case OP_CRMINQUERY:
  2614. case OP_CRPOSSTAR:
  2615. case OP_CRPOSQUERY:
  2616. list[1] = TRUE;
  2617. end++;
  2618. break;
  2619. case OP_CRPLUS:
  2620. case OP_CRMINPLUS:
  2621. case OP_CRPOSPLUS:
  2622. end++;
  2623. break;
  2624. case OP_CRRANGE:
  2625. case OP_CRMINRANGE:
  2626. case OP_CRPOSRANGE:
  2627. list[1] = (GET2(end, 1) == 0);
  2628. end += 1 + 2 * IMM2_SIZE;
  2629. break;
  2630. }
  2631. list[2] = (pcre_uint32)(end - code);
  2632. return end;
  2633. }
  2634. return NULL; /* Opcode not accepted */
  2635. }
  2636. /*************************************************
  2637. * Scan further character sets for match *
  2638. *************************************************/
  2639. /* Checks whether the base and the current opcode have a common character, in
  2640. which case the base cannot be possessified.
  2641. Arguments:
  2642. code points to the byte code
  2643. utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
  2644. cd static compile data
  2645. base_list the data list of the base opcode
  2646. Returns: TRUE if the auto-possessification is possible
  2647. */
  2648. static BOOL
  2649. compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
  2650. const pcre_uint32 *base_list, const pcre_uchar *base_end, int *rec_limit)
  2651. {
  2652. pcre_uchar c;
  2653. pcre_uint32 list[8];
  2654. const pcre_uint32 *chr_ptr;
  2655. const pcre_uint32 *ochr_ptr;
  2656. const pcre_uint32 *list_ptr;
  2657. const pcre_uchar *next_code;
  2658. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  2659. const pcre_uchar *xclass_flags;
  2660. #endif
  2661. const pcre_uint8 *class_bitset;
  2662. const pcre_uint8 *set1, *set2, *set_end;
  2663. pcre_uint32 chr;
  2664. BOOL accepted, invert_bits;
  2665. BOOL entered_a_group = FALSE;
  2666. if (*rec_limit == 0) return FALSE;
  2667. --(*rec_limit);
  2668. /* Note: the base_list[1] contains whether the current opcode has greedy
  2669. (represented by a non-zero value) quantifier. This is a different from
  2670. other character type lists, which stores here that the character iterator
  2671. matches to an empty string (also represented by a non-zero value). */
  2672. for(;;)
  2673. {
  2674. /* All operations move the code pointer forward.
  2675. Therefore infinite recursions are not possible. */
  2676. c = *code;
  2677. /* Skip over callouts */
  2678. if (c == OP_CALLOUT)
  2679. {
  2680. code += PRIV(OP_lengths)[c];
  2681. continue;
  2682. }
  2683. if (c == OP_ALT)
  2684. {
  2685. do code += GET(code, 1); while (*code == OP_ALT);
  2686. c = *code;
  2687. }
  2688. switch(c)
  2689. {
  2690. case OP_END:
  2691. case OP_KETRPOS:
  2692. /* TRUE only in greedy case. The non-greedy case could be replaced by
  2693. an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT
  2694. uses more memory, which we cannot get at this stage.) */
  2695. return base_list[1] != 0;
  2696. case OP_KET:
  2697. /* If the bracket is capturing, and referenced by an OP_RECURSE, or
  2698. it is an atomic sub-pattern (assert, once, etc.) the non-greedy case
  2699. cannot be converted to a possessive form. */
  2700. if (base_list[1] == 0) return FALSE;
  2701. switch(*(code - GET(code, 1)))
  2702. {
  2703. case OP_ASSERT:
  2704. case OP_ASSERT_NOT:
  2705. case OP_ASSERTBACK:
  2706. case OP_ASSERTBACK_NOT:
  2707. case OP_ONCE:
  2708. case OP_ONCE_NC:
  2709. /* Atomic sub-patterns and assertions can always auto-possessify their
  2710. last iterator. However, if the group was entered as a result of checking
  2711. a previous iterator, this is not possible. */
  2712. return !entered_a_group;
  2713. }
  2714. code += PRIV(OP_lengths)[c];
  2715. continue;
  2716. case OP_ONCE:
  2717. case OP_ONCE_NC:
  2718. case OP_BRA:
  2719. case OP_CBRA:
  2720. next_code = code + GET(code, 1);
  2721. code += PRIV(OP_lengths)[c];
  2722. while (*next_code == OP_ALT)
  2723. {
  2724. if (!compare_opcodes(code, utf, cd, base_list, base_end, rec_limit))
  2725. return FALSE;
  2726. code = next_code + 1 + LINK_SIZE;
  2727. next_code += GET(next_code, 1);
  2728. }
  2729. entered_a_group = TRUE;
  2730. continue;
  2731. case OP_BRAZERO:
  2732. case OP_BRAMINZERO:
  2733. next_code = code + 1;
  2734. if (*next_code != OP_BRA && *next_code != OP_CBRA
  2735. && *next_code != OP_ONCE && *next_code != OP_ONCE_NC) return FALSE;
  2736. do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
  2737. /* The bracket content will be checked by the
  2738. OP_BRA/OP_CBRA case above. */
  2739. next_code += 1 + LINK_SIZE;
  2740. if (!compare_opcodes(next_code, utf, cd, base_list, base_end, rec_limit))
  2741. return FALSE;
  2742. code += PRIV(OP_lengths)[c];
  2743. continue;
  2744. default:
  2745. break;
  2746. }
  2747. /* Check for a supported opcode, and load its properties. */
  2748. code = get_chr_property_list(code, utf, cd->fcc, list);
  2749. if (code == NULL) return FALSE; /* Unsupported */
  2750. /* If either opcode is a small character list, set pointers for comparing
  2751. characters from that list with another list, or with a property. */
  2752. if (base_list[0] == OP_CHAR)
  2753. {
  2754. chr_ptr = base_list + 2;
  2755. list_ptr = list;
  2756. }
  2757. else if (list[0] == OP_CHAR)
  2758. {
  2759. chr_ptr = list + 2;
  2760. list_ptr = base_list;
  2761. }
  2762. /* Character bitsets can also be compared to certain opcodes. */
  2763. else if (base_list[0] == OP_CLASS || list[0] == OP_CLASS
  2764. #ifdef COMPILE_PCRE8
  2765. /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */
  2766. || (!utf && (base_list[0] == OP_NCLASS || list[0] == OP_NCLASS))
  2767. #endif
  2768. )
  2769. {
  2770. #ifdef COMPILE_PCRE8
  2771. if (base_list[0] == OP_CLASS || (!utf && base_list[0] == OP_NCLASS))
  2772. #else
  2773. if (base_list[0] == OP_CLASS)
  2774. #endif
  2775. {
  2776. set1 = (pcre_uint8 *)(base_end - base_list[2]);
  2777. list_ptr = list;
  2778. }
  2779. else
  2780. {
  2781. set1 = (pcre_uint8 *)(code - list[2]);
  2782. list_ptr = base_list;
  2783. }
  2784. invert_bits = FALSE;
  2785. switch(list_ptr[0])
  2786. {
  2787. case OP_CLASS:
  2788. case OP_NCLASS:
  2789. set2 = (pcre_uint8 *)
  2790. ((list_ptr == list ? code : base_end) - list_ptr[2]);
  2791. break;
  2792. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  2793. case OP_XCLASS:
  2794. xclass_flags = (list_ptr == list ? code : base_end) - list_ptr[2] + LINK_SIZE;
  2795. if ((*xclass_flags & XCL_HASPROP) != 0) return FALSE;
  2796. if ((*xclass_flags & XCL_MAP) == 0)
  2797. {
  2798. /* No bits are set for characters < 256. */
  2799. if (list[1] == 0) return (*xclass_flags & XCL_NOT) == 0;
  2800. /* Might be an empty repeat. */
  2801. continue;
  2802. }
  2803. set2 = (pcre_uint8 *)(xclass_flags + 1);
  2804. break;
  2805. #endif
  2806. case OP_NOT_DIGIT:
  2807. invert_bits = TRUE;
  2808. /* Fall through */
  2809. case OP_DIGIT:
  2810. set2 = (pcre_uint8 *)(cd->cbits + cbit_digit);
  2811. break;
  2812. case OP_NOT_WHITESPACE:
  2813. invert_bits = TRUE;
  2814. /* Fall through */
  2815. case OP_WHITESPACE:
  2816. set2 = (pcre_uint8 *)(cd->cbits + cbit_space);
  2817. break;
  2818. case OP_NOT_WORDCHAR:
  2819. invert_bits = TRUE;
  2820. /* Fall through */
  2821. case OP_WORDCHAR:
  2822. set2 = (pcre_uint8 *)(cd->cbits + cbit_word);
  2823. break;
  2824. default:
  2825. return FALSE;
  2826. }
  2827. /* Because the sets are unaligned, we need
  2828. to perform byte comparison here. */
  2829. set_end = set1 + 32;
  2830. if (invert_bits)
  2831. {
  2832. do
  2833. {
  2834. if ((*set1++ & ~(*set2++)) != 0) return FALSE;
  2835. }
  2836. while (set1 < set_end);
  2837. }
  2838. else
  2839. {
  2840. do
  2841. {
  2842. if ((*set1++ & *set2++) != 0) return FALSE;
  2843. }
  2844. while (set1 < set_end);
  2845. }
  2846. if (list[1] == 0) return TRUE;
  2847. /* Might be an empty repeat. */
  2848. continue;
  2849. }
  2850. /* Some property combinations also acceptable. Unicode property opcodes are
  2851. processed specially; the rest can be handled with a lookup table. */
  2852. else
  2853. {
  2854. pcre_uint32 leftop, rightop;
  2855. leftop = base_list[0];
  2856. rightop = list[0];
  2857. #ifdef SUPPORT_UCP
  2858. accepted = FALSE; /* Always set in non-unicode case. */
  2859. if (leftop == OP_PROP || leftop == OP_NOTPROP)
  2860. {
  2861. if (rightop == OP_EOD)
  2862. accepted = TRUE;
  2863. else if (rightop == OP_PROP || rightop == OP_NOTPROP)
  2864. {
  2865. int n;
  2866. const pcre_uint8 *p;
  2867. BOOL same = leftop == rightop;
  2868. BOOL lisprop = leftop == OP_PROP;
  2869. BOOL risprop = rightop == OP_PROP;
  2870. BOOL bothprop = lisprop && risprop;
  2871. /* There's a table that specifies how each combination is to be
  2872. processed:
  2873. 0 Always return FALSE (never auto-possessify)
  2874. 1 Character groups are distinct (possessify if both are OP_PROP)
  2875. 2 Check character categories in the same group (general or particular)
  2876. 3 Return TRUE if the two opcodes are not the same
  2877. ... see comments below
  2878. */
  2879. n = propposstab[base_list[2]][list[2]];
  2880. switch(n)
  2881. {
  2882. case 0: break;
  2883. case 1: accepted = bothprop; break;
  2884. case 2: accepted = (base_list[3] == list[3]) != same; break;
  2885. case 3: accepted = !same; break;
  2886. case 4: /* Left general category, right particular category */
  2887. accepted = risprop && catposstab[base_list[3]][list[3]] == same;
  2888. break;
  2889. case 5: /* Right general category, left particular category */
  2890. accepted = lisprop && catposstab[list[3]][base_list[3]] == same;
  2891. break;
  2892. /* This code is logically tricky. Think hard before fiddling with it.
  2893. The posspropstab table has four entries per row. Each row relates to
  2894. one of PCRE's special properties such as ALNUM or SPACE or WORD.
  2895. Only WORD actually needs all four entries, but using repeats for the
  2896. others means they can all use the same code below.
  2897. The first two entries in each row are Unicode general categories, and
  2898. apply always, because all the characters they include are part of the
  2899. PCRE character set. The third and fourth entries are a general and a
  2900. particular category, respectively, that include one or more relevant
  2901. characters. One or the other is used, depending on whether the check
  2902. is for a general or a particular category. However, in both cases the
  2903. category contains more characters than the specials that are defined
  2904. for the property being tested against. Therefore, it cannot be used
  2905. in a NOTPROP case.
  2906. Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
  2907. Underscore is covered by ucp_P or ucp_Po. */
  2908. case 6: /* Left alphanum vs right general category */
  2909. case 7: /* Left space vs right general category */
  2910. case 8: /* Left word vs right general category */
  2911. p = posspropstab[n-6];
  2912. accepted = risprop && lisprop ==
  2913. (list[3] != p[0] &&
  2914. list[3] != p[1] &&
  2915. (list[3] != p[2] || !lisprop));
  2916. break;
  2917. case 9: /* Right alphanum vs left general category */
  2918. case 10: /* Right space vs left general category */
  2919. case 11: /* Right word vs left general category */
  2920. p = posspropstab[n-9];
  2921. accepted = lisprop && risprop ==
  2922. (base_list[3] != p[0] &&
  2923. base_list[3] != p[1] &&
  2924. (base_list[3] != p[2] || !risprop));
  2925. break;
  2926. case 12: /* Left alphanum vs right particular category */
  2927. case 13: /* Left space vs right particular category */
  2928. case 14: /* Left word vs right particular category */
  2929. p = posspropstab[n-12];
  2930. accepted = risprop && lisprop ==
  2931. (catposstab[p[0]][list[3]] &&
  2932. catposstab[p[1]][list[3]] &&
  2933. (list[3] != p[3] || !lisprop));
  2934. break;
  2935. case 15: /* Right alphanum vs left particular category */
  2936. case 16: /* Right space vs left particular category */
  2937. case 17: /* Right word vs left particular category */
  2938. p = posspropstab[n-15];
  2939. accepted = lisprop && risprop ==
  2940. (catposstab[p[0]][base_list[3]] &&
  2941. catposstab[p[1]][base_list[3]] &&
  2942. (base_list[3] != p[3] || !risprop));
  2943. break;
  2944. }
  2945. }
  2946. }
  2947. else
  2948. #endif /* SUPPORT_UCP */
  2949. accepted = leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
  2950. rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
  2951. autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
  2952. if (!accepted) return FALSE;
  2953. if (list[1] == 0) return TRUE;
  2954. /* Might be an empty repeat. */
  2955. continue;
  2956. }
  2957. /* Control reaches here only if one of the items is a small character list.
  2958. All characters are checked against the other side. */
  2959. do
  2960. {
  2961. chr = *chr_ptr;
  2962. switch(list_ptr[0])
  2963. {
  2964. case OP_CHAR:
  2965. ochr_ptr = list_ptr + 2;
  2966. do
  2967. {
  2968. if (chr == *ochr_ptr) return FALSE;
  2969. ochr_ptr++;
  2970. }
  2971. while(*ochr_ptr != NOTACHAR);
  2972. break;
  2973. case OP_NOT:
  2974. ochr_ptr = list_ptr + 2;
  2975. do
  2976. {
  2977. if (chr == *ochr_ptr)
  2978. break;
  2979. ochr_ptr++;
  2980. }
  2981. while(*ochr_ptr != NOTACHAR);
  2982. if (*ochr_ptr == NOTACHAR) return FALSE; /* Not found */
  2983. break;
  2984. /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
  2985. set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
  2986. case OP_DIGIT:
  2987. if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
  2988. break;
  2989. case OP_NOT_DIGIT:
  2990. if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
  2991. break;
  2992. case OP_WHITESPACE:
  2993. if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
  2994. break;
  2995. case OP_NOT_WHITESPACE:
  2996. if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
  2997. break;
  2998. case OP_WORDCHAR:
  2999. if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
  3000. break;
  3001. case OP_NOT_WORDCHAR:
  3002. if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
  3003. break;
  3004. case OP_HSPACE:
  3005. switch(chr)
  3006. {
  3007. HSPACE_CASES: return FALSE;
  3008. default: break;
  3009. }
  3010. break;
  3011. case OP_NOT_HSPACE:
  3012. switch(chr)
  3013. {
  3014. HSPACE_CASES: break;
  3015. default: return FALSE;
  3016. }
  3017. break;
  3018. case OP_ANYNL:
  3019. case OP_VSPACE:
  3020. switch(chr)
  3021. {
  3022. VSPACE_CASES: return FALSE;
  3023. default: break;
  3024. }
  3025. break;
  3026. case OP_NOT_VSPACE:
  3027. switch(chr)
  3028. {
  3029. VSPACE_CASES: break;
  3030. default: return FALSE;
  3031. }
  3032. break;
  3033. case OP_DOLL:
  3034. case OP_EODN:
  3035. switch (chr)
  3036. {
  3037. case CHAR_CR:
  3038. case CHAR_LF:
  3039. case CHAR_VT:
  3040. case CHAR_FF:
  3041. case CHAR_NEL:
  3042. #ifndef EBCDIC
  3043. case 0x2028:
  3044. case 0x2029:
  3045. #endif /* Not EBCDIC */
  3046. return FALSE;
  3047. }
  3048. break;
  3049. case OP_EOD: /* Can always possessify before \z */
  3050. break;
  3051. #ifdef SUPPORT_UCP
  3052. case OP_PROP:
  3053. case OP_NOTPROP:
  3054. if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
  3055. list_ptr[0] == OP_NOTPROP))
  3056. return FALSE;
  3057. break;
  3058. #endif
  3059. case OP_NCLASS:
  3060. if (chr > 255) return FALSE;
  3061. /* Fall through */
  3062. case OP_CLASS:
  3063. if (chr > 255) break;
  3064. class_bitset = (pcre_uint8 *)
  3065. ((list_ptr == list ? code : base_end) - list_ptr[2]);
  3066. if ((class_bitset[chr >> 3] & (1U << (chr & 7))) != 0) return FALSE;
  3067. break;
  3068. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  3069. case OP_XCLASS:
  3070. if (PRIV(xclass)(chr, (list_ptr == list ? code : base_end) -
  3071. list_ptr[2] + LINK_SIZE, utf)) return FALSE;
  3072. break;
  3073. #endif
  3074. default:
  3075. return FALSE;
  3076. }
  3077. chr_ptr++;
  3078. }
  3079. while(*chr_ptr != NOTACHAR);
  3080. /* At least one character must be matched from this opcode. */
  3081. if (list[1] == 0) return TRUE;
  3082. }
  3083. /* Control never reaches here. There used to be a fail-save return FALSE; here,
  3084. but some compilers complain about an unreachable statement. */
  3085. }
  3086. /*************************************************
  3087. * Scan compiled regex for auto-possession *
  3088. *************************************************/
  3089. /* Replaces single character iterations with their possessive alternatives
  3090. if appropriate. This function modifies the compiled opcode!
  3091. Arguments:
  3092. code points to start of the byte code
  3093. utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
  3094. cd static compile data
  3095. Returns: nothing
  3096. */
  3097. static void
  3098. auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
  3099. {
  3100. register pcre_uchar c;
  3101. const pcre_uchar *end;
  3102. pcre_uchar *repeat_opcode;
  3103. pcre_uint32 list[8];
  3104. int rec_limit;
  3105. for (;;)
  3106. {
  3107. c = *code;
  3108. /* When a pattern with bad UTF-8 encoding is compiled with NO_UTF_CHECK,
  3109. it may compile without complaining, but may get into a loop here if the code
  3110. pointer points to a bad value. This is, of course a documentated possibility,
  3111. when NO_UTF_CHECK is set, so it isn't a bug, but we can detect this case and
  3112. just give up on this optimization. */
  3113. if (c >= OP_TABLE_LENGTH) return;
  3114. if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
  3115. {
  3116. c -= get_repeat_base(c) - OP_STAR;
  3117. end = (c <= OP_MINUPTO) ?
  3118. get_chr_property_list(code, utf, cd->fcc, list) : NULL;
  3119. list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
  3120. rec_limit = 1000;
  3121. if (end != NULL && compare_opcodes(end, utf, cd, list, end, &rec_limit))
  3122. {
  3123. switch(c)
  3124. {
  3125. case OP_STAR:
  3126. *code += OP_POSSTAR - OP_STAR;
  3127. break;
  3128. case OP_MINSTAR:
  3129. *code += OP_POSSTAR - OP_MINSTAR;
  3130. break;
  3131. case OP_PLUS:
  3132. *code += OP_POSPLUS - OP_PLUS;
  3133. break;
  3134. case OP_MINPLUS:
  3135. *code += OP_POSPLUS - OP_MINPLUS;
  3136. break;
  3137. case OP_QUERY:
  3138. *code += OP_POSQUERY - OP_QUERY;
  3139. break;
  3140. case OP_MINQUERY:
  3141. *code += OP_POSQUERY - OP_MINQUERY;
  3142. break;
  3143. case OP_UPTO:
  3144. *code += OP_POSUPTO - OP_UPTO;
  3145. break;
  3146. case OP_MINUPTO:
  3147. *code += OP_POSUPTO - OP_MINUPTO;
  3148. break;
  3149. }
  3150. }
  3151. c = *code;
  3152. }
  3153. else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS)
  3154. {
  3155. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  3156. if (c == OP_XCLASS)
  3157. repeat_opcode = code + GET(code, 1);
  3158. else
  3159. #endif
  3160. repeat_opcode = code + 1 + (32 / sizeof(pcre_uchar));
  3161. c = *repeat_opcode;
  3162. if (c >= OP_CRSTAR && c <= OP_CRMINRANGE)
  3163. {
  3164. /* end must not be NULL. */
  3165. end = get_chr_property_list(code, utf, cd->fcc, list);
  3166. list[1] = (c & 1) == 0;
  3167. rec_limit = 1000;
  3168. if (compare_opcodes(end, utf, cd, list, end, &rec_limit))
  3169. {
  3170. switch (c)
  3171. {
  3172. case OP_CRSTAR:
  3173. case OP_CRMINSTAR:
  3174. *repeat_opcode = OP_CRPOSSTAR;
  3175. break;
  3176. case OP_CRPLUS:
  3177. case OP_CRMINPLUS:
  3178. *repeat_opcode = OP_CRPOSPLUS;
  3179. break;
  3180. case OP_CRQUERY:
  3181. case OP_CRMINQUERY:
  3182. *repeat_opcode = OP_CRPOSQUERY;
  3183. break;
  3184. case OP_CRRANGE:
  3185. case OP_CRMINRANGE:
  3186. *repeat_opcode = OP_CRPOSRANGE;
  3187. break;
  3188. }
  3189. }
  3190. }
  3191. c = *code;
  3192. }
  3193. switch(c)
  3194. {
  3195. case OP_END:
  3196. return;
  3197. case OP_TYPESTAR:
  3198. case OP_TYPEMINSTAR:
  3199. case OP_TYPEPLUS:
  3200. case OP_TYPEMINPLUS:
  3201. case OP_TYPEQUERY:
  3202. case OP_TYPEMINQUERY:
  3203. case OP_TYPEPOSSTAR:
  3204. case OP_TYPEPOSPLUS:
  3205. case OP_TYPEPOSQUERY:
  3206. if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
  3207. break;
  3208. case OP_TYPEUPTO:
  3209. case OP_TYPEMINUPTO:
  3210. case OP_TYPEEXACT:
  3211. case OP_TYPEPOSUPTO:
  3212. if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
  3213. code += 2;
  3214. break;
  3215. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  3216. case OP_XCLASS:
  3217. code += GET(code, 1);
  3218. break;
  3219. #endif
  3220. case OP_MARK:
  3221. case OP_PRUNE_ARG:
  3222. case OP_SKIP_ARG:
  3223. case OP_THEN_ARG:
  3224. code += code[1];
  3225. break;
  3226. }
  3227. /* Add in the fixed length from the table */
  3228. code += PRIV(OP_lengths)[c];
  3229. /* In UTF-8 mode, opcodes that are followed by a character may be followed by
  3230. a multi-byte character. The length in the table is a minimum, so we have to
  3231. arrange to skip the extra bytes. */
  3232. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  3233. if (utf) switch(c)
  3234. {
  3235. case OP_CHAR:
  3236. case OP_CHARI:
  3237. case OP_NOT:
  3238. case OP_NOTI:
  3239. case OP_STAR:
  3240. case OP_MINSTAR:
  3241. case OP_PLUS:
  3242. case OP_MINPLUS:
  3243. case OP_QUERY:
  3244. case OP_MINQUERY:
  3245. case OP_UPTO:
  3246. case OP_MINUPTO:
  3247. case OP_EXACT:
  3248. case OP_POSSTAR:
  3249. case OP_POSPLUS:
  3250. case OP_POSQUERY:
  3251. case OP_POSUPTO:
  3252. case OP_STARI:
  3253. case OP_MINSTARI:
  3254. case OP_PLUSI:
  3255. case OP_MINPLUSI:
  3256. case OP_QUERYI:
  3257. case OP_MINQUERYI:
  3258. case OP_UPTOI:
  3259. case OP_MINUPTOI:
  3260. case OP_EXACTI:
  3261. case OP_POSSTARI:
  3262. case OP_POSPLUSI:
  3263. case OP_POSQUERYI:
  3264. case OP_POSUPTOI:
  3265. case OP_NOTSTAR:
  3266. case OP_NOTMINSTAR:
  3267. case OP_NOTPLUS:
  3268. case OP_NOTMINPLUS:
  3269. case OP_NOTQUERY:
  3270. case OP_NOTMINQUERY:
  3271. case OP_NOTUPTO:
  3272. case OP_NOTMINUPTO:
  3273. case OP_NOTEXACT:
  3274. case OP_NOTPOSSTAR:
  3275. case OP_NOTPOSPLUS:
  3276. case OP_NOTPOSQUERY:
  3277. case OP_NOTPOSUPTO:
  3278. case OP_NOTSTARI:
  3279. case OP_NOTMINSTARI:
  3280. case OP_NOTPLUSI:
  3281. case OP_NOTMINPLUSI:
  3282. case OP_NOTQUERYI:
  3283. case OP_NOTMINQUERYI:
  3284. case OP_NOTUPTOI:
  3285. case OP_NOTMINUPTOI:
  3286. case OP_NOTEXACTI:
  3287. case OP_NOTPOSSTARI:
  3288. case OP_NOTPOSPLUSI:
  3289. case OP_NOTPOSQUERYI:
  3290. case OP_NOTPOSUPTOI:
  3291. if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
  3292. break;
  3293. }
  3294. #else
  3295. (void)(utf); /* Keep compiler happy by referencing function argument */
  3296. #endif
  3297. }
  3298. }
  3299. /*************************************************
  3300. * Check for POSIX class syntax *
  3301. *************************************************/
  3302. /* This function is called when the sequence "[:" or "[." or "[=" is
  3303. encountered in a character class. It checks whether this is followed by a
  3304. sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
  3305. reach an unescaped ']' without the special preceding character, return FALSE.
  3306. Originally, this function only recognized a sequence of letters between the
  3307. terminators, but it seems that Perl recognizes any sequence of characters,
  3308. though of course unknown POSIX names are subsequently rejected. Perl gives an
  3309. "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
  3310. didn't consider this to be a POSIX class. Likewise for [:1234:].
  3311. The problem in trying to be exactly like Perl is in the handling of escapes. We
  3312. have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
  3313. class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
  3314. below handles the special cases \\ and \], but does not try to do any other
  3315. escape processing. This makes it different from Perl for cases such as
  3316. [:l\ower:] where Perl recognizes it as the POSIX class "lower" but PCRE does
  3317. not recognize "l\ower". This is a lesser evil than not diagnosing bad classes
  3318. when Perl does, I think.
  3319. A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
  3320. It seems that the appearance of a nested POSIX class supersedes an apparent
  3321. external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
  3322. a digit.
  3323. In Perl, unescaped square brackets may also appear as part of class names. For
  3324. example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
  3325. [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
  3326. seem right at all. PCRE does not allow closing square brackets in POSIX class
  3327. names.
  3328. Arguments:
  3329. ptr pointer to the initial [
  3330. endptr where to return the end pointer
  3331. Returns: TRUE or FALSE
  3332. */
  3333. static BOOL
  3334. check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
  3335. {
  3336. pcre_uchar terminator; /* Don't combine these lines; the Solaris cc */
  3337. terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */
  3338. for (++ptr; *ptr != CHAR_NULL; ptr++)
  3339. {
  3340. if (*ptr == CHAR_BACKSLASH &&
  3341. (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET ||
  3342. ptr[1] == CHAR_BACKSLASH))
  3343. ptr++;
  3344. else if ((*ptr == CHAR_LEFT_SQUARE_BRACKET && ptr[1] == terminator) ||
  3345. *ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
  3346. else if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
  3347. {
  3348. *endptr = ptr;
  3349. return TRUE;
  3350. }
  3351. }
  3352. return FALSE;
  3353. }
  3354. /*************************************************
  3355. * Check POSIX class name *
  3356. *************************************************/
  3357. /* This function is called to check the name given in a POSIX-style class entry
  3358. such as [:alnum:].
  3359. Arguments:
  3360. ptr points to the first letter
  3361. len the length of the name
  3362. Returns: a value representing the name, or -1 if unknown
  3363. */
  3364. static int
  3365. check_posix_name(const pcre_uchar *ptr, int len)
  3366. {
  3367. const char *pn = posix_names;
  3368. register int yield = 0;
  3369. while (posix_name_lengths[yield] != 0)
  3370. {
  3371. if (len == posix_name_lengths[yield] &&
  3372. STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
  3373. pn += posix_name_lengths[yield] + 1;
  3374. yield++;
  3375. }
  3376. return -1;
  3377. }
  3378. /*************************************************
  3379. * Adjust OP_RECURSE items in repeated group *
  3380. *************************************************/
  3381. /* OP_RECURSE items contain an offset from the start of the regex to the group
  3382. that is referenced. This means that groups can be replicated for fixed
  3383. repetition simply by copying (because the recursion is allowed to refer to
  3384. earlier groups that are outside the current group). However, when a group is
  3385. optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
  3386. inserted before it, after it has been compiled. This means that any OP_RECURSE
  3387. items within it that refer to the group itself or any contained groups have to
  3388. have their offsets adjusted. That one of the jobs of this function. Before it
  3389. is called, the partially compiled regex must be temporarily terminated with
  3390. OP_END.
  3391. This function has been extended to cope with forward references for recursions
  3392. and subroutine calls. It must check the list of such references for the
  3393. group we are dealing with. If it finds that one of the recursions in the
  3394. current group is on this list, it does not adjust the value in the reference
  3395. (which is a group number). After the group has been scanned, all the offsets in
  3396. the forward reference list for the group are adjusted.
  3397. Arguments:
  3398. group points to the start of the group
  3399. adjust the amount by which the group is to be moved
  3400. utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
  3401. cd contains pointers to tables etc.
  3402. save_hwm_offset the hwm forward reference offset at the start of the group
  3403. Returns: nothing
  3404. */
  3405. static void
  3406. adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
  3407. size_t save_hwm_offset)
  3408. {
  3409. int offset;
  3410. pcre_uchar *hc;
  3411. pcre_uchar *ptr = group;
  3412. while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
  3413. {
  3414. for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; hc < cd->hwm;
  3415. hc += LINK_SIZE)
  3416. {
  3417. offset = (int)GET(hc, 0);
  3418. if (cd->start_code + offset == ptr + 1) break;
  3419. }
  3420. /* If we have not found this recursion on the forward reference list, adjust
  3421. the recursion's offset if it's after the start of this group. */
  3422. if (hc >= cd->hwm)
  3423. {
  3424. offset = (int)GET(ptr, 1);
  3425. if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
  3426. }
  3427. ptr += 1 + LINK_SIZE;
  3428. }
  3429. /* Now adjust all forward reference offsets for the group. */
  3430. for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; hc < cd->hwm;
  3431. hc += LINK_SIZE)
  3432. {
  3433. offset = (int)GET(hc, 0);
  3434. PUT(hc, 0, offset + adjust);
  3435. }
  3436. }
  3437. /*************************************************
  3438. * Insert an automatic callout point *
  3439. *************************************************/
  3440. /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
  3441. callout points before each pattern item.
  3442. Arguments:
  3443. code current code pointer
  3444. ptr current pattern pointer
  3445. cd pointers to tables etc
  3446. Returns: new code pointer
  3447. */
  3448. static pcre_uchar *
  3449. auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
  3450. {
  3451. *code++ = OP_CALLOUT;
  3452. *code++ = 255;
  3453. PUT(code, 0, (int)(ptr - cd->start_pattern)); /* Pattern offset */
  3454. PUT(code, LINK_SIZE, 0); /* Default length */
  3455. return code + 2 * LINK_SIZE;
  3456. }
  3457. /*************************************************
  3458. * Complete a callout item *
  3459. *************************************************/
  3460. /* A callout item contains the length of the next item in the pattern, which
  3461. we can't fill in till after we have reached the relevant point. This is used
  3462. for both automatic and manual callouts.
  3463. Arguments:
  3464. previous_callout points to previous callout item
  3465. ptr current pattern pointer
  3466. cd pointers to tables etc
  3467. Returns: nothing
  3468. */
  3469. static void
  3470. complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
  3471. {
  3472. int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
  3473. PUT(previous_callout, 2 + LINK_SIZE, length);
  3474. }
  3475. #ifdef SUPPORT_UCP
  3476. /*************************************************
  3477. * Get othercase range *
  3478. *************************************************/
  3479. /* This function is passed the start and end of a class range, in UTF-8 mode
  3480. with UCP support. It searches up the characters, looking for ranges of
  3481. characters in the "other" case. Each call returns the next one, updating the
  3482. start address. A character with multiple other cases is returned on its own
  3483. with a special return value.
  3484. Arguments:
  3485. cptr points to starting character value; updated
  3486. d end value
  3487. ocptr where to put start of othercase range
  3488. odptr where to put end of othercase range
  3489. Yield: -1 when no more
  3490. 0 when a range is returned
  3491. >0 the CASESET offset for char with multiple other cases
  3492. in this case, ocptr contains the original
  3493. */
  3494. static int
  3495. get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
  3496. pcre_uint32 *odptr)
  3497. {
  3498. pcre_uint32 c, othercase, next;
  3499. unsigned int co;
  3500. /* Find the first character that has an other case. If it has multiple other
  3501. cases, return its case offset value. */
  3502. for (c = *cptr; c <= d; c++)
  3503. {
  3504. if ((co = UCD_CASESET(c)) != 0)
  3505. {
  3506. *ocptr = c++; /* Character that has the set */
  3507. *cptr = c; /* Rest of input range */
  3508. return (int)co;
  3509. }
  3510. if ((othercase = UCD_OTHERCASE(c)) != c) break;
  3511. }
  3512. if (c > d) return -1; /* Reached end of range */
  3513. /* Found a character that has a single other case. Search for the end of the
  3514. range, which is either the end of the input range, or a character that has zero
  3515. or more than one other cases. */
  3516. *ocptr = othercase;
  3517. next = othercase + 1;
  3518. for (++c; c <= d; c++)
  3519. {
  3520. if ((co = UCD_CASESET(c)) != 0 || UCD_OTHERCASE(c) != next) break;
  3521. next++;
  3522. }
  3523. *odptr = next - 1; /* End of othercase range */
  3524. *cptr = c; /* Rest of input range */
  3525. return 0;
  3526. }
  3527. #endif /* SUPPORT_UCP */
  3528. /*************************************************
  3529. * Add a character or range to a class *
  3530. *************************************************/
  3531. /* This function packages up the logic of adding a character or range of
  3532. characters to a class. The character values in the arguments will be within the
  3533. valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
  3534. mutually recursive with the function immediately below.
  3535. Arguments:
  3536. classbits the bit map for characters < 256
  3537. uchardptr points to the pointer for extra data
  3538. options the options word
  3539. cd contains pointers to tables etc.
  3540. start start of range character
  3541. end end of range character
  3542. Returns: the number of < 256 characters added
  3543. the pointer to extra data is updated
  3544. */
  3545. static int
  3546. add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
  3547. compile_data *cd, pcre_uint32 start, pcre_uint32 end)
  3548. {
  3549. pcre_uint32 c;
  3550. pcre_uint32 classbits_end = (end <= 0xff ? end : 0xff);
  3551. int n8 = 0;
  3552. /* If caseless matching is required, scan the range and process alternate
  3553. cases. In Unicode, there are 8-bit characters that have alternate cases that
  3554. are greater than 255 and vice-versa. Sometimes we can just extend the original
  3555. range. */
  3556. if ((options & PCRE_CASELESS) != 0)
  3557. {
  3558. #ifdef SUPPORT_UCP
  3559. if ((options & PCRE_UTF8) != 0)
  3560. {
  3561. int rc;
  3562. pcre_uint32 oc, od;
  3563. options &= ~PCRE_CASELESS; /* Remove for recursive calls */
  3564. c = start;
  3565. while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
  3566. {
  3567. /* Handle a single character that has more than one other case. */
  3568. if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
  3569. PRIV(ucd_caseless_sets) + rc, oc);
  3570. /* Do nothing if the other case range is within the original range. */
  3571. else if (oc >= start && od <= end) continue;
  3572. /* Extend the original range if there is overlap, noting that if oc < c, we
  3573. can't have od > end because a subrange is always shorter than the basic
  3574. range. Otherwise, use a recursive call to add the additional range. */
  3575. else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
  3576. else if (od > end && oc <= end + 1)
  3577. {
  3578. end = od; /* Extend upwards */
  3579. if (end > classbits_end) classbits_end = (end <= 0xff ? end : 0xff);
  3580. }
  3581. else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
  3582. }
  3583. }
  3584. else
  3585. #endif /* SUPPORT_UCP */
  3586. /* Not UTF-mode, or no UCP */
  3587. for (c = start; c <= classbits_end; c++)
  3588. {
  3589. SETBIT(classbits, cd->fcc[c]);
  3590. n8++;
  3591. }
  3592. }
  3593. /* Now handle the original range. Adjust the final value according to the bit
  3594. length - this means that the same lists of (e.g.) horizontal spaces can be used
  3595. in all cases. */
  3596. #if defined COMPILE_PCRE8
  3597. #ifdef SUPPORT_UTF
  3598. if ((options & PCRE_UTF8) == 0)
  3599. #endif
  3600. if (end > 0xff) end = 0xff;
  3601. #elif defined COMPILE_PCRE16
  3602. #ifdef SUPPORT_UTF
  3603. if ((options & PCRE_UTF16) == 0)
  3604. #endif
  3605. if (end > 0xffff) end = 0xffff;
  3606. #endif /* COMPILE_PCRE[8|16] */
  3607. /* Use the bitmap for characters < 256. Otherwise use extra data.*/
  3608. for (c = start; c <= classbits_end; c++)
  3609. {
  3610. /* Regardless of start, c will always be <= 255. */
  3611. SETBIT(classbits, c);
  3612. n8++;
  3613. }
  3614. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  3615. if (start <= 0xff) start = 0xff + 1;
  3616. if (end >= start)
  3617. {
  3618. pcre_uchar *uchardata = *uchardptr;
  3619. #ifdef SUPPORT_UTF
  3620. if ((options & PCRE_UTF8) != 0) /* All UTFs use the same flag bit */
  3621. {
  3622. if (start < end)
  3623. {
  3624. *uchardata++ = XCL_RANGE;
  3625. uchardata += PRIV(ord2utf)(start, uchardata);
  3626. uchardata += PRIV(ord2utf)(end, uchardata);
  3627. }
  3628. else if (start == end)
  3629. {
  3630. *uchardata++ = XCL_SINGLE;
  3631. uchardata += PRIV(ord2utf)(start, uchardata);
  3632. }
  3633. }
  3634. else
  3635. #endif /* SUPPORT_UTF */
  3636. /* Without UTF support, character values are constrained by the bit length,
  3637. and can only be > 256 for 16-bit and 32-bit libraries. */
  3638. #ifdef COMPILE_PCRE8
  3639. {}
  3640. #else
  3641. if (start < end)
  3642. {
  3643. *uchardata++ = XCL_RANGE;
  3644. *uchardata++ = start;
  3645. *uchardata++ = end;
  3646. }
  3647. else if (start == end)
  3648. {
  3649. *uchardata++ = XCL_SINGLE;
  3650. *uchardata++ = start;
  3651. }
  3652. #endif
  3653. *uchardptr = uchardata; /* Updata extra data pointer */
  3654. }
  3655. #endif /* SUPPORT_UTF || !COMPILE_PCRE8 */
  3656. return n8; /* Number of 8-bit characters */
  3657. }
  3658. /*************************************************
  3659. * Add a list of characters to a class *
  3660. *************************************************/
  3661. /* This function is used for adding a list of case-equivalent characters to a
  3662. class, and also for adding a list of horizontal or vertical whitespace. If the
  3663. list is in order (which it should be), ranges of characters are detected and
  3664. handled appropriately. This function is mutually recursive with the function
  3665. above.
  3666. Arguments:
  3667. classbits the bit map for characters < 256
  3668. uchardptr points to the pointer for extra data
  3669. options the options word
  3670. cd contains pointers to tables etc.
  3671. p points to row of 32-bit values, terminated by NOTACHAR
  3672. except character to omit; this is used when adding lists of
  3673. case-equivalent characters to avoid including the one we
  3674. already know about
  3675. Returns: the number of < 256 characters added
  3676. the pointer to extra data is updated
  3677. */
  3678. static int
  3679. add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
  3680. compile_data *cd, const pcre_uint32 *p, unsigned int except)
  3681. {
  3682. int n8 = 0;
  3683. while (p[0] < NOTACHAR)
  3684. {
  3685. int n = 0;
  3686. if (p[0] != except)
  3687. {
  3688. while(p[n+1] == p[0] + n + 1) n++;
  3689. n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
  3690. }
  3691. p += n + 1;
  3692. }
  3693. return n8;
  3694. }
  3695. /*************************************************
  3696. * Add characters not in a list to a class *
  3697. *************************************************/
  3698. /* This function is used for adding the complement of a list of horizontal or
  3699. vertical whitespace to a class. The list must be in order.
  3700. Arguments:
  3701. classbits the bit map for characters < 256
  3702. uchardptr points to the pointer for extra data
  3703. options the options word
  3704. cd contains pointers to tables etc.
  3705. p points to row of 32-bit values, terminated by NOTACHAR
  3706. Returns: the number of < 256 characters added
  3707. the pointer to extra data is updated
  3708. */
  3709. static int
  3710. add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
  3711. int options, compile_data *cd, const pcre_uint32 *p)
  3712. {
  3713. BOOL utf = (options & PCRE_UTF8) != 0;
  3714. int n8 = 0;
  3715. if (p[0] > 0)
  3716. n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
  3717. while (p[0] < NOTACHAR)
  3718. {
  3719. while (p[1] == p[0] + 1) p++;
  3720. n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
  3721. (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
  3722. p++;
  3723. }
  3724. return n8;
  3725. }
  3726. /*************************************************
  3727. * Compile one branch *
  3728. *************************************************/
  3729. /* Scan the pattern, compiling it into the a vector. If the options are
  3730. changed during the branch, the pointer is used to change the external options
  3731. bits. This function is used during the pre-compile phase when we are trying
  3732. to find out the amount of memory needed, as well as during the real compile
  3733. phase. The value of lengthptr distinguishes the two phases.
  3734. Arguments:
  3735. optionsptr pointer to the option bits
  3736. codeptr points to the pointer to the current code point
  3737. ptrptr points to the current pattern pointer
  3738. errorcodeptr points to error code variable
  3739. firstcharptr place to put the first required character
  3740. firstcharflagsptr place to put the first character flags, or a negative number
  3741. reqcharptr place to put the last required character
  3742. reqcharflagsptr place to put the last required character flags, or a negative number
  3743. bcptr points to current branch chain
  3744. cond_depth conditional nesting depth
  3745. cd contains pointers to tables etc.
  3746. lengthptr NULL during the real compile phase
  3747. points to length accumulator during pre-compile phase
  3748. Returns: TRUE on success
  3749. FALSE, with *errorcodeptr set non-zero on error
  3750. */
  3751. static BOOL
  3752. compile_branch(int *optionsptr, pcre_uchar **codeptr,
  3753. const pcre_uchar **ptrptr, int *errorcodeptr,
  3754. pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
  3755. pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
  3756. branch_chain *bcptr, int cond_depth,
  3757. compile_data *cd, int *lengthptr)
  3758. {
  3759. int repeat_type, op_type;
  3760. int repeat_min = 0, repeat_max = 0; /* To please picky compilers */
  3761. int bravalue = 0;
  3762. int greedy_default, greedy_non_default;
  3763. pcre_uint32 firstchar, reqchar;
  3764. pcre_int32 firstcharflags, reqcharflags;
  3765. pcre_uint32 zeroreqchar, zerofirstchar;
  3766. pcre_int32 zeroreqcharflags, zerofirstcharflags;
  3767. pcre_int32 req_caseopt, reqvary, tempreqvary;
  3768. int options = *optionsptr; /* May change dynamically */
  3769. int after_manual_callout = 0;
  3770. int length_prevgroup = 0;
  3771. register pcre_uint32 c;
  3772. int escape;
  3773. register pcre_uchar *code = *codeptr;
  3774. pcre_uchar *last_code = code;
  3775. pcre_uchar *orig_code = code;
  3776. pcre_uchar *tempcode;
  3777. BOOL inescq = FALSE;
  3778. BOOL groupsetfirstchar = FALSE;
  3779. const pcre_uchar *ptr = *ptrptr;
  3780. const pcre_uchar *tempptr;
  3781. const pcre_uchar *nestptr = NULL;
  3782. pcre_uchar *previous = NULL;
  3783. pcre_uchar *previous_callout = NULL;
  3784. size_t item_hwm_offset = 0;
  3785. pcre_uint8 classbits[32];
  3786. /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
  3787. must not do this for other options (e.g. PCRE_EXTENDED) because they may change
  3788. dynamically as we process the pattern. */
  3789. #ifdef SUPPORT_UTF
  3790. /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
  3791. BOOL utf = (options & PCRE_UTF8) != 0;
  3792. #ifndef COMPILE_PCRE32
  3793. pcre_uchar utf_chars[6];
  3794. #endif
  3795. #else
  3796. BOOL utf = FALSE;
  3797. #endif
  3798. /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
  3799. class_uchardata always so that it can be passed to add_to_class() always,
  3800. though it will not be used in non-UTF 8-bit cases. This avoids having to supply
  3801. alternative calls for the different cases. */
  3802. pcre_uchar *class_uchardata;
  3803. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  3804. BOOL xclass;
  3805. pcre_uchar *class_uchardata_base;
  3806. #endif
  3807. #ifdef PCRE_DEBUG
  3808. if (lengthptr != NULL) DPRINTF((">> start branch\n"));
  3809. #endif
  3810. /* Set up the default and non-default settings for greediness */
  3811. greedy_default = ((options & PCRE_UNGREEDY) != 0);
  3812. greedy_non_default = greedy_default ^ 1;
  3813. /* Initialize no first byte, no required byte. REQ_UNSET means "no char
  3814. matching encountered yet". It gets changed to REQ_NONE if we hit something that
  3815. matches a non-fixed char first char; reqchar just remains unset if we never
  3816. find one.
  3817. When we hit a repeat whose minimum is zero, we may have to adjust these values
  3818. to take the zero repeat into account. This is implemented by setting them to
  3819. zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
  3820. item types that can be repeated set these backoff variables appropriately. */
  3821. firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
  3822. firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
  3823. /* The variable req_caseopt contains either the REQ_CASELESS value
  3824. or zero, according to the current setting of the caseless flag. The
  3825. REQ_CASELESS leaves the lower 28 bit empty. It is added into the
  3826. firstchar or reqchar variables to record the case status of the
  3827. value. This is used only for ASCII characters. */
  3828. req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
  3829. /* Switch on next character until the end of the branch */
  3830. for (;; ptr++)
  3831. {
  3832. BOOL negate_class;
  3833. BOOL should_flip_negation;
  3834. BOOL possessive_quantifier;
  3835. BOOL is_quantifier;
  3836. BOOL is_recurse;
  3837. BOOL reset_bracount;
  3838. int class_has_8bitchar;
  3839. int class_one_char;
  3840. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  3841. BOOL xclass_has_prop;
  3842. #endif
  3843. int newoptions;
  3844. int recno;
  3845. int refsign;
  3846. int skipbytes;
  3847. pcre_uint32 subreqchar, subfirstchar;
  3848. pcre_int32 subreqcharflags, subfirstcharflags;
  3849. int terminator;
  3850. unsigned int mclength;
  3851. unsigned int tempbracount;
  3852. pcre_uint32 ec;
  3853. pcre_uchar mcbuffer[8];
  3854. /* Come here to restart the loop without advancing the pointer. */
  3855. REDO_LOOP:
  3856. /* Get next character in the pattern */
  3857. c = *ptr;
  3858. /* If we are at the end of a nested substitution, revert to the outer level
  3859. string. Nesting only happens one level deep. */
  3860. if (c == CHAR_NULL && nestptr != NULL)
  3861. {
  3862. ptr = nestptr;
  3863. nestptr = NULL;
  3864. c = *ptr;
  3865. }
  3866. /* If we are in the pre-compile phase, accumulate the length used for the
  3867. previous cycle of this loop. */
  3868. if (lengthptr != NULL)
  3869. {
  3870. #ifdef PCRE_DEBUG
  3871. if (code > cd->hwm) cd->hwm = code; /* High water info */
  3872. #endif
  3873. if (code > cd->start_workspace + cd->workspace_size -
  3874. WORK_SIZE_SAFETY_MARGIN) /* Check for overrun */
  3875. {
  3876. *errorcodeptr = (code >= cd->start_workspace + cd->workspace_size)?
  3877. ERR52 : ERR87;
  3878. goto FAILED;
  3879. }
  3880. /* There is at least one situation where code goes backwards: this is the
  3881. case of a zero quantifier after a class (e.g. [ab]{0}). At compile time,
  3882. the class is simply eliminated. However, it is created first, so we have to
  3883. allow memory for it. Therefore, don't ever reduce the length at this point.
  3884. */
  3885. if (code < last_code) code = last_code;
  3886. /* Paranoid check for integer overflow */
  3887. if (OFLOW_MAX - *lengthptr < code - last_code)
  3888. {
  3889. *errorcodeptr = ERR20;
  3890. goto FAILED;
  3891. }
  3892. *lengthptr += (int)(code - last_code);
  3893. DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
  3894. (int)(code - last_code), c, c));
  3895. /* If "previous" is set and it is not at the start of the work space, move
  3896. it back to there, in order to avoid filling up the work space. Otherwise,
  3897. if "previous" is NULL, reset the current code pointer to the start. */
  3898. if (previous != NULL)
  3899. {
  3900. if (previous > orig_code)
  3901. {
  3902. memmove(orig_code, previous, IN_UCHARS(code - previous));
  3903. code -= previous - orig_code;
  3904. previous = orig_code;
  3905. }
  3906. }
  3907. else code = orig_code;
  3908. /* Remember where this code item starts so we can pick up the length
  3909. next time round. */
  3910. last_code = code;
  3911. }
  3912. /* In the real compile phase, just check the workspace used by the forward
  3913. reference list. */
  3914. else if (cd->hwm > cd->start_workspace + cd->workspace_size)
  3915. {
  3916. *errorcodeptr = ERR52;
  3917. goto FAILED;
  3918. }
  3919. /* If in \Q...\E, check for the end; if not, we have a literal. Otherwise an
  3920. isolated \E is ignored. */
  3921. if (c != CHAR_NULL)
  3922. {
  3923. if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
  3924. {
  3925. inescq = FALSE;
  3926. ptr++;
  3927. continue;
  3928. }
  3929. else if (inescq)
  3930. {
  3931. if (previous_callout != NULL)
  3932. {
  3933. if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
  3934. complete_callout(previous_callout, ptr, cd);
  3935. previous_callout = NULL;
  3936. }
  3937. if ((options & PCRE_AUTO_CALLOUT) != 0)
  3938. {
  3939. previous_callout = code;
  3940. code = auto_callout(code, ptr, cd);
  3941. }
  3942. goto NORMAL_CHAR;
  3943. }
  3944. /* Check for the start of a \Q...\E sequence. We must do this here rather
  3945. than later in case it is immediately followed by \E, which turns it into a
  3946. "do nothing" sequence. */
  3947. if (c == CHAR_BACKSLASH && ptr[1] == CHAR_Q)
  3948. {
  3949. inescq = TRUE;
  3950. ptr++;
  3951. continue;
  3952. }
  3953. }
  3954. /* In extended mode, skip white space and comments. */
  3955. if ((options & PCRE_EXTENDED) != 0)
  3956. {
  3957. const pcre_uchar *wscptr = ptr;
  3958. while (MAX_255(c) && (cd->ctypes[c] & ctype_space) != 0) c = *(++ptr);
  3959. if (c == CHAR_NUMBER_SIGN)
  3960. {
  3961. ptr++;
  3962. while (*ptr != CHAR_NULL)
  3963. {
  3964. if (IS_NEWLINE(ptr)) /* For non-fixed-length newline cases, */
  3965. { /* IS_NEWLINE sets cd->nllen. */
  3966. ptr += cd->nllen;
  3967. break;
  3968. }
  3969. ptr++;
  3970. #ifdef SUPPORT_UTF
  3971. if (utf) FORWARDCHAR(ptr);
  3972. #endif
  3973. }
  3974. }
  3975. /* If we skipped any characters, restart the loop. Otherwise, we didn't see
  3976. a comment. */
  3977. if (ptr > wscptr) goto REDO_LOOP;
  3978. }
  3979. /* Skip over (?# comments. We need to do this here because we want to know if
  3980. the next thing is a quantifier, and these comments may come between an item
  3981. and its quantifier. */
  3982. if (c == CHAR_LEFT_PARENTHESIS && ptr[1] == CHAR_QUESTION_MARK &&
  3983. ptr[2] == CHAR_NUMBER_SIGN)
  3984. {
  3985. ptr += 3;
  3986. while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
  3987. if (*ptr == CHAR_NULL)
  3988. {
  3989. *errorcodeptr = ERR18;
  3990. goto FAILED;
  3991. }
  3992. continue;
  3993. }
  3994. /* See if the next thing is a quantifier. */
  3995. is_quantifier =
  3996. c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
  3997. (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
  3998. /* Fill in length of a previous callout, except when the next thing is a
  3999. quantifier or when processing a property substitution string in UCP mode. */
  4000. if (!is_quantifier && previous_callout != NULL && nestptr == NULL &&
  4001. after_manual_callout-- <= 0)
  4002. {
  4003. if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
  4004. complete_callout(previous_callout, ptr, cd);
  4005. previous_callout = NULL;
  4006. }
  4007. /* Create auto callout, except for quantifiers, or while processing property
  4008. strings that are substituted for \w etc in UCP mode. */
  4009. if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL)
  4010. {
  4011. previous_callout = code;
  4012. code = auto_callout(code, ptr, cd);
  4013. }
  4014. /* Process the next pattern item. */
  4015. switch(c)
  4016. {
  4017. /* ===================================================================*/
  4018. case CHAR_NULL: /* The branch terminates at string end */
  4019. case CHAR_VERTICAL_LINE: /* or | or ) */
  4020. case CHAR_RIGHT_PARENTHESIS:
  4021. *firstcharptr = firstchar;
  4022. *firstcharflagsptr = firstcharflags;
  4023. *reqcharptr = reqchar;
  4024. *reqcharflagsptr = reqcharflags;
  4025. *codeptr = code;
  4026. *ptrptr = ptr;
  4027. if (lengthptr != NULL)
  4028. {
  4029. if (OFLOW_MAX - *lengthptr < code - last_code)
  4030. {
  4031. *errorcodeptr = ERR20;
  4032. goto FAILED;
  4033. }
  4034. *lengthptr += (int)(code - last_code); /* To include callout length */
  4035. DPRINTF((">> end branch\n"));
  4036. }
  4037. return TRUE;
  4038. /* ===================================================================*/
  4039. /* Handle single-character metacharacters. In multiline mode, ^ disables
  4040. the setting of any following char as a first character. */
  4041. case CHAR_CIRCUMFLEX_ACCENT:
  4042. previous = NULL;
  4043. if ((options & PCRE_MULTILINE) != 0)
  4044. {
  4045. if (firstcharflags == REQ_UNSET)
  4046. zerofirstcharflags = firstcharflags = REQ_NONE;
  4047. *code++ = OP_CIRCM;
  4048. }
  4049. else *code++ = OP_CIRC;
  4050. break;
  4051. case CHAR_DOLLAR_SIGN:
  4052. previous = NULL;
  4053. *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
  4054. break;
  4055. /* There can never be a first char if '.' is first, whatever happens about
  4056. repeats. The value of reqchar doesn't change either. */
  4057. case CHAR_DOT:
  4058. if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
  4059. zerofirstchar = firstchar;
  4060. zerofirstcharflags = firstcharflags;
  4061. zeroreqchar = reqchar;
  4062. zeroreqcharflags = reqcharflags;
  4063. previous = code;
  4064. item_hwm_offset = cd->hwm - cd->start_workspace;
  4065. *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
  4066. break;
  4067. /* ===================================================================*/
  4068. /* Character classes. If the included characters are all < 256, we build a
  4069. 32-byte bitmap of the permitted characters, except in the special case
  4070. where there is only one such character. For negated classes, we build the
  4071. map as usual, then invert it at the end. However, we use a different opcode
  4072. so that data characters > 255 can be handled correctly.
  4073. If the class contains characters outside the 0-255 range, a different
  4074. opcode is compiled. It may optionally have a bit map for characters < 256,
  4075. but those above are are explicitly listed afterwards. A flag byte tells
  4076. whether the bitmap is present, and whether this is a negated class or not.
  4077. In JavaScript compatibility mode, an isolated ']' causes an error. In
  4078. default (Perl) mode, it is treated as a data character. */
  4079. case CHAR_RIGHT_SQUARE_BRACKET:
  4080. if ((cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
  4081. {
  4082. *errorcodeptr = ERR64;
  4083. goto FAILED;
  4084. }
  4085. goto NORMAL_CHAR;
  4086. /* In another (POSIX) regex library, the ugly syntax [[:<:]] and [[:>:]] is
  4087. used for "start of word" and "end of word". As these are otherwise illegal
  4088. sequences, we don't break anything by recognizing them. They are replaced
  4089. by \b(?=\w) and \b(?<=\w) respectively. Sequences like [a[:<:]] are
  4090. erroneous and are handled by the normal code below. */
  4091. case CHAR_LEFT_SQUARE_BRACKET:
  4092. if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_STARTWORD, 6) == 0)
  4093. {
  4094. nestptr = ptr + 7;
  4095. ptr = sub_start_of_word;
  4096. goto REDO_LOOP;
  4097. }
  4098. if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_ENDWORD, 6) == 0)
  4099. {
  4100. nestptr = ptr + 7;
  4101. ptr = sub_end_of_word;
  4102. goto REDO_LOOP;
  4103. }
  4104. /* Handle a real character class. */
  4105. previous = code;
  4106. item_hwm_offset = cd->hwm - cd->start_workspace;
  4107. /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
  4108. they are encountered at the top level, so we'll do that too. */
  4109. if ((ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
  4110. ptr[1] == CHAR_EQUALS_SIGN) &&
  4111. check_posix_syntax(ptr, &tempptr))
  4112. {
  4113. *errorcodeptr = (ptr[1] == CHAR_COLON)? ERR13 : ERR31;
  4114. goto FAILED;
  4115. }
  4116. /* If the first character is '^', set the negation flag and skip it. Also,
  4117. if the first few characters (either before or after ^) are \Q\E or \E we
  4118. skip them too. This makes for compatibility with Perl. */
  4119. negate_class = FALSE;
  4120. for (;;)
  4121. {
  4122. c = *(++ptr);
  4123. if (c == CHAR_BACKSLASH)
  4124. {
  4125. if (ptr[1] == CHAR_E)
  4126. ptr++;
  4127. else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0)
  4128. ptr += 3;
  4129. else
  4130. break;
  4131. }
  4132. else if (!negate_class && c == CHAR_CIRCUMFLEX_ACCENT)
  4133. negate_class = TRUE;
  4134. else break;
  4135. }
  4136. /* Empty classes are allowed in JavaScript compatibility mode. Otherwise,
  4137. an initial ']' is taken as a data character -- the code below handles
  4138. that. In JS mode, [] must always fail, so generate OP_FAIL, whereas
  4139. [^] must match any character, so generate OP_ALLANY. */
  4140. if (c == CHAR_RIGHT_SQUARE_BRACKET &&
  4141. (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
  4142. {
  4143. *code++ = negate_class? OP_ALLANY : OP_FAIL;
  4144. if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
  4145. zerofirstchar = firstchar;
  4146. zerofirstcharflags = firstcharflags;
  4147. break;
  4148. }
  4149. /* If a class contains a negative special such as \S, we need to flip the
  4150. negation flag at the end, so that support for characters > 255 works
  4151. correctly (they are all included in the class). */
  4152. should_flip_negation = FALSE;
  4153. /* Extended class (xclass) will be used when characters > 255
  4154. might match. */
  4155. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  4156. xclass = FALSE;
  4157. class_uchardata = code + LINK_SIZE + 2; /* For XCLASS items */
  4158. class_uchardata_base = class_uchardata; /* Save the start */
  4159. #endif
  4160. /* For optimization purposes, we track some properties of the class:
  4161. class_has_8bitchar will be non-zero if the class contains at least one <
  4162. 256 character; class_one_char will be 1 if the class contains just one
  4163. character; xclass_has_prop will be TRUE if unicode property checks
  4164. are present in the class. */
  4165. class_has_8bitchar = 0;
  4166. class_one_char = 0;
  4167. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  4168. xclass_has_prop = FALSE;
  4169. #endif
  4170. /* Initialize the 32-char bit map to all zeros. We build the map in a
  4171. temporary bit of memory, in case the class contains fewer than two
  4172. 8-bit characters because in that case the compiled code doesn't use the bit
  4173. map. */
  4174. memset(classbits, 0, 32 * sizeof(pcre_uint8));
  4175. /* Process characters until ] is reached. By writing this as a "do" it
  4176. means that an initial ] is taken as a data character. At the start of the
  4177. loop, c contains the first byte of the character. */
  4178. if (c != CHAR_NULL) do
  4179. {
  4180. const pcre_uchar *oldptr;
  4181. #ifdef SUPPORT_UTF
  4182. if (utf && HAS_EXTRALEN(c))
  4183. { /* Braces are required because the */
  4184. GETCHARLEN(c, ptr, ptr); /* macro generates multiple statements */
  4185. }
  4186. #endif
  4187. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  4188. /* In the pre-compile phase, accumulate the length of any extra
  4189. data and reset the pointer. This is so that very large classes that
  4190. contain a zillion > 255 characters no longer overwrite the work space
  4191. (which is on the stack). We have to remember that there was XCLASS data,
  4192. however. */
  4193. if (class_uchardata > class_uchardata_base) xclass = TRUE;
  4194. if (lengthptr != NULL && class_uchardata > class_uchardata_base)
  4195. {
  4196. *lengthptr += (int)(class_uchardata - class_uchardata_base);
  4197. class_uchardata = class_uchardata_base;
  4198. }
  4199. #endif
  4200. /* Inside \Q...\E everything is literal except \E */
  4201. if (inescq)
  4202. {
  4203. if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E) /* If we are at \E */
  4204. {
  4205. inescq = FALSE; /* Reset literal state */
  4206. ptr++; /* Skip the 'E' */
  4207. continue; /* Carry on with next */
  4208. }
  4209. goto CHECK_RANGE; /* Could be range if \E follows */
  4210. }
  4211. /* Handle POSIX class names. Perl allows a negation extension of the
  4212. form [:^name:]. A square bracket that doesn't match the syntax is
  4213. treated as a literal. We also recognize the POSIX constructions
  4214. [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
  4215. 5.6 and 5.8 do. */
  4216. if (c == CHAR_LEFT_SQUARE_BRACKET &&
  4217. (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
  4218. ptr[1] == CHAR_EQUALS_SIGN) && check_posix_syntax(ptr, &tempptr))
  4219. {
  4220. BOOL local_negate = FALSE;
  4221. int posix_class, taboffset, tabopt;
  4222. register const pcre_uint8 *cbits = cd->cbits;
  4223. pcre_uint8 pbits[32];
  4224. if (ptr[1] != CHAR_COLON)
  4225. {
  4226. *errorcodeptr = ERR31;
  4227. goto FAILED;
  4228. }
  4229. ptr += 2;
  4230. if (*ptr == CHAR_CIRCUMFLEX_ACCENT)
  4231. {
  4232. local_negate = TRUE;
  4233. should_flip_negation = TRUE; /* Note negative special */
  4234. ptr++;
  4235. }
  4236. posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
  4237. if (posix_class < 0)
  4238. {
  4239. *errorcodeptr = ERR30;
  4240. goto FAILED;
  4241. }
  4242. /* If matching is caseless, upper and lower are converted to
  4243. alpha. This relies on the fact that the class table starts with
  4244. alpha, lower, upper as the first 3 entries. */
  4245. if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
  4246. posix_class = 0;
  4247. /* When PCRE_UCP is set, some of the POSIX classes are converted to
  4248. different escape sequences that use Unicode properties \p or \P. Others
  4249. that are not available via \p or \P generate XCL_PROP/XCL_NOTPROP
  4250. directly. */
  4251. #ifdef SUPPORT_UCP
  4252. if ((options & PCRE_UCP) != 0)
  4253. {
  4254. unsigned int ptype = 0;
  4255. int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
  4256. /* The posix_substitutes table specifies which POSIX classes can be
  4257. converted to \p or \P items. */
  4258. if (posix_substitutes[pc] != NULL)
  4259. {
  4260. nestptr = tempptr + 1;
  4261. ptr = posix_substitutes[pc] - 1;
  4262. continue;
  4263. }
  4264. /* There are three other classes that generate special property calls
  4265. that are recognized only in an XCLASS. */
  4266. else switch(posix_class)
  4267. {
  4268. case PC_GRAPH:
  4269. ptype = PT_PXGRAPH;
  4270. /* Fall through */
  4271. case PC_PRINT:
  4272. if (ptype == 0) ptype = PT_PXPRINT;
  4273. /* Fall through */
  4274. case PC_PUNCT:
  4275. if (ptype == 0) ptype = PT_PXPUNCT;
  4276. *class_uchardata++ = local_negate? XCL_NOTPROP : XCL_PROP;
  4277. *class_uchardata++ = ptype;
  4278. *class_uchardata++ = 0;
  4279. xclass_has_prop = TRUE;
  4280. ptr = tempptr + 1;
  4281. continue;
  4282. /* For the other POSIX classes (ascii, cntrl, xdigit) we are going
  4283. to fall through to the non-UCP case and build a bit map for
  4284. characters with code points less than 256. If we are in a negated
  4285. POSIX class, characters with code points greater than 255 must
  4286. either all match or all not match. In the special case where we
  4287. have not yet generated any xclass data, and this is the final item
  4288. in the overall class, we need do nothing: later on, the opcode
  4289. OP_NCLASS will be used to indicate that characters greater than 255
  4290. are acceptable. If we have already seen an xclass item or one may
  4291. follow (we have to assume that it might if this is not the end of
  4292. the class), explicitly list all wide codepoints, which will then
  4293. either not match or match, depending on whether the class is or is
  4294. not negated. */
  4295. default:
  4296. if (local_negate &&
  4297. (xclass || tempptr[2] != CHAR_RIGHT_SQUARE_BRACKET))
  4298. {
  4299. *class_uchardata++ = XCL_RANGE;
  4300. class_uchardata += PRIV(ord2utf)(0x100, class_uchardata);
  4301. class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);
  4302. }
  4303. break;
  4304. }
  4305. }
  4306. #endif
  4307. /* In the non-UCP case, or when UCP makes no difference, we build the
  4308. bit map for the POSIX class in a chunk of local store because we may be
  4309. adding and subtracting from it, and we don't want to subtract bits that
  4310. may be in the main map already. At the end we or the result into the
  4311. bit map that is being built. */
  4312. posix_class *= 3;
  4313. /* Copy in the first table (always present) */
  4314. memcpy(pbits, cbits + posix_class_maps[posix_class],
  4315. 32 * sizeof(pcre_uint8));
  4316. /* If there is a second table, add or remove it as required. */
  4317. taboffset = posix_class_maps[posix_class + 1];
  4318. tabopt = posix_class_maps[posix_class + 2];
  4319. if (taboffset >= 0)
  4320. {
  4321. if (tabopt >= 0)
  4322. for (c = 0; c < 32; c++) pbits[c] |= cbits[c + taboffset];
  4323. else
  4324. for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
  4325. }
  4326. /* Now see if we need to remove any special characters. An option
  4327. value of 1 removes vertical space and 2 removes underscore. */
  4328. if (tabopt < 0) tabopt = -tabopt;
  4329. if (tabopt == 1) pbits[1] &= ~0x3c;
  4330. else if (tabopt == 2) pbits[11] &= 0x7f;
  4331. /* Add the POSIX table or its complement into the main table that is
  4332. being built and we are done. */
  4333. if (local_negate)
  4334. for (c = 0; c < 32; c++) classbits[c] |= ~pbits[c];
  4335. else
  4336. for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
  4337. ptr = tempptr + 1;
  4338. /* Every class contains at least one < 256 character. */
  4339. class_has_8bitchar = 1;
  4340. /* Every class contains at least two characters. */
  4341. class_one_char = 2;
  4342. continue; /* End of POSIX syntax handling */
  4343. }
  4344. /* Backslash may introduce a single character, or it may introduce one
  4345. of the specials, which just set a flag. The sequence \b is a special
  4346. case. Inside a class (and only there) it is treated as backspace. We
  4347. assume that other escapes have more than one character in them, so
  4348. speculatively set both class_has_8bitchar and class_one_char bigger
  4349. than one. Unrecognized escapes fall through and are either treated
  4350. as literal characters (by default), or are faulted if
  4351. PCRE_EXTRA is set. */
  4352. if (c == CHAR_BACKSLASH)
  4353. {
  4354. escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
  4355. TRUE);
  4356. if (*errorcodeptr != 0) goto FAILED;
  4357. if (escape == 0) c = ec;
  4358. else if (escape == ESC_b) c = CHAR_BS; /* \b is backspace in a class */
  4359. else if (escape == ESC_N) /* \N is not supported in a class */
  4360. {
  4361. *errorcodeptr = ERR71;
  4362. goto FAILED;
  4363. }
  4364. else if (escape == ESC_Q) /* Handle start of quoted string */
  4365. {
  4366. if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
  4367. {
  4368. ptr += 2; /* avoid empty string */
  4369. }
  4370. else inescq = TRUE;
  4371. continue;
  4372. }
  4373. else if (escape == ESC_E) continue; /* Ignore orphan \E */
  4374. else
  4375. {
  4376. register const pcre_uint8 *cbits = cd->cbits;
  4377. /* Every class contains at least two < 256 characters. */
  4378. class_has_8bitchar++;
  4379. /* Every class contains at least two characters. */
  4380. class_one_char += 2;
  4381. switch (escape)
  4382. {
  4383. #ifdef SUPPORT_UCP
  4384. case ESC_du: /* These are the values given for \d etc */
  4385. case ESC_DU: /* when PCRE_UCP is set. We replace the */
  4386. case ESC_wu: /* escape sequence with an appropriate \p */
  4387. case ESC_WU: /* or \P to test Unicode properties instead */
  4388. case ESC_su: /* of the default ASCII testing. */
  4389. case ESC_SU:
  4390. nestptr = ptr;
  4391. ptr = substitutes[escape - ESC_DU] - 1; /* Just before substitute */
  4392. class_has_8bitchar--; /* Undo! */
  4393. continue;
  4394. #endif
  4395. case ESC_d:
  4396. for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
  4397. continue;
  4398. case ESC_D:
  4399. should_flip_negation = TRUE;
  4400. for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];
  4401. continue;
  4402. case ESC_w:
  4403. for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_word];
  4404. continue;
  4405. case ESC_W:
  4406. should_flip_negation = TRUE;
  4407. for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
  4408. continue;
  4409. /* Perl 5.004 onwards omitted VT from \s, but restored it at Perl
  4410. 5.18. Before PCRE 8.34, we had to preserve the VT bit if it was
  4411. previously set by something earlier in the character class.
  4412. Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
  4413. we could just adjust the appropriate bit. From PCRE 8.34 we no
  4414. longer treat \s and \S specially. */
  4415. case ESC_s:
  4416. for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
  4417. continue;
  4418. case ESC_S:
  4419. should_flip_negation = TRUE;
  4420. for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
  4421. continue;
  4422. /* The rest apply in both UCP and non-UCP cases. */
  4423. case ESC_h:
  4424. (void)add_list_to_class(classbits, &class_uchardata, options, cd,
  4425. PRIV(hspace_list), NOTACHAR);
  4426. continue;
  4427. case ESC_H:
  4428. (void)add_not_list_to_class(classbits, &class_uchardata, options,
  4429. cd, PRIV(hspace_list));
  4430. continue;
  4431. case ESC_v:
  4432. (void)add_list_to_class(classbits, &class_uchardata, options, cd,
  4433. PRIV(vspace_list), NOTACHAR);
  4434. continue;
  4435. case ESC_V:
  4436. (void)add_not_list_to_class(classbits, &class_uchardata, options,
  4437. cd, PRIV(vspace_list));
  4438. continue;
  4439. case ESC_p:
  4440. case ESC_P:
  4441. #ifdef SUPPORT_UCP
  4442. {
  4443. BOOL negated;
  4444. unsigned int ptype = 0, pdata = 0;
  4445. if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
  4446. goto FAILED;
  4447. *class_uchardata++ = ((escape == ESC_p) != negated)?
  4448. XCL_PROP : XCL_NOTPROP;
  4449. *class_uchardata++ = ptype;
  4450. *class_uchardata++ = pdata;
  4451. xclass_has_prop = TRUE;
  4452. class_has_8bitchar--; /* Undo! */
  4453. continue;
  4454. }
  4455. #else
  4456. *errorcodeptr = ERR45;
  4457. goto FAILED;
  4458. #endif
  4459. /* Unrecognized escapes are faulted if PCRE is running in its
  4460. strict mode. By default, for compatibility with Perl, they are
  4461. treated as literals. */
  4462. default:
  4463. if ((options & PCRE_EXTRA) != 0)
  4464. {
  4465. *errorcodeptr = ERR7;
  4466. goto FAILED;
  4467. }
  4468. class_has_8bitchar--; /* Undo the speculative increase. */
  4469. class_one_char -= 2; /* Undo the speculative increase. */
  4470. c = *ptr; /* Get the final character and fall through */
  4471. break;
  4472. }
  4473. }
  4474. /* Fall through if the escape just defined a single character (c >= 0).
  4475. This may be greater than 256. */
  4476. escape = 0;
  4477. } /* End of backslash handling */
  4478. /* A character may be followed by '-' to form a range. However, Perl does
  4479. not permit ']' to be the end of the range. A '-' character at the end is
  4480. treated as a literal. Perl ignores orphaned \E sequences entirely. The
  4481. code for handling \Q and \E is messy. */
  4482. CHECK_RANGE:
  4483. while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
  4484. {
  4485. inescq = FALSE;
  4486. ptr += 2;
  4487. }
  4488. oldptr = ptr;
  4489. /* Remember if \r or \n were explicitly used */
  4490. if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
  4491. /* Check for range */
  4492. if (!inescq && ptr[1] == CHAR_MINUS)
  4493. {
  4494. pcre_uint32 d;
  4495. ptr += 2;
  4496. while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;
  4497. /* If we hit \Q (not followed by \E) at this point, go into escaped
  4498. mode. */
  4499. while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_Q)
  4500. {
  4501. ptr += 2;
  4502. if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E)
  4503. { ptr += 2; continue; }
  4504. inescq = TRUE;
  4505. break;
  4506. }
  4507. /* Minus (hyphen) at the end of a class is treated as a literal, so put
  4508. back the pointer and jump to handle the character that preceded it. */
  4509. if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
  4510. {
  4511. ptr = oldptr;
  4512. goto CLASS_SINGLE_CHARACTER;
  4513. }
  4514. /* Otherwise, we have a potential range; pick up the next character */
  4515. #ifdef SUPPORT_UTF
  4516. if (utf)
  4517. { /* Braces are required because the */
  4518. GETCHARLEN(d, ptr, ptr); /* macro generates multiple statements */
  4519. }
  4520. else
  4521. #endif
  4522. d = *ptr; /* Not UTF-8 mode */
  4523. /* The second part of a range can be a single-character escape
  4524. sequence, but not any of the other escapes. Perl treats a hyphen as a
  4525. literal in such circumstances. However, in Perl's warning mode, a
  4526. warning is given, so PCRE now faults it as it is almost certainly a
  4527. mistake on the user's part. */
  4528. if (!inescq)
  4529. {
  4530. if (d == CHAR_BACKSLASH)
  4531. {
  4532. int descape;
  4533. descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
  4534. if (*errorcodeptr != 0) goto FAILED;
  4535. /* 0 means a character was put into d; \b is backspace; any other
  4536. special causes an error. */
  4537. if (descape != 0)
  4538. {
  4539. if (descape == ESC_b) d = CHAR_BS; else
  4540. {
  4541. *errorcodeptr = ERR83;
  4542. goto FAILED;
  4543. }
  4544. }
  4545. }
  4546. /* A hyphen followed by a POSIX class is treated in the same way. */
  4547. else if (d == CHAR_LEFT_SQUARE_BRACKET &&
  4548. (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
  4549. ptr[1] == CHAR_EQUALS_SIGN) &&
  4550. check_posix_syntax(ptr, &tempptr))
  4551. {
  4552. *errorcodeptr = ERR83;
  4553. goto FAILED;
  4554. }
  4555. }
  4556. /* Check that the two values are in the correct order. Optimize
  4557. one-character ranges. */
  4558. if (d < c)
  4559. {
  4560. *errorcodeptr = ERR8;
  4561. goto FAILED;
  4562. }
  4563. if (d == c) goto CLASS_SINGLE_CHARACTER; /* A few lines below */
  4564. /* We have found a character range, so single character optimizations
  4565. cannot be done anymore. Any value greater than 1 indicates that there
  4566. is more than one character. */
  4567. class_one_char = 2;
  4568. /* Remember an explicit \r or \n, and add the range to the class. */
  4569. if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
  4570. class_has_8bitchar +=
  4571. add_to_class(classbits, &class_uchardata, options, cd, c, d);
  4572. continue; /* Go get the next char in the class */
  4573. }
  4574. /* Handle a single character - we can get here for a normal non-escape
  4575. char, or after \ that introduces a single character or for an apparent
  4576. range that isn't. Only the value 1 matters for class_one_char, so don't
  4577. increase it if it is already 2 or more ... just in case there's a class
  4578. with a zillion characters in it. */
  4579. CLASS_SINGLE_CHARACTER:
  4580. if (class_one_char < 2) class_one_char++;
  4581. /* If xclass_has_prop is false and class_one_char is 1, we have the first
  4582. single character in the class, and there have been no prior ranges, or
  4583. XCLASS items generated by escapes. If this is the final character in the
  4584. class, we can optimize by turning the item into a 1-character OP_CHAR[I]
  4585. if it's positive, or OP_NOT[I] if it's negative. In the positive case, it
  4586. can cause firstchar to be set. Otherwise, there can be no first char if
  4587. this item is first, whatever repeat count may follow. In the case of
  4588. reqchar, save the previous value for reinstating. */
  4589. if (!inescq &&
  4590. #ifdef SUPPORT_UCP
  4591. !xclass_has_prop &&
  4592. #endif
  4593. class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
  4594. {
  4595. ptr++;
  4596. zeroreqchar = reqchar;
  4597. zeroreqcharflags = reqcharflags;
  4598. if (negate_class)
  4599. {
  4600. #ifdef SUPPORT_UCP
  4601. int d;
  4602. #endif
  4603. if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
  4604. zerofirstchar = firstchar;
  4605. zerofirstcharflags = firstcharflags;
  4606. /* For caseless UTF-8 mode when UCP support is available, check
  4607. whether this character has more than one other case. If so, generate
  4608. a special OP_NOTPROP item instead of OP_NOTI. */
  4609. #ifdef SUPPORT_UCP
  4610. if (utf && (options & PCRE_CASELESS) != 0 &&
  4611. (d = UCD_CASESET(c)) != 0)
  4612. {
  4613. *code++ = OP_NOTPROP;
  4614. *code++ = PT_CLIST;
  4615. *code++ = d;
  4616. }
  4617. else
  4618. #endif
  4619. /* Char has only one other case, or UCP not available */
  4620. {
  4621. *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
  4622. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  4623. if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
  4624. code += PRIV(ord2utf)(c, code);
  4625. else
  4626. #endif
  4627. *code++ = c;
  4628. }
  4629. /* We are finished with this character class */
  4630. goto END_CLASS;
  4631. }
  4632. /* For a single, positive character, get the value into mcbuffer, and
  4633. then we can handle this with the normal one-character code. */
  4634. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  4635. if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
  4636. mclength = PRIV(ord2utf)(c, mcbuffer);
  4637. else
  4638. #endif
  4639. {
  4640. mcbuffer[0] = c;
  4641. mclength = 1;
  4642. }
  4643. goto ONE_CHAR;
  4644. } /* End of 1-char optimization */
  4645. /* There is more than one character in the class, or an XCLASS item
  4646. has been generated. Add this character to the class. */
  4647. class_has_8bitchar +=
  4648. add_to_class(classbits, &class_uchardata, options, cd, c, c);
  4649. }
  4650. /* Loop until ']' reached. This "while" is the end of the "do" far above.
  4651. If we are at the end of an internal nested string, revert to the outer
  4652. string. */
  4653. while (((c = *(++ptr)) != CHAR_NULL ||
  4654. (nestptr != NULL &&
  4655. (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != CHAR_NULL)) &&
  4656. (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
  4657. /* Check for missing terminating ']' */
  4658. if (c == CHAR_NULL)
  4659. {
  4660. *errorcodeptr = ERR6;
  4661. goto FAILED;
  4662. }
  4663. /* We will need an XCLASS if data has been placed in class_uchardata. In
  4664. the second phase this is a sufficient test. However, in the pre-compile
  4665. phase, class_uchardata gets emptied to prevent workspace overflow, so it
  4666. only if the very last character in the class needs XCLASS will it contain
  4667. anything at this point. For this reason, xclass gets set TRUE above when
  4668. uchar_classdata is emptied, and that's why this code is the way it is here
  4669. instead of just doing a test on class_uchardata below. */
  4670. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  4671. if (class_uchardata > class_uchardata_base) xclass = TRUE;
  4672. #endif
  4673. /* If this is the first thing in the branch, there can be no first char
  4674. setting, whatever the repeat count. Any reqchar setting must remain
  4675. unchanged after any kind of repeat. */
  4676. if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
  4677. zerofirstchar = firstchar;
  4678. zerofirstcharflags = firstcharflags;
  4679. zeroreqchar = reqchar;
  4680. zeroreqcharflags = reqcharflags;
  4681. /* If there are characters with values > 255, we have to compile an
  4682. extended class, with its own opcode, unless there was a negated special
  4683. such as \S in the class, and PCRE_UCP is not set, because in that case all
  4684. characters > 255 are in the class, so any that were explicitly given as
  4685. well can be ignored. If (when there are explicit characters > 255 that must
  4686. be listed) there are no characters < 256, we can omit the bitmap in the
  4687. actual compiled code. */
  4688. #ifdef SUPPORT_UTF
  4689. if (xclass && (xclass_has_prop || !should_flip_negation ||
  4690. (options & PCRE_UCP) != 0))
  4691. #elif !defined COMPILE_PCRE8
  4692. if (xclass && (xclass_has_prop || !should_flip_negation))
  4693. #endif
  4694. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  4695. {
  4696. /* For non-UCP wide characters, in a non-negative class containing \S or
  4697. similar (should_flip_negation is set), all characters greater than 255
  4698. must be in the class. */
  4699. if (
  4700. #if defined COMPILE_PCRE8
  4701. utf &&
  4702. #endif
  4703. should_flip_negation && !negate_class && (options & PCRE_UCP) == 0)
  4704. {
  4705. *class_uchardata++ = XCL_RANGE;
  4706. if (utf) /* Will always be utf in the 8-bit library */
  4707. {
  4708. class_uchardata += PRIV(ord2utf)(0x100, class_uchardata);
  4709. class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);
  4710. }
  4711. else /* Can only happen for the 16-bit & 32-bit libraries */
  4712. {
  4713. #if defined COMPILE_PCRE16
  4714. *class_uchardata++ = 0x100;
  4715. *class_uchardata++ = 0xffffu;
  4716. #elif defined COMPILE_PCRE32
  4717. *class_uchardata++ = 0x100;
  4718. *class_uchardata++ = 0xffffffffu;
  4719. #endif
  4720. }
  4721. }
  4722. *class_uchardata++ = XCL_END; /* Marks the end of extra data */
  4723. *code++ = OP_XCLASS;
  4724. code += LINK_SIZE;
  4725. *code = negate_class? XCL_NOT:0;
  4726. if (xclass_has_prop) *code |= XCL_HASPROP;
  4727. /* If the map is required, move up the extra data to make room for it;
  4728. otherwise just move the code pointer to the end of the extra data. */
  4729. if (class_has_8bitchar > 0)
  4730. {
  4731. *code++ |= XCL_MAP;
  4732. memmove(code + (32 / sizeof(pcre_uchar)), code,
  4733. IN_UCHARS(class_uchardata - code));
  4734. if (negate_class && !xclass_has_prop)
  4735. for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
  4736. memcpy(code, classbits, 32);
  4737. code = class_uchardata + (32 / sizeof(pcre_uchar));
  4738. }
  4739. else code = class_uchardata;
  4740. /* Now fill in the complete length of the item */
  4741. PUT(previous, 1, (int)(code - previous));
  4742. break; /* End of class handling */
  4743. }
  4744. /* Even though any XCLASS list is now discarded, we must allow for
  4745. its memory. */
  4746. if (lengthptr != NULL)
  4747. *lengthptr += (int)(class_uchardata - class_uchardata_base);
  4748. #endif
  4749. /* If there are no characters > 255, or they are all to be included or
  4750. excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
  4751. whole class was negated and whether there were negative specials such as \S
  4752. (non-UCP) in the class. Then copy the 32-byte map into the code vector,
  4753. negating it if necessary. */
  4754. *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
  4755. if (lengthptr == NULL) /* Save time in the pre-compile phase */
  4756. {
  4757. if (negate_class)
  4758. for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
  4759. memcpy(code, classbits, 32);
  4760. }
  4761. code += 32 / sizeof(pcre_uchar);
  4762. END_CLASS:
  4763. break;
  4764. /* ===================================================================*/
  4765. /* Various kinds of repeat; '{' is not necessarily a quantifier, but this
  4766. has been tested above. */
  4767. case CHAR_LEFT_CURLY_BRACKET:
  4768. if (!is_quantifier) goto NORMAL_CHAR;
  4769. ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorcodeptr);
  4770. if (*errorcodeptr != 0) goto FAILED;
  4771. goto REPEAT;
  4772. case CHAR_ASTERISK:
  4773. repeat_min = 0;
  4774. repeat_max = -1;
  4775. goto REPEAT;
  4776. case CHAR_PLUS:
  4777. repeat_min = 1;
  4778. repeat_max = -1;
  4779. goto REPEAT;
  4780. case CHAR_QUESTION_MARK:
  4781. repeat_min = 0;
  4782. repeat_max = 1;
  4783. REPEAT:
  4784. if (previous == NULL)
  4785. {
  4786. *errorcodeptr = ERR9;
  4787. goto FAILED;
  4788. }
  4789. if (repeat_min == 0)
  4790. {
  4791. firstchar = zerofirstchar; /* Adjust for zero repeat */
  4792. firstcharflags = zerofirstcharflags;
  4793. reqchar = zeroreqchar; /* Ditto */
  4794. reqcharflags = zeroreqcharflags;
  4795. }
  4796. /* Remember whether this is a variable length repeat */
  4797. reqvary = (repeat_min == repeat_max)? 0 : REQ_VARY;
  4798. op_type = 0; /* Default single-char op codes */
  4799. possessive_quantifier = FALSE; /* Default not possessive quantifier */
  4800. /* Save start of previous item, in case we have to move it up in order to
  4801. insert something before it. */
  4802. tempcode = previous;
  4803. /* Before checking for a possessive quantifier, we must skip over
  4804. whitespace and comments in extended mode because Perl allows white space at
  4805. this point. */
  4806. if ((options & PCRE_EXTENDED) != 0)
  4807. {
  4808. const pcre_uchar *p = ptr + 1;
  4809. for (;;)
  4810. {
  4811. while (MAX_255(*p) && (cd->ctypes[*p] & ctype_space) != 0) p++;
  4812. if (*p != CHAR_NUMBER_SIGN) break;
  4813. p++;
  4814. while (*p != CHAR_NULL)
  4815. {
  4816. if (IS_NEWLINE(p)) /* For non-fixed-length newline cases, */
  4817. { /* IS_NEWLINE sets cd->nllen. */
  4818. p += cd->nllen;
  4819. break;
  4820. }
  4821. p++;
  4822. #ifdef SUPPORT_UTF
  4823. if (utf) FORWARDCHAR(p);
  4824. #endif
  4825. } /* Loop for comment characters */
  4826. } /* Loop for multiple comments */
  4827. ptr = p - 1; /* Character before the next significant one. */
  4828. }
  4829. /* We also need to skip over (?# comments, which are not dependent on
  4830. extended mode. */
  4831. if (ptr[1] == CHAR_LEFT_PARENTHESIS && ptr[2] == CHAR_QUESTION_MARK &&
  4832. ptr[3] == CHAR_NUMBER_SIGN)
  4833. {
  4834. ptr += 4;
  4835. while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
  4836. if (*ptr == CHAR_NULL)
  4837. {
  4838. *errorcodeptr = ERR18;
  4839. goto FAILED;
  4840. }
  4841. }
  4842. /* If the next character is '+', we have a possessive quantifier. This
  4843. implies greediness, whatever the setting of the PCRE_UNGREEDY option.
  4844. If the next character is '?' this is a minimizing repeat, by default,
  4845. but if PCRE_UNGREEDY is set, it works the other way round. We change the
  4846. repeat type to the non-default. */
  4847. if (ptr[1] == CHAR_PLUS)
  4848. {
  4849. repeat_type = 0; /* Force greedy */
  4850. possessive_quantifier = TRUE;
  4851. ptr++;
  4852. }
  4853. else if (ptr[1] == CHAR_QUESTION_MARK)
  4854. {
  4855. repeat_type = greedy_non_default;
  4856. ptr++;
  4857. }
  4858. else repeat_type = greedy_default;
  4859. /* If previous was a recursion call, wrap it in atomic brackets so that
  4860. previous becomes the atomic group. All recursions were so wrapped in the
  4861. past, but it no longer happens for non-repeated recursions. In fact, the
  4862. repeated ones could be re-implemented independently so as not to need this,
  4863. but for the moment we rely on the code for repeating groups. */
  4864. if (*previous == OP_RECURSE)
  4865. {
  4866. memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
  4867. *previous = OP_ONCE;
  4868. PUT(previous, 1, 2 + 2*LINK_SIZE);
  4869. previous[2 + 2*LINK_SIZE] = OP_KET;
  4870. PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
  4871. code += 2 + 2 * LINK_SIZE;
  4872. length_prevgroup = 3 + 3*LINK_SIZE;
  4873. /* When actually compiling, we need to check whether this was a forward
  4874. reference, and if so, adjust the offset. */
  4875. if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
  4876. {
  4877. int offset = GET(cd->hwm, -LINK_SIZE);
  4878. if (offset == previous + 1 - cd->start_code)
  4879. PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
  4880. }
  4881. }
  4882. /* Now handle repetition for the different types of item. */
  4883. /* If previous was a character or negated character match, abolish the item
  4884. and generate a repeat item instead. If a char item has a minimum of more
  4885. than one, ensure that it is set in reqchar - it might not be if a sequence
  4886. such as x{3} is the first thing in a branch because the x will have gone
  4887. into firstchar instead. */
  4888. if (*previous == OP_CHAR || *previous == OP_CHARI
  4889. || *previous == OP_NOT || *previous == OP_NOTI)
  4890. {
  4891. switch (*previous)
  4892. {
  4893. default: /* Make compiler happy. */
  4894. case OP_CHAR: op_type = OP_STAR - OP_STAR; break;
  4895. case OP_CHARI: op_type = OP_STARI - OP_STAR; break;
  4896. case OP_NOT: op_type = OP_NOTSTAR - OP_STAR; break;
  4897. case OP_NOTI: op_type = OP_NOTSTARI - OP_STAR; break;
  4898. }
  4899. /* Deal with UTF characters that take up more than one character. It's
  4900. easier to write this out separately than try to macrify it. Use c to
  4901. hold the length of the character in bytes, plus UTF_LENGTH to flag that
  4902. it's a length rather than a small character. */
  4903. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  4904. if (utf && NOT_FIRSTCHAR(code[-1]))
  4905. {
  4906. pcre_uchar *lastchar = code - 1;
  4907. BACKCHAR(lastchar);
  4908. c = (int)(code - lastchar); /* Length of UTF-8 character */
  4909. memcpy(utf_chars, lastchar, IN_UCHARS(c)); /* Save the char */
  4910. c |= UTF_LENGTH; /* Flag c as a length */
  4911. }
  4912. else
  4913. #endif /* SUPPORT_UTF */
  4914. /* Handle the case of a single charater - either with no UTF support, or
  4915. with UTF disabled, or for a single character UTF character. */
  4916. {
  4917. c = code[-1];
  4918. if (*previous <= OP_CHARI && repeat_min > 1)
  4919. {
  4920. reqchar = c;
  4921. reqcharflags = req_caseopt | cd->req_varyopt;
  4922. }
  4923. }
  4924. goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */
  4925. }
  4926. /* If previous was a character type match (\d or similar), abolish it and
  4927. create a suitable repeat item. The code is shared with single-character
  4928. repeats by setting op_type to add a suitable offset into repeat_type. Note
  4929. the the Unicode property types will be present only when SUPPORT_UCP is
  4930. defined, but we don't wrap the little bits of code here because it just
  4931. makes it horribly messy. */
  4932. else if (*previous < OP_EODN)
  4933. {
  4934. pcre_uchar *oldcode;
  4935. int prop_type, prop_value;
  4936. op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */
  4937. c = *previous;
  4938. OUTPUT_SINGLE_REPEAT:
  4939. if (*previous == OP_PROP || *previous == OP_NOTPROP)
  4940. {
  4941. prop_type = previous[1];
  4942. prop_value = previous[2];
  4943. }
  4944. else prop_type = prop_value = -1;
  4945. oldcode = code;
  4946. code = previous; /* Usually overwrite previous item */
  4947. /* If the maximum is zero then the minimum must also be zero; Perl allows
  4948. this case, so we do too - by simply omitting the item altogether. */
  4949. if (repeat_max == 0) goto END_REPEAT;
  4950. /* Combine the op_type with the repeat_type */
  4951. repeat_type += op_type;
  4952. /* A minimum of zero is handled either as the special case * or ?, or as
  4953. an UPTO, with the maximum given. */
  4954. if (repeat_min == 0)
  4955. {
  4956. if (repeat_max == -1) *code++ = OP_STAR + repeat_type;
  4957. else if (repeat_max == 1) *code++ = OP_QUERY + repeat_type;
  4958. else
  4959. {
  4960. *code++ = OP_UPTO + repeat_type;
  4961. PUT2INC(code, 0, repeat_max);
  4962. }
  4963. }
  4964. /* A repeat minimum of 1 is optimized into some special cases. If the
  4965. maximum is unlimited, we use OP_PLUS. Otherwise, the original item is
  4966. left in place and, if the maximum is greater than 1, we use OP_UPTO with
  4967. one less than the maximum. */
  4968. else if (repeat_min == 1)
  4969. {
  4970. if (repeat_max == -1)
  4971. *code++ = OP_PLUS + repeat_type;
  4972. else
  4973. {
  4974. code = oldcode; /* leave previous item in place */
  4975. if (repeat_max == 1) goto END_REPEAT;
  4976. *code++ = OP_UPTO + repeat_type;
  4977. PUT2INC(code, 0, repeat_max - 1);
  4978. }
  4979. }
  4980. /* The case {n,n} is just an EXACT, while the general case {n,m} is
  4981. handled as an EXACT followed by an UPTO. */
  4982. else
  4983. {
  4984. *code++ = OP_EXACT + op_type; /* NB EXACT doesn't have repeat_type */
  4985. PUT2INC(code, 0, repeat_min);
  4986. /* If the maximum is unlimited, insert an OP_STAR. Before doing so,
  4987. we have to insert the character for the previous code. For a repeated
  4988. Unicode property match, there are two extra bytes that define the
  4989. required property. In UTF-8 mode, long characters have their length in
  4990. c, with the UTF_LENGTH bit as a flag. */
  4991. if (repeat_max < 0)
  4992. {
  4993. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  4994. if (utf && (c & UTF_LENGTH) != 0)
  4995. {
  4996. memcpy(code, utf_chars, IN_UCHARS(c & 7));
  4997. code += c & 7;
  4998. }
  4999. else
  5000. #endif
  5001. {
  5002. *code++ = c;
  5003. if (prop_type >= 0)
  5004. {
  5005. *code++ = prop_type;
  5006. *code++ = prop_value;
  5007. }
  5008. }
  5009. *code++ = OP_STAR + repeat_type;
  5010. }
  5011. /* Else insert an UPTO if the max is greater than the min, again
  5012. preceded by the character, for the previously inserted code. If the
  5013. UPTO is just for 1 instance, we can use QUERY instead. */
  5014. else if (repeat_max != repeat_min)
  5015. {
  5016. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  5017. if (utf && (c & UTF_LENGTH) != 0)
  5018. {
  5019. memcpy(code, utf_chars, IN_UCHARS(c & 7));
  5020. code += c & 7;
  5021. }
  5022. else
  5023. #endif
  5024. *code++ = c;
  5025. if (prop_type >= 0)
  5026. {
  5027. *code++ = prop_type;
  5028. *code++ = prop_value;
  5029. }
  5030. repeat_max -= repeat_min;
  5031. if (repeat_max == 1)
  5032. {
  5033. *code++ = OP_QUERY + repeat_type;
  5034. }
  5035. else
  5036. {
  5037. *code++ = OP_UPTO + repeat_type;
  5038. PUT2INC(code, 0, repeat_max);
  5039. }
  5040. }
  5041. }
  5042. /* The character or character type itself comes last in all cases. */
  5043. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  5044. if (utf && (c & UTF_LENGTH) != 0)
  5045. {
  5046. memcpy(code, utf_chars, IN_UCHARS(c & 7));
  5047. code += c & 7;
  5048. }
  5049. else
  5050. #endif
  5051. *code++ = c;
  5052. /* For a repeated Unicode property match, there are two extra bytes that
  5053. define the required property. */
  5054. #ifdef SUPPORT_UCP
  5055. if (prop_type >= 0)
  5056. {
  5057. *code++ = prop_type;
  5058. *code++ = prop_value;
  5059. }
  5060. #endif
  5061. }
  5062. /* If previous was a character class or a back reference, we put the repeat
  5063. stuff after it, but just skip the item if the repeat was {0,0}. */
  5064. else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
  5065. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  5066. *previous == OP_XCLASS ||
  5067. #endif
  5068. *previous == OP_REF || *previous == OP_REFI ||
  5069. *previous == OP_DNREF || *previous == OP_DNREFI)
  5070. {
  5071. if (repeat_max == 0)
  5072. {
  5073. code = previous;
  5074. goto END_REPEAT;
  5075. }
  5076. if (repeat_min == 0 && repeat_max == -1)
  5077. *code++ = OP_CRSTAR + repeat_type;
  5078. else if (repeat_min == 1 && repeat_max == -1)
  5079. *code++ = OP_CRPLUS + repeat_type;
  5080. else if (repeat_min == 0 && repeat_max == 1)
  5081. *code++ = OP_CRQUERY + repeat_type;
  5082. else
  5083. {
  5084. *code++ = OP_CRRANGE + repeat_type;
  5085. PUT2INC(code, 0, repeat_min);
  5086. if (repeat_max == -1) repeat_max = 0; /* 2-byte encoding for max */
  5087. PUT2INC(code, 0, repeat_max);
  5088. }
  5089. }
  5090. /* If previous was a bracket group, we may have to replicate it in certain
  5091. cases. Note that at this point we can encounter only the "basic" bracket
  5092. opcodes such as BRA and CBRA, as this is the place where they get converted
  5093. into the more special varieties such as BRAPOS and SBRA. A test for >=
  5094. OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
  5095. ASSERTBACK_NOT, ONCE, ONCE_NC, BRA, BRAPOS, CBRA, CBRAPOS, and COND.
  5096. Originally, PCRE did not allow repetition of assertions, but now it does,
  5097. for Perl compatibility. */
  5098. else if (*previous >= OP_ASSERT && *previous <= OP_COND)
  5099. {
  5100. register int i;
  5101. int len = (int)(code - previous);
  5102. size_t base_hwm_offset = item_hwm_offset;
  5103. pcre_uchar *bralink = NULL;
  5104. pcre_uchar *brazeroptr = NULL;
  5105. /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
  5106. we just ignore the repeat. */
  5107. if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
  5108. goto END_REPEAT;
  5109. /* There is no sense in actually repeating assertions. The only potential
  5110. use of repetition is in cases when the assertion is optional. Therefore,
  5111. if the minimum is greater than zero, just ignore the repeat. If the
  5112. maximum is not zero or one, set it to 1. */
  5113. if (*previous < OP_ONCE) /* Assertion */
  5114. {
  5115. if (repeat_min > 0) goto END_REPEAT;
  5116. if (repeat_max < 0 || repeat_max > 1) repeat_max = 1;
  5117. }
  5118. /* The case of a zero minimum is special because of the need to stick
  5119. OP_BRAZERO in front of it, and because the group appears once in the
  5120. data, whereas in other cases it appears the minimum number of times. For
  5121. this reason, it is simplest to treat this case separately, as otherwise
  5122. the code gets far too messy. There are several special subcases when the
  5123. minimum is zero. */
  5124. if (repeat_min == 0)
  5125. {
  5126. /* If the maximum is also zero, we used to just omit the group from the
  5127. output altogether, like this:
  5128. ** if (repeat_max == 0)
  5129. ** {
  5130. ** code = previous;
  5131. ** goto END_REPEAT;
  5132. ** }
  5133. However, that fails when a group or a subgroup within it is referenced
  5134. as a subroutine from elsewhere in the pattern, so now we stick in
  5135. OP_SKIPZERO in front of it so that it is skipped on execution. As we
  5136. don't have a list of which groups are referenced, we cannot do this
  5137. selectively.
  5138. If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
  5139. and do no more at this point. However, we do need to adjust any
  5140. OP_RECURSE calls inside the group that refer to the group itself or any
  5141. internal or forward referenced group, because the offset is from the
  5142. start of the whole regex. Temporarily terminate the pattern while doing
  5143. this. */
  5144. if (repeat_max <= 1) /* Covers 0, 1, and unlimited */
  5145. {
  5146. *code = OP_END;
  5147. adjust_recurse(previous, 1, utf, cd, item_hwm_offset);
  5148. memmove(previous + 1, previous, IN_UCHARS(len));
  5149. code++;
  5150. if (repeat_max == 0)
  5151. {
  5152. *previous++ = OP_SKIPZERO;
  5153. goto END_REPEAT;
  5154. }
  5155. brazeroptr = previous; /* Save for possessive optimizing */
  5156. *previous++ = OP_BRAZERO + repeat_type;
  5157. }
  5158. /* If the maximum is greater than 1 and limited, we have to replicate
  5159. in a nested fashion, sticking OP_BRAZERO before each set of brackets.
  5160. The first one has to be handled carefully because it's the original
  5161. copy, which has to be moved up. The remainder can be handled by code
  5162. that is common with the non-zero minimum case below. We have to
  5163. adjust the value or repeat_max, since one less copy is required. Once
  5164. again, we may have to adjust any OP_RECURSE calls inside the group. */
  5165. else
  5166. {
  5167. int offset;
  5168. *code = OP_END;
  5169. adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, item_hwm_offset);
  5170. memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));
  5171. code += 2 + LINK_SIZE;
  5172. *previous++ = OP_BRAZERO + repeat_type;
  5173. *previous++ = OP_BRA;
  5174. /* We chain together the bracket offset fields that have to be
  5175. filled in later when the ends of the brackets are reached. */
  5176. offset = (bralink == NULL)? 0 : (int)(previous - bralink);
  5177. bralink = previous;
  5178. PUTINC(previous, 0, offset);
  5179. }
  5180. repeat_max--;
  5181. }
  5182. /* If the minimum is greater than zero, replicate the group as many
  5183. times as necessary, and adjust the maximum to the number of subsequent
  5184. copies that we need. If we set a first char from the group, and didn't
  5185. set a required char, copy the latter from the former. If there are any
  5186. forward reference subroutine calls in the group, there will be entries on
  5187. the workspace list; replicate these with an appropriate increment. */
  5188. else
  5189. {
  5190. if (repeat_min > 1)
  5191. {
  5192. /* In the pre-compile phase, we don't actually do the replication. We
  5193. just adjust the length as if we had. Do some paranoid checks for
  5194. potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
  5195. integer type when available, otherwise double. */
  5196. if (lengthptr != NULL)
  5197. {
  5198. int delta = (repeat_min - 1)*length_prevgroup;
  5199. if ((INT64_OR_DOUBLE)(repeat_min - 1)*
  5200. (INT64_OR_DOUBLE)length_prevgroup >
  5201. (INT64_OR_DOUBLE)INT_MAX ||
  5202. OFLOW_MAX - *lengthptr < delta)
  5203. {
  5204. *errorcodeptr = ERR20;
  5205. goto FAILED;
  5206. }
  5207. *lengthptr += delta;
  5208. }
  5209. /* This is compiling for real. If there is a set first byte for
  5210. the group, and we have not yet set a "required byte", set it. Make
  5211. sure there is enough workspace for copying forward references before
  5212. doing the copy. */
  5213. else
  5214. {
  5215. if (groupsetfirstchar && reqcharflags < 0)
  5216. {
  5217. reqchar = firstchar;
  5218. reqcharflags = firstcharflags;
  5219. }
  5220. for (i = 1; i < repeat_min; i++)
  5221. {
  5222. pcre_uchar *hc;
  5223. size_t this_hwm_offset = cd->hwm - cd->start_workspace;
  5224. memcpy(code, previous, IN_UCHARS(len));
  5225. while (cd->hwm > cd->start_workspace + cd->workspace_size -
  5226. WORK_SIZE_SAFETY_MARGIN -
  5227. (this_hwm_offset - base_hwm_offset))
  5228. {
  5229. *errorcodeptr = expand_workspace(cd);
  5230. if (*errorcodeptr != 0) goto FAILED;
  5231. }
  5232. for (hc = (pcre_uchar *)cd->start_workspace + base_hwm_offset;
  5233. hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset;
  5234. hc += LINK_SIZE)
  5235. {
  5236. PUT(cd->hwm, 0, GET(hc, 0) + len);
  5237. cd->hwm += LINK_SIZE;
  5238. }
  5239. base_hwm_offset = this_hwm_offset;
  5240. code += len;
  5241. }
  5242. }
  5243. }
  5244. if (repeat_max > 0) repeat_max -= repeat_min;
  5245. }
  5246. /* This code is common to both the zero and non-zero minimum cases. If
  5247. the maximum is limited, it replicates the group in a nested fashion,
  5248. remembering the bracket starts on a stack. In the case of a zero minimum,
  5249. the first one was set up above. In all cases the repeat_max now specifies
  5250. the number of additional copies needed. Again, we must remember to
  5251. replicate entries on the forward reference list. */
  5252. if (repeat_max >= 0)
  5253. {
  5254. /* In the pre-compile phase, we don't actually do the replication. We
  5255. just adjust the length as if we had. For each repetition we must add 1
  5256. to the length for BRAZERO and for all but the last repetition we must
  5257. add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
  5258. paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
  5259. a 64-bit integer type when available, otherwise double. */
  5260. if (lengthptr != NULL && repeat_max > 0)
  5261. {
  5262. int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
  5263. 2 - 2*LINK_SIZE; /* Last one doesn't nest */
  5264. if ((INT64_OR_DOUBLE)repeat_max *
  5265. (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
  5266. > (INT64_OR_DOUBLE)INT_MAX ||
  5267. OFLOW_MAX - *lengthptr < delta)
  5268. {
  5269. *errorcodeptr = ERR20;
  5270. goto FAILED;
  5271. }
  5272. *lengthptr += delta;
  5273. }
  5274. /* This is compiling for real */
  5275. else for (i = repeat_max - 1; i >= 0; i--)
  5276. {
  5277. pcre_uchar *hc;
  5278. size_t this_hwm_offset = cd->hwm - cd->start_workspace;
  5279. *code++ = OP_BRAZERO + repeat_type;
  5280. /* All but the final copy start a new nesting, maintaining the
  5281. chain of brackets outstanding. */
  5282. if (i != 0)
  5283. {
  5284. int offset;
  5285. *code++ = OP_BRA;
  5286. offset = (bralink == NULL)? 0 : (int)(code - bralink);
  5287. bralink = code;
  5288. PUTINC(code, 0, offset);
  5289. }
  5290. memcpy(code, previous, IN_UCHARS(len));
  5291. /* Ensure there is enough workspace for forward references before
  5292. copying them. */
  5293. while (cd->hwm > cd->start_workspace + cd->workspace_size -
  5294. WORK_SIZE_SAFETY_MARGIN -
  5295. (this_hwm_offset - base_hwm_offset))
  5296. {
  5297. *errorcodeptr = expand_workspace(cd);
  5298. if (*errorcodeptr != 0) goto FAILED;
  5299. }
  5300. for (hc = (pcre_uchar *)cd->start_workspace + base_hwm_offset;
  5301. hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset;
  5302. hc += LINK_SIZE)
  5303. {
  5304. PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));
  5305. cd->hwm += LINK_SIZE;
  5306. }
  5307. base_hwm_offset = this_hwm_offset;
  5308. code += len;
  5309. }
  5310. /* Now chain through the pending brackets, and fill in their length
  5311. fields (which are holding the chain links pro tem). */
  5312. while (bralink != NULL)
  5313. {
  5314. int oldlinkoffset;
  5315. int offset = (int)(code - bralink + 1);
  5316. pcre_uchar *bra = code - offset;
  5317. oldlinkoffset = GET(bra, 1);
  5318. bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
  5319. *code++ = OP_KET;
  5320. PUTINC(code, 0, offset);
  5321. PUT(bra, 1, offset);
  5322. }
  5323. }
  5324. /* If the maximum is unlimited, set a repeater in the final copy. For
  5325. ONCE brackets, that's all we need to do. However, possessively repeated
  5326. ONCE brackets can be converted into non-capturing brackets, as the
  5327. behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
  5328. deal with possessive ONCEs specially.
  5329. Otherwise, when we are doing the actual compile phase, check to see
  5330. whether this group is one that could match an empty string. If so,
  5331. convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
  5332. that runtime checking can be done. [This check is also applied to ONCE
  5333. groups at runtime, but in a different way.]
  5334. Then, if the quantifier was possessive and the bracket is not a
  5335. conditional, we convert the BRA code to the POS form, and the KET code to
  5336. KETRPOS. (It turns out to be convenient at runtime to detect this kind of
  5337. subpattern at both the start and at the end.) The use of special opcodes
  5338. makes it possible to reduce greatly the stack usage in pcre_exec(). If
  5339. the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
  5340. Then, if the minimum number of matches is 1 or 0, cancel the possessive
  5341. flag so that the default action below, of wrapping everything inside
  5342. atomic brackets, does not happen. When the minimum is greater than 1,
  5343. there will be earlier copies of the group, and so we still have to wrap
  5344. the whole thing. */
  5345. else
  5346. {
  5347. pcre_uchar *ketcode = code - 1 - LINK_SIZE;
  5348. pcre_uchar *bracode = ketcode - GET(ketcode, 1);
  5349. /* Convert possessive ONCE brackets to non-capturing */
  5350. if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&
  5351. possessive_quantifier) *bracode = OP_BRA;
  5352. /* For non-possessive ONCE brackets, all we need to do is to
  5353. set the KET. */
  5354. if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)
  5355. *ketcode = OP_KETRMAX + repeat_type;
  5356. /* Handle non-ONCE brackets and possessive ONCEs (which have been
  5357. converted to non-capturing above). */
  5358. else
  5359. {
  5360. /* In the compile phase, check for empty string matching. */
  5361. if (lengthptr == NULL)
  5362. {
  5363. pcre_uchar *scode = bracode;
  5364. do
  5365. {
  5366. if (could_be_empty_branch(scode, ketcode, utf, cd, NULL))
  5367. {
  5368. *bracode += OP_SBRA - OP_BRA;
  5369. break;
  5370. }
  5371. scode += GET(scode, 1);
  5372. }
  5373. while (*scode == OP_ALT);
  5374. }
  5375. /* A conditional group with only one branch has an implicit empty
  5376. alternative branch. */
  5377. if (*bracode == OP_COND && bracode[GET(bracode,1)] != OP_ALT)
  5378. *bracode = OP_SCOND;
  5379. /* Handle possessive quantifiers. */
  5380. if (possessive_quantifier)
  5381. {
  5382. /* For COND brackets, we wrap the whole thing in a possessively
  5383. repeated non-capturing bracket, because we have not invented POS
  5384. versions of the COND opcodes. Because we are moving code along, we
  5385. must ensure that any pending recursive references are updated. */
  5386. if (*bracode == OP_COND || *bracode == OP_SCOND)
  5387. {
  5388. int nlen = (int)(code - bracode);
  5389. *code = OP_END;
  5390. adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, item_hwm_offset);
  5391. memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));
  5392. code += 1 + LINK_SIZE;
  5393. nlen += 1 + LINK_SIZE;
  5394. *bracode = (*bracode == OP_COND)? OP_BRAPOS : OP_SBRAPOS;
  5395. *code++ = OP_KETRPOS;
  5396. PUTINC(code, 0, nlen);
  5397. PUT(bracode, 1, nlen);
  5398. }
  5399. /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
  5400. else
  5401. {
  5402. *bracode += 1; /* Switch to xxxPOS opcodes */
  5403. *ketcode = OP_KETRPOS;
  5404. }
  5405. /* If the minimum is zero, mark it as possessive, then unset the
  5406. possessive flag when the minimum is 0 or 1. */
  5407. if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
  5408. if (repeat_min < 2) possessive_quantifier = FALSE;
  5409. }
  5410. /* Non-possessive quantifier */
  5411. else *ketcode = OP_KETRMAX + repeat_type;
  5412. }
  5413. }
  5414. }
  5415. /* If previous is OP_FAIL, it was generated by an empty class [] in
  5416. JavaScript mode. The other ways in which OP_FAIL can be generated, that is
  5417. by (*FAIL) or (?!) set previous to NULL, which gives a "nothing to repeat"
  5418. error above. We can just ignore the repeat in JS case. */
  5419. else if (*previous == OP_FAIL) goto END_REPEAT;
  5420. /* Else there's some kind of shambles */
  5421. else
  5422. {
  5423. *errorcodeptr = ERR11;
  5424. goto FAILED;
  5425. }
  5426. /* If the character following a repeat is '+', possessive_quantifier is
  5427. TRUE. For some opcodes, there are special alternative opcodes for this
  5428. case. For anything else, we wrap the entire repeated item inside OP_ONCE
  5429. brackets. Logically, the '+' notation is just syntactic sugar, taken from
  5430. Sun's Java package, but the special opcodes can optimize it.
  5431. Some (but not all) possessively repeated subpatterns have already been
  5432. completely handled in the code just above. For them, possessive_quantifier
  5433. is always FALSE at this stage. Note that the repeated item starts at
  5434. tempcode, not at previous, which might be the first part of a string whose
  5435. (former) last char we repeated. */
  5436. if (possessive_quantifier)
  5437. {
  5438. int len;
  5439. /* Possessifying an EXACT quantifier has no effect, so we can ignore it.
  5440. However, QUERY, STAR, or UPTO may follow (for quantifiers such as {5,6},
  5441. {5,}, or {5,10}). We skip over an EXACT item; if the length of what
  5442. remains is greater than zero, there's a further opcode that can be
  5443. handled. If not, do nothing, leaving the EXACT alone. */
  5444. switch(*tempcode)
  5445. {
  5446. case OP_TYPEEXACT:
  5447. tempcode += PRIV(OP_lengths)[*tempcode] +
  5448. ((tempcode[1 + IMM2_SIZE] == OP_PROP
  5449. || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
  5450. break;
  5451. /* CHAR opcodes are used for exacts whose count is 1. */
  5452. case OP_CHAR:
  5453. case OP_CHARI:
  5454. case OP_NOT:
  5455. case OP_NOTI:
  5456. case OP_EXACT:
  5457. case OP_EXACTI:
  5458. case OP_NOTEXACT:
  5459. case OP_NOTEXACTI:
  5460. tempcode += PRIV(OP_lengths)[*tempcode];
  5461. #ifdef SUPPORT_UTF
  5462. if (utf && HAS_EXTRALEN(tempcode[-1]))
  5463. tempcode += GET_EXTRALEN(tempcode[-1]);
  5464. #endif
  5465. break;
  5466. /* For the class opcodes, the repeat operator appears at the end;
  5467. adjust tempcode to point to it. */
  5468. case OP_CLASS:
  5469. case OP_NCLASS:
  5470. tempcode += 1 + 32/sizeof(pcre_uchar);
  5471. break;
  5472. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  5473. case OP_XCLASS:
  5474. tempcode += GET(tempcode, 1);
  5475. break;
  5476. #endif
  5477. }
  5478. /* If tempcode is equal to code (which points to the end of the repeated
  5479. item), it means we have skipped an EXACT item but there is no following
  5480. QUERY, STAR, or UPTO; the value of len will be 0, and we do nothing. In
  5481. all other cases, tempcode will be pointing to the repeat opcode, and will
  5482. be less than code, so the value of len will be greater than 0. */
  5483. len = (int)(code - tempcode);
  5484. if (len > 0)
  5485. {
  5486. unsigned int repcode = *tempcode;
  5487. /* There is a table for possessifying opcodes, all of which are less
  5488. than OP_CALLOUT. A zero entry means there is no possessified version.
  5489. */
  5490. if (repcode < OP_CALLOUT && opcode_possessify[repcode] > 0)
  5491. *tempcode = opcode_possessify[repcode];
  5492. /* For opcode without a special possessified version, wrap the item in
  5493. ONCE brackets. Because we are moving code along, we must ensure that any
  5494. pending recursive references are updated. */
  5495. else
  5496. {
  5497. *code = OP_END;
  5498. adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, item_hwm_offset);
  5499. memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
  5500. code += 1 + LINK_SIZE;
  5501. len += 1 + LINK_SIZE;
  5502. tempcode[0] = OP_ONCE;
  5503. *code++ = OP_KET;
  5504. PUTINC(code, 0, len);
  5505. PUT(tempcode, 1, len);
  5506. }
  5507. }
  5508. #ifdef NEVER
  5509. if (len > 0) switch (*tempcode)
  5510. {
  5511. case OP_STAR: *tempcode = OP_POSSTAR; break;
  5512. case OP_PLUS: *tempcode = OP_POSPLUS; break;
  5513. case OP_QUERY: *tempcode = OP_POSQUERY; break;
  5514. case OP_UPTO: *tempcode = OP_POSUPTO; break;
  5515. case OP_STARI: *tempcode = OP_POSSTARI; break;
  5516. case OP_PLUSI: *tempcode = OP_POSPLUSI; break;
  5517. case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
  5518. case OP_UPTOI: *tempcode = OP_POSUPTOI; break;
  5519. case OP_NOTSTAR: *tempcode = OP_NOTPOSSTAR; break;
  5520. case OP_NOTPLUS: *tempcode = OP_NOTPOSPLUS; break;
  5521. case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
  5522. case OP_NOTUPTO: *tempcode = OP_NOTPOSUPTO; break;
  5523. case OP_NOTSTARI: *tempcode = OP_NOTPOSSTARI; break;
  5524. case OP_NOTPLUSI: *tempcode = OP_NOTPOSPLUSI; break;
  5525. case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
  5526. case OP_NOTUPTOI: *tempcode = OP_NOTPOSUPTOI; break;
  5527. case OP_TYPESTAR: *tempcode = OP_TYPEPOSSTAR; break;
  5528. case OP_TYPEPLUS: *tempcode = OP_TYPEPOSPLUS; break;
  5529. case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
  5530. case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break;
  5531. case OP_CRSTAR: *tempcode = OP_CRPOSSTAR; break;
  5532. case OP_CRPLUS: *tempcode = OP_CRPOSPLUS; break;
  5533. case OP_CRQUERY: *tempcode = OP_CRPOSQUERY; break;
  5534. case OP_CRRANGE: *tempcode = OP_CRPOSRANGE; break;
  5535. /* Because we are moving code along, we must ensure that any
  5536. pending recursive references are updated. */
  5537. default:
  5538. *code = OP_END;
  5539. adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, item_hwm_offset);
  5540. memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
  5541. code += 1 + LINK_SIZE;
  5542. len += 1 + LINK_SIZE;
  5543. tempcode[0] = OP_ONCE;
  5544. *code++ = OP_KET;
  5545. PUTINC(code, 0, len);
  5546. PUT(tempcode, 1, len);
  5547. break;
  5548. }
  5549. #endif
  5550. }
  5551. /* In all case we no longer have a previous item. We also set the
  5552. "follows varying string" flag for subsequently encountered reqchars if
  5553. it isn't already set and we have just passed a varying length item. */
  5554. END_REPEAT:
  5555. previous = NULL;
  5556. cd->req_varyopt |= reqvary;
  5557. break;
  5558. /* ===================================================================*/
  5559. /* Start of nested parenthesized sub-expression, or comment or lookahead or
  5560. lookbehind or option setting or condition or all the other extended
  5561. parenthesis forms. */
  5562. case CHAR_LEFT_PARENTHESIS:
  5563. ptr++;
  5564. /* Now deal with various "verbs" that can be introduced by '*'. */
  5565. if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':'
  5566. || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0))))
  5567. {
  5568. int i, namelen;
  5569. int arglen = 0;
  5570. const char *vn = verbnames;
  5571. const pcre_uchar *name = ptr + 1;
  5572. const pcre_uchar *arg = NULL;
  5573. previous = NULL;
  5574. ptr++;
  5575. while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
  5576. namelen = (int)(ptr - name);
  5577. /* It appears that Perl allows any characters whatsoever, other than
  5578. a closing parenthesis, to appear in arguments, so we no longer insist on
  5579. letters, digits, and underscores. */
  5580. if (*ptr == CHAR_COLON)
  5581. {
  5582. arg = ++ptr;
  5583. while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
  5584. arglen = (int)(ptr - arg);
  5585. if ((unsigned int)arglen > MAX_MARK)
  5586. {
  5587. *errorcodeptr = ERR75;
  5588. goto FAILED;
  5589. }
  5590. }
  5591. if (*ptr != CHAR_RIGHT_PARENTHESIS)
  5592. {
  5593. *errorcodeptr = ERR60;
  5594. goto FAILED;
  5595. }
  5596. /* Scan the table of verb names */
  5597. for (i = 0; i < verbcount; i++)
  5598. {
  5599. if (namelen == verbs[i].len &&
  5600. STRNCMP_UC_C8(name, vn, namelen) == 0)
  5601. {
  5602. int setverb;
  5603. /* Check for open captures before ACCEPT and convert it to
  5604. ASSERT_ACCEPT if in an assertion. */
  5605. if (verbs[i].op == OP_ACCEPT)
  5606. {
  5607. open_capitem *oc;
  5608. if (arglen != 0)
  5609. {
  5610. *errorcodeptr = ERR59;
  5611. goto FAILED;
  5612. }
  5613. cd->had_accept = TRUE;
  5614. for (oc = cd->open_caps; oc != NULL; oc = oc->next)
  5615. {
  5616. if (lengthptr != NULL)
  5617. {
  5618. #ifdef COMPILE_PCRE8
  5619. *lengthptr += 1 + IMM2_SIZE;
  5620. #elif defined COMPILE_PCRE16
  5621. *lengthptr += 2 + IMM2_SIZE;
  5622. #elif defined COMPILE_PCRE32
  5623. *lengthptr += 4 + IMM2_SIZE;
  5624. #endif
  5625. }
  5626. else
  5627. {
  5628. *code++ = OP_CLOSE;
  5629. PUT2INC(code, 0, oc->number);
  5630. }
  5631. }
  5632. setverb = *code++ =
  5633. (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
  5634. /* Do not set firstchar after *ACCEPT */
  5635. if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
  5636. }
  5637. /* Handle other cases with/without an argument */
  5638. else if (arglen == 0)
  5639. {
  5640. if (verbs[i].op < 0) /* Argument is mandatory */
  5641. {
  5642. *errorcodeptr = ERR66;
  5643. goto FAILED;
  5644. }
  5645. setverb = *code++ = verbs[i].op;
  5646. }
  5647. else
  5648. {
  5649. if (verbs[i].op_arg < 0) /* Argument is forbidden */
  5650. {
  5651. *errorcodeptr = ERR59;
  5652. goto FAILED;
  5653. }
  5654. setverb = *code++ = verbs[i].op_arg;
  5655. if (lengthptr != NULL) /* In pass 1 just add in the length */
  5656. { /* to avoid potential workspace */
  5657. *lengthptr += arglen; /* overflow. */
  5658. *code++ = 0;
  5659. }
  5660. else
  5661. {
  5662. *code++ = arglen;
  5663. memcpy(code, arg, IN_UCHARS(arglen));
  5664. code += arglen;
  5665. }
  5666. *code++ = 0;
  5667. }
  5668. switch (setverb)
  5669. {
  5670. case OP_THEN:
  5671. case OP_THEN_ARG:
  5672. cd->external_flags |= PCRE_HASTHEN;
  5673. break;
  5674. case OP_PRUNE:
  5675. case OP_PRUNE_ARG:
  5676. case OP_SKIP:
  5677. case OP_SKIP_ARG:
  5678. cd->had_pruneorskip = TRUE;
  5679. break;
  5680. }
  5681. break; /* Found verb, exit loop */
  5682. }
  5683. vn += verbs[i].len + 1;
  5684. }
  5685. if (i < verbcount) continue; /* Successfully handled a verb */
  5686. *errorcodeptr = ERR60; /* Verb not recognized */
  5687. goto FAILED;
  5688. }
  5689. /* Initialize for "real" parentheses */
  5690. newoptions = options;
  5691. skipbytes = 0;
  5692. bravalue = OP_CBRA;
  5693. item_hwm_offset = cd->hwm - cd->start_workspace;
  5694. reset_bracount = FALSE;
  5695. /* Deal with the extended parentheses; all are introduced by '?', and the
  5696. appearance of any of them means that this is not a capturing group. */
  5697. if (*ptr == CHAR_QUESTION_MARK)
  5698. {
  5699. int i, set, unset, namelen;
  5700. int *optset;
  5701. const pcre_uchar *name;
  5702. pcre_uchar *slot;
  5703. switch (*(++ptr))
  5704. {
  5705. /* ------------------------------------------------------------ */
  5706. case CHAR_VERTICAL_LINE: /* Reset capture count for each branch */
  5707. reset_bracount = TRUE;
  5708. cd->dupgroups = TRUE; /* Record (?| encountered */
  5709. /* Fall through */
  5710. /* ------------------------------------------------------------ */
  5711. case CHAR_COLON: /* Non-capturing bracket */
  5712. bravalue = OP_BRA;
  5713. ptr++;
  5714. break;
  5715. /* ------------------------------------------------------------ */
  5716. case CHAR_LEFT_PARENTHESIS:
  5717. bravalue = OP_COND; /* Conditional group */
  5718. tempptr = ptr;
  5719. /* A condition can be an assertion, a number (referring to a numbered
  5720. group's having been set), a name (referring to a named group), or 'R',
  5721. referring to recursion. R<digits> and R&name are also permitted for
  5722. recursion tests.
  5723. There are ways of testing a named group: (?(name)) is used by Python;
  5724. Perl 5.10 onwards uses (?(<name>) or (?('name')).
  5725. There is one unfortunate ambiguity, caused by history. 'R' can be the
  5726. recursive thing or the name 'R' (and similarly for 'R' followed by
  5727. digits). We look for a name first; if not found, we try the other case.
  5728. For compatibility with auto-callouts, we allow a callout to be
  5729. specified before a condition that is an assertion. First, check for the
  5730. syntax of a callout; if found, adjust the temporary pointer that is
  5731. used to check for an assertion condition. That's all that is needed! */
  5732. if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
  5733. {
  5734. for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
  5735. if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
  5736. tempptr += i + 1;
  5737. /* tempptr should now be pointing to the opening parenthesis of the
  5738. assertion condition. */
  5739. if (*tempptr != CHAR_LEFT_PARENTHESIS)
  5740. {
  5741. *errorcodeptr = ERR28;
  5742. goto FAILED;
  5743. }
  5744. }
  5745. /* For conditions that are assertions, check the syntax, and then exit
  5746. the switch. This will take control down to where bracketed groups,
  5747. including assertions, are processed. */
  5748. if (tempptr[1] == CHAR_QUESTION_MARK &&
  5749. (tempptr[2] == CHAR_EQUALS_SIGN ||
  5750. tempptr[2] == CHAR_EXCLAMATION_MARK ||
  5751. (tempptr[2] == CHAR_LESS_THAN_SIGN &&
  5752. (tempptr[3] == CHAR_EQUALS_SIGN ||
  5753. tempptr[3] == CHAR_EXCLAMATION_MARK))))
  5754. {
  5755. cd->iscondassert = TRUE;
  5756. break;
  5757. }
  5758. /* Other conditions use OP_CREF/OP_DNCREF/OP_RREF/OP_DNRREF, and all
  5759. need to skip at least 1+IMM2_SIZE bytes at the start of the group. */
  5760. code[1+LINK_SIZE] = OP_CREF;
  5761. skipbytes = 1+IMM2_SIZE;
  5762. refsign = -1; /* => not a number */
  5763. namelen = -1; /* => not a name; must set to avoid warning */
  5764. name = NULL; /* Always set to avoid warning */
  5765. recno = 0; /* Always set to avoid warning */
  5766. /* Check for a test for recursion in a named group. */
  5767. ptr++;
  5768. if (*ptr == CHAR_R && ptr[1] == CHAR_AMPERSAND)
  5769. {
  5770. terminator = -1;
  5771. ptr += 2;
  5772. code[1+LINK_SIZE] = OP_RREF; /* Change the type of test */
  5773. }
  5774. /* Check for a test for a named group's having been set, using the Perl
  5775. syntax (?(<name>) or (?('name'), and also allow for the original PCRE
  5776. syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). */
  5777. else if (*ptr == CHAR_LESS_THAN_SIGN)
  5778. {
  5779. terminator = CHAR_GREATER_THAN_SIGN;
  5780. ptr++;
  5781. }
  5782. else if (*ptr == CHAR_APOSTROPHE)
  5783. {
  5784. terminator = CHAR_APOSTROPHE;
  5785. ptr++;
  5786. }
  5787. else
  5788. {
  5789. terminator = CHAR_NULL;
  5790. if (*ptr == CHAR_MINUS || *ptr == CHAR_PLUS) refsign = *ptr++;
  5791. else if (IS_DIGIT(*ptr)) refsign = 0;
  5792. }
  5793. /* Handle a number */
  5794. if (refsign >= 0)
  5795. {
  5796. while (IS_DIGIT(*ptr))
  5797. {
  5798. if (recno > INT_MAX / 10 - 1) /* Integer overflow */
  5799. {
  5800. while (IS_DIGIT(*ptr)) ptr++;
  5801. *errorcodeptr = ERR61;
  5802. goto FAILED;
  5803. }
  5804. recno = recno * 10 + (int)(*ptr - CHAR_0);
  5805. ptr++;
  5806. }
  5807. }
  5808. /* Otherwise we expect to read a name; anything else is an error. When
  5809. a name is one of a number of duplicates, a different opcode is used and
  5810. it needs more memory. Unfortunately we cannot tell whether a name is a
  5811. duplicate in the first pass, so we have to allow for more memory. */
  5812. else
  5813. {
  5814. if (IS_DIGIT(*ptr))
  5815. {
  5816. *errorcodeptr = ERR84;
  5817. goto FAILED;
  5818. }
  5819. if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_word) == 0)
  5820. {
  5821. *errorcodeptr = ERR28; /* Assertion expected */
  5822. goto FAILED;
  5823. }
  5824. name = ptr++;
  5825. while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
  5826. {
  5827. ptr++;
  5828. }
  5829. namelen = (int)(ptr - name);
  5830. if (lengthptr != NULL) skipbytes += IMM2_SIZE;
  5831. }
  5832. /* Check the terminator */
  5833. if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
  5834. *ptr++ != CHAR_RIGHT_PARENTHESIS)
  5835. {
  5836. ptr--; /* Error offset */
  5837. *errorcodeptr = ERR26; /* Malformed number or name */
  5838. goto FAILED;
  5839. }
  5840. /* Do no further checking in the pre-compile phase. */
  5841. if (lengthptr != NULL) break;
  5842. /* In the real compile we do the work of looking for the actual
  5843. reference. If refsign is not negative, it means we have a number in
  5844. recno. */
  5845. if (refsign >= 0)
  5846. {
  5847. if (recno <= 0)
  5848. {
  5849. *errorcodeptr = ERR35;
  5850. goto FAILED;
  5851. }
  5852. if (refsign != 0) recno = (refsign == CHAR_MINUS)?
  5853. cd->bracount - recno + 1 : recno + cd->bracount;
  5854. if (recno <= 0 || recno > cd->final_bracount)
  5855. {
  5856. *errorcodeptr = ERR15;
  5857. goto FAILED;
  5858. }
  5859. PUT2(code, 2+LINK_SIZE, recno);
  5860. if (recno > cd->top_backref) cd->top_backref = recno;
  5861. break;
  5862. }
  5863. /* Otherwise look for the name. */
  5864. slot = cd->name_table;
  5865. for (i = 0; i < cd->names_found; i++)
  5866. {
  5867. if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 &&
  5868. slot[IMM2_SIZE+namelen] == 0) break;
  5869. slot += cd->name_entry_size;
  5870. }
  5871. /* Found the named subpattern. If the name is duplicated, add one to
  5872. the opcode to change CREF/RREF into DNCREF/DNRREF and insert
  5873. appropriate data values. Otherwise, just insert the unique subpattern
  5874. number. */
  5875. if (i < cd->names_found)
  5876. {
  5877. int offset = i++;
  5878. int count = 1;
  5879. recno = GET2(slot, 0); /* Number from first found */
  5880. if (recno > cd->top_backref) cd->top_backref = recno;
  5881. for (; i < cd->names_found; i++)
  5882. {
  5883. slot += cd->name_entry_size;
  5884. if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) != 0 ||
  5885. (slot+IMM2_SIZE)[namelen] != 0) break;
  5886. count++;
  5887. }
  5888. if (count > 1)
  5889. {
  5890. PUT2(code, 2+LINK_SIZE, offset);
  5891. PUT2(code, 2+LINK_SIZE+IMM2_SIZE, count);
  5892. skipbytes += IMM2_SIZE;
  5893. code[1+LINK_SIZE]++;
  5894. }
  5895. else /* Not a duplicated name */
  5896. {
  5897. PUT2(code, 2+LINK_SIZE, recno);
  5898. }
  5899. }
  5900. /* If terminator == CHAR_NULL it means that the name followed directly
  5901. after the opening parenthesis [e.g. (?(abc)...] and in this case there
  5902. are some further alternatives to try. For the cases where terminator !=
  5903. CHAR_NULL [things like (?(<name>... or (?('name')... or (?(R&name)... ]
  5904. we have now checked all the possibilities, so give an error. */
  5905. else if (terminator != CHAR_NULL)
  5906. {
  5907. *errorcodeptr = ERR15;
  5908. goto FAILED;
  5909. }
  5910. /* Check for (?(R) for recursion. Allow digits after R to specify a
  5911. specific group number. */
  5912. else if (*name == CHAR_R)
  5913. {
  5914. recno = 0;
  5915. for (i = 1; i < namelen; i++)
  5916. {
  5917. if (!IS_DIGIT(name[i]))
  5918. {
  5919. *errorcodeptr = ERR15;
  5920. goto FAILED;
  5921. }
  5922. if (recno > INT_MAX / 10 - 1) /* Integer overflow */
  5923. {
  5924. *errorcodeptr = ERR61;
  5925. goto FAILED;
  5926. }
  5927. recno = recno * 10 + name[i] - CHAR_0;
  5928. }
  5929. if (recno == 0) recno = RREF_ANY;
  5930. code[1+LINK_SIZE] = OP_RREF; /* Change test type */
  5931. PUT2(code, 2+LINK_SIZE, recno);
  5932. }
  5933. /* Similarly, check for the (?(DEFINE) "condition", which is always
  5934. false. */
  5935. else if (namelen == 6 && STRNCMP_UC_C8(name, STRING_DEFINE, 6) == 0)
  5936. {
  5937. code[1+LINK_SIZE] = OP_DEF;
  5938. skipbytes = 1;
  5939. }
  5940. /* Reference to an unidentified subpattern. */
  5941. else
  5942. {
  5943. *errorcodeptr = ERR15;
  5944. goto FAILED;
  5945. }
  5946. break;
  5947. /* ------------------------------------------------------------ */
  5948. case CHAR_EQUALS_SIGN: /* Positive lookahead */
  5949. bravalue = OP_ASSERT;
  5950. cd->assert_depth += 1;
  5951. ptr++;
  5952. break;
  5953. /* Optimize (?!) to (*FAIL) unless it is quantified - which is a weird
  5954. thing to do, but Perl allows all assertions to be quantified, and when
  5955. they contain capturing parentheses there may be a potential use for
  5956. this feature. Not that that applies to a quantified (?!) but we allow
  5957. it for uniformity. */
  5958. /* ------------------------------------------------------------ */
  5959. case CHAR_EXCLAMATION_MARK: /* Negative lookahead */
  5960. ptr++;
  5961. if (*ptr == CHAR_RIGHT_PARENTHESIS && ptr[1] != CHAR_ASTERISK &&
  5962. ptr[1] != CHAR_PLUS && ptr[1] != CHAR_QUESTION_MARK &&
  5963. (ptr[1] != CHAR_LEFT_CURLY_BRACKET || !is_counted_repeat(ptr+2)))
  5964. {
  5965. *code++ = OP_FAIL;
  5966. previous = NULL;
  5967. continue;
  5968. }
  5969. bravalue = OP_ASSERT_NOT;
  5970. cd->assert_depth += 1;
  5971. break;
  5972. /* ------------------------------------------------------------ */
  5973. case CHAR_LESS_THAN_SIGN: /* Lookbehind or named define */
  5974. switch (ptr[1])
  5975. {
  5976. case CHAR_EQUALS_SIGN: /* Positive lookbehind */
  5977. bravalue = OP_ASSERTBACK;
  5978. cd->assert_depth += 1;
  5979. ptr += 2;
  5980. break;
  5981. case CHAR_EXCLAMATION_MARK: /* Negative lookbehind */
  5982. bravalue = OP_ASSERTBACK_NOT;
  5983. cd->assert_depth += 1;
  5984. ptr += 2;
  5985. break;
  5986. default: /* Could be name define, else bad */
  5987. if (MAX_255(ptr[1]) && (cd->ctypes[ptr[1]] & ctype_word) != 0)
  5988. goto DEFINE_NAME;
  5989. ptr++; /* Correct offset for error */
  5990. *errorcodeptr = ERR24;
  5991. goto FAILED;
  5992. }
  5993. break;
  5994. /* ------------------------------------------------------------ */
  5995. case CHAR_GREATER_THAN_SIGN: /* One-time brackets */
  5996. bravalue = OP_ONCE;
  5997. ptr++;
  5998. break;
  5999. /* ------------------------------------------------------------ */
  6000. case CHAR_C: /* Callout - may be followed by digits; */
  6001. previous_callout = code; /* Save for later completion */
  6002. after_manual_callout = 1; /* Skip one item before completing */
  6003. *code++ = OP_CALLOUT;
  6004. {
  6005. int n = 0;
  6006. ptr++;
  6007. while(IS_DIGIT(*ptr))
  6008. {
  6009. n = n * 10 + *ptr++ - CHAR_0;
  6010. if (n > 255)
  6011. {
  6012. *errorcodeptr = ERR38;
  6013. goto FAILED;
  6014. }
  6015. }
  6016. if (*ptr != CHAR_RIGHT_PARENTHESIS)
  6017. {
  6018. *errorcodeptr = ERR39;
  6019. goto FAILED;
  6020. }
  6021. *code++ = n;
  6022. PUT(code, 0, (int)(ptr - cd->start_pattern + 1)); /* Pattern offset */
  6023. PUT(code, LINK_SIZE, 0); /* Default length */
  6024. code += 2 * LINK_SIZE;
  6025. }
  6026. previous = NULL;
  6027. continue;
  6028. /* ------------------------------------------------------------ */
  6029. case CHAR_P: /* Python-style named subpattern handling */
  6030. if (*(++ptr) == CHAR_EQUALS_SIGN ||
  6031. *ptr == CHAR_GREATER_THAN_SIGN) /* Reference or recursion */
  6032. {
  6033. is_recurse = *ptr == CHAR_GREATER_THAN_SIGN;
  6034. terminator = CHAR_RIGHT_PARENTHESIS;
  6035. goto NAMED_REF_OR_RECURSE;
  6036. }
  6037. else if (*ptr != CHAR_LESS_THAN_SIGN) /* Test for Python-style defn */
  6038. {
  6039. *errorcodeptr = ERR41;
  6040. goto FAILED;
  6041. }
  6042. /* Fall through to handle (?P< as (?< is handled */
  6043. /* ------------------------------------------------------------ */
  6044. DEFINE_NAME: /* Come here from (?< handling */
  6045. case CHAR_APOSTROPHE:
  6046. terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
  6047. CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
  6048. name = ++ptr;
  6049. if (IS_DIGIT(*ptr))
  6050. {
  6051. *errorcodeptr = ERR84; /* Group name must start with non-digit */
  6052. goto FAILED;
  6053. }
  6054. while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
  6055. namelen = (int)(ptr - name);
  6056. /* In the pre-compile phase, do a syntax check, remember the longest
  6057. name, and then remember the group in a vector, expanding it if
  6058. necessary. Duplicates for the same number are skipped; other duplicates
  6059. are checked for validity. In the actual compile, there is nothing to
  6060. do. */
  6061. if (lengthptr != NULL)
  6062. {
  6063. named_group *ng;
  6064. pcre_uint32 number = cd->bracount + 1;
  6065. if (*ptr != (pcre_uchar)terminator)
  6066. {
  6067. *errorcodeptr = ERR42;
  6068. goto FAILED;
  6069. }
  6070. if (cd->names_found >= MAX_NAME_COUNT)
  6071. {
  6072. *errorcodeptr = ERR49;
  6073. goto FAILED;
  6074. }
  6075. if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
  6076. {
  6077. cd->name_entry_size = namelen + IMM2_SIZE + 1;
  6078. if (namelen > MAX_NAME_SIZE)
  6079. {
  6080. *errorcodeptr = ERR48;
  6081. goto FAILED;
  6082. }
  6083. }
  6084. /* Scan the list to check for duplicates. For duplicate names, if the
  6085. number is the same, break the loop, which causes the name to be
  6086. discarded; otherwise, if DUPNAMES is not set, give an error.
  6087. If it is set, allow the name with a different number, but continue
  6088. scanning in case this is a duplicate with the same number. For
  6089. non-duplicate names, give an error if the number is duplicated. */
  6090. ng = cd->named_groups;
  6091. for (i = 0; i < cd->names_found; i++, ng++)
  6092. {
  6093. if (namelen == ng->length &&
  6094. STRNCMP_UC_UC(name, ng->name, namelen) == 0)
  6095. {
  6096. if (ng->number == number) break;
  6097. if ((options & PCRE_DUPNAMES) == 0)
  6098. {
  6099. *errorcodeptr = ERR43;
  6100. goto FAILED;
  6101. }
  6102. cd->dupnames = TRUE; /* Duplicate names exist */
  6103. }
  6104. else if (ng->number == number)
  6105. {
  6106. *errorcodeptr = ERR65;
  6107. goto FAILED;
  6108. }
  6109. }
  6110. if (i >= cd->names_found) /* Not a duplicate with same number */
  6111. {
  6112. /* Increase the list size if necessary */
  6113. if (cd->names_found >= cd->named_group_list_size)
  6114. {
  6115. int newsize = cd->named_group_list_size * 2;
  6116. named_group *newspace = (PUBL(malloc))
  6117. (newsize * sizeof(named_group));
  6118. if (newspace == NULL)
  6119. {
  6120. *errorcodeptr = ERR21;
  6121. goto FAILED;
  6122. }
  6123. memcpy(newspace, cd->named_groups,
  6124. cd->named_group_list_size * sizeof(named_group));
  6125. if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
  6126. (PUBL(free))((void *)cd->named_groups);
  6127. cd->named_groups = newspace;
  6128. cd->named_group_list_size = newsize;
  6129. }
  6130. cd->named_groups[cd->names_found].name = name;
  6131. cd->named_groups[cd->names_found].length = namelen;
  6132. cd->named_groups[cd->names_found].number = number;
  6133. cd->names_found++;
  6134. }
  6135. }
  6136. ptr++; /* Move past > or ' in both passes. */
  6137. goto NUMBERED_GROUP;
  6138. /* ------------------------------------------------------------ */
  6139. case CHAR_AMPERSAND: /* Perl recursion/subroutine syntax */
  6140. terminator = CHAR_RIGHT_PARENTHESIS;
  6141. is_recurse = TRUE;
  6142. /* Fall through */
  6143. /* We come here from the Python syntax above that handles both
  6144. references (?P=name) and recursion (?P>name), as well as falling
  6145. through from the Perl recursion syntax (?&name). We also come here from
  6146. the Perl \k<name> or \k'name' back reference syntax and the \k{name}
  6147. .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */
  6148. NAMED_REF_OR_RECURSE:
  6149. name = ++ptr;
  6150. if (IS_DIGIT(*ptr))
  6151. {
  6152. *errorcodeptr = ERR84; /* Group name must start with non-digit */
  6153. goto FAILED;
  6154. }
  6155. while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
  6156. namelen = (int)(ptr - name);
  6157. /* In the pre-compile phase, do a syntax check. We used to just set
  6158. a dummy reference number, because it was not used in the first pass.
  6159. However, with the change of recursive back references to be atomic,
  6160. we have to look for the number so that this state can be identified, as
  6161. otherwise the incorrect length is computed. If it's not a backwards
  6162. reference, the dummy number will do. */
  6163. if (lengthptr != NULL)
  6164. {
  6165. named_group *ng;
  6166. recno = 0;
  6167. if (namelen == 0)
  6168. {
  6169. *errorcodeptr = ERR62;
  6170. goto FAILED;
  6171. }
  6172. if (*ptr != (pcre_uchar)terminator)
  6173. {
  6174. *errorcodeptr = ERR42;
  6175. goto FAILED;
  6176. }
  6177. if (namelen > MAX_NAME_SIZE)
  6178. {
  6179. *errorcodeptr = ERR48;
  6180. goto FAILED;
  6181. }
  6182. /* Count named back references. */
  6183. if (!is_recurse) cd->namedrefcount++;
  6184. /* We have to allow for a named reference to a duplicated name (this
  6185. cannot be determined until the second pass). This needs an extra
  6186. 16-bit data item. */
  6187. *lengthptr += IMM2_SIZE;
  6188. /* If this is a forward reference and we are within a (?|...) group,
  6189. the reference may end up as the number of a group which we are
  6190. currently inside, that is, it could be a recursive reference. In the
  6191. real compile this will be picked up and the reference wrapped with
  6192. OP_ONCE to make it atomic, so we must space in case this occurs. */
  6193. /* In fact, this can happen for a non-forward reference because
  6194. another group with the same number might be created later. This
  6195. issue is fixed "properly" in PCRE2. As PCRE1 is now in maintenance
  6196. only mode, we finesse the bug by allowing more memory always. */
  6197. *lengthptr += 4 + 4*LINK_SIZE;
  6198. /* It is even worse than that. The current reference may be to an
  6199. existing named group with a different number (so apparently not
  6200. recursive) but which later on is also attached to a group with the
  6201. current number. This can only happen if $(| has been previous
  6202. encountered. In that case, we allow yet more memory, just in case.
  6203. (Again, this is fixed "properly" in PCRE2. */
  6204. if (cd->dupgroups) *lengthptr += 4 + 4*LINK_SIZE;
  6205. /* Otherwise, check for recursion here. The name table does not exist
  6206. in the first pass; instead we must scan the list of names encountered
  6207. so far in order to get the number. If the name is not found, leave
  6208. the value of recno as 0 for a forward reference. */
  6209. /* This patch (removing "else") fixes a problem when a reference is
  6210. to multiple identically named nested groups from within the nest.
  6211. Once again, it is not the "proper" fix, and it results in an
  6212. over-allocation of memory. */
  6213. /* else */
  6214. {
  6215. ng = cd->named_groups;
  6216. for (i = 0; i < cd->names_found; i++, ng++)
  6217. {
  6218. if (namelen == ng->length &&
  6219. STRNCMP_UC_UC(name, ng->name, namelen) == 0)
  6220. {
  6221. open_capitem *oc;
  6222. recno = ng->number;
  6223. if (is_recurse) break;
  6224. for (oc = cd->open_caps; oc != NULL; oc = oc->next)
  6225. {
  6226. if (oc->number == recno)
  6227. {
  6228. oc->flag = TRUE;
  6229. break;
  6230. }
  6231. }
  6232. }
  6233. }
  6234. }
  6235. }
  6236. /* In the real compile, search the name table. We check the name
  6237. first, and then check that we have reached the end of the name in the
  6238. table. That way, if the name is longer than any in the table, the
  6239. comparison will fail without reading beyond the table entry. */
  6240. else
  6241. {
  6242. slot = cd->name_table;
  6243. for (i = 0; i < cd->names_found; i++)
  6244. {
  6245. if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 &&
  6246. slot[IMM2_SIZE+namelen] == 0)
  6247. break;
  6248. slot += cd->name_entry_size;
  6249. }
  6250. if (i < cd->names_found)
  6251. {
  6252. recno = GET2(slot, 0);
  6253. }
  6254. else
  6255. {
  6256. *errorcodeptr = ERR15;
  6257. goto FAILED;
  6258. }
  6259. }
  6260. /* In both phases, for recursions, we can now go to the code than
  6261. handles numerical recursion. */
  6262. if (is_recurse) goto HANDLE_RECURSION;
  6263. /* In the second pass we must see if the name is duplicated. If so, we
  6264. generate a different opcode. */
  6265. if (lengthptr == NULL && cd->dupnames)
  6266. {
  6267. int count = 1;
  6268. unsigned int index = i;
  6269. pcre_uchar *cslot = slot + cd->name_entry_size;
  6270. for (i++; i < cd->names_found; i++)
  6271. {
  6272. if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break;
  6273. count++;
  6274. cslot += cd->name_entry_size;
  6275. }
  6276. if (count > 1)
  6277. {
  6278. if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
  6279. previous = code;
  6280. item_hwm_offset = cd->hwm - cd->start_workspace;
  6281. *code++ = ((options & PCRE_CASELESS) != 0)? OP_DNREFI : OP_DNREF;
  6282. PUT2INC(code, 0, index);
  6283. PUT2INC(code, 0, count);
  6284. /* Process each potentially referenced group. */
  6285. for (; slot < cslot; slot += cd->name_entry_size)
  6286. {
  6287. open_capitem *oc;
  6288. recno = GET2(slot, 0);
  6289. cd->backref_map |= (recno < 32)? (1U << recno) : 1;
  6290. if (recno > cd->top_backref) cd->top_backref = recno;
  6291. /* Check to see if this back reference is recursive, that it, it
  6292. is inside the group that it references. A flag is set so that the
  6293. group can be made atomic. */
  6294. for (oc = cd->open_caps; oc != NULL; oc = oc->next)
  6295. {
  6296. if (oc->number == recno)
  6297. {
  6298. oc->flag = TRUE;
  6299. break;
  6300. }
  6301. }
  6302. }
  6303. continue; /* End of back ref handling */
  6304. }
  6305. }
  6306. /* First pass, or a non-duplicated name. */
  6307. goto HANDLE_REFERENCE;
  6308. /* ------------------------------------------------------------ */
  6309. case CHAR_R: /* Recursion, same as (?0) */
  6310. recno = 0;
  6311. if (*(++ptr) != CHAR_RIGHT_PARENTHESIS)
  6312. {
  6313. *errorcodeptr = ERR29;
  6314. goto FAILED;
  6315. }
  6316. goto HANDLE_RECURSION;
  6317. /* ------------------------------------------------------------ */
  6318. case CHAR_MINUS: case CHAR_PLUS: /* Recursion or subroutine */
  6319. case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:
  6320. case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
  6321. {
  6322. const pcre_uchar *called;
  6323. terminator = CHAR_RIGHT_PARENTHESIS;
  6324. /* Come here from the \g<...> and \g'...' code (Oniguruma
  6325. compatibility). However, the syntax has been checked to ensure that
  6326. the ... are a (signed) number, so that neither ERR63 nor ERR29 will
  6327. be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY
  6328. ever be taken. */
  6329. HANDLE_NUMERICAL_RECURSION:
  6330. if ((refsign = *ptr) == CHAR_PLUS)
  6331. {
  6332. ptr++;
  6333. if (!IS_DIGIT(*ptr))
  6334. {
  6335. *errorcodeptr = ERR63;
  6336. goto FAILED;
  6337. }
  6338. }
  6339. else if (refsign == CHAR_MINUS)
  6340. {
  6341. if (!IS_DIGIT(ptr[1]))
  6342. goto OTHER_CHAR_AFTER_QUERY;
  6343. ptr++;
  6344. }
  6345. recno = 0;
  6346. while(IS_DIGIT(*ptr))
  6347. {
  6348. if (recno > INT_MAX / 10 - 1) /* Integer overflow */
  6349. {
  6350. while (IS_DIGIT(*ptr)) ptr++;
  6351. *errorcodeptr = ERR61;
  6352. goto FAILED;
  6353. }
  6354. recno = recno * 10 + *ptr++ - CHAR_0;
  6355. }
  6356. if (*ptr != (pcre_uchar)terminator)
  6357. {
  6358. *errorcodeptr = ERR29;
  6359. goto FAILED;
  6360. }
  6361. if (refsign == CHAR_MINUS)
  6362. {
  6363. if (recno == 0)
  6364. {
  6365. *errorcodeptr = ERR58;
  6366. goto FAILED;
  6367. }
  6368. recno = cd->bracount - recno + 1;
  6369. if (recno <= 0)
  6370. {
  6371. *errorcodeptr = ERR15;
  6372. goto FAILED;
  6373. }
  6374. }
  6375. else if (refsign == CHAR_PLUS)
  6376. {
  6377. if (recno == 0)
  6378. {
  6379. *errorcodeptr = ERR58;
  6380. goto FAILED;
  6381. }
  6382. recno += cd->bracount;
  6383. }
  6384. /* Come here from code above that handles a named recursion */
  6385. HANDLE_RECURSION:
  6386. previous = code;
  6387. item_hwm_offset = cd->hwm - cd->start_workspace;
  6388. called = cd->start_code;
  6389. /* When we are actually compiling, find the bracket that is being
  6390. referenced. Temporarily end the regex in case it doesn't exist before
  6391. this point. If we end up with a forward reference, first check that
  6392. the bracket does occur later so we can give the error (and position)
  6393. now. Then remember this forward reference in the workspace so it can
  6394. be filled in at the end. */
  6395. if (lengthptr == NULL)
  6396. {
  6397. *code = OP_END;
  6398. if (recno != 0)
  6399. called = PRIV(find_bracket)(cd->start_code, utf, recno);
  6400. /* Forward reference */
  6401. if (called == NULL)
  6402. {
  6403. if (recno > cd->final_bracount)
  6404. {
  6405. *errorcodeptr = ERR15;
  6406. goto FAILED;
  6407. }
  6408. /* Fudge the value of "called" so that when it is inserted as an
  6409. offset below, what it actually inserted is the reference number
  6410. of the group. Then remember the forward reference. */
  6411. called = cd->start_code + recno;
  6412. if (cd->hwm >= cd->start_workspace + cd->workspace_size -
  6413. WORK_SIZE_SAFETY_MARGIN)
  6414. {
  6415. *errorcodeptr = expand_workspace(cd);
  6416. if (*errorcodeptr != 0) goto FAILED;
  6417. }
  6418. PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
  6419. }
  6420. /* If not a forward reference, and the subpattern is still open,
  6421. this is a recursive call. We check to see if this is a left
  6422. recursion that could loop for ever, and diagnose that case. We
  6423. must not, however, do this check if we are in a conditional
  6424. subpattern because the condition might be testing for recursion in
  6425. a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid.
  6426. Forever loops are also detected at runtime, so those that occur in
  6427. conditional subpatterns will be picked up then. */
  6428. else if (GET(called, 1) == 0 && cond_depth <= 0 &&
  6429. could_be_empty(called, code, bcptr, utf, cd))
  6430. {
  6431. *errorcodeptr = ERR40;
  6432. goto FAILED;
  6433. }
  6434. }
  6435. /* Insert the recursion/subroutine item. It does not have a set first
  6436. character (relevant if it is repeated, because it will then be
  6437. wrapped with ONCE brackets). */
  6438. *code = OP_RECURSE;
  6439. PUT(code, 1, (int)(called - cd->start_code));
  6440. code += 1 + LINK_SIZE;
  6441. groupsetfirstchar = FALSE;
  6442. }
  6443. /* Can't determine a first byte now */
  6444. if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
  6445. zerofirstchar = firstchar;
  6446. zerofirstcharflags = firstcharflags;
  6447. continue;
  6448. /* ------------------------------------------------------------ */
  6449. default: /* Other characters: check option setting */
  6450. OTHER_CHAR_AFTER_QUERY:
  6451. set = unset = 0;
  6452. optset = &set;
  6453. while (*ptr != CHAR_RIGHT_PARENTHESIS && *ptr != CHAR_COLON)
  6454. {
  6455. switch (*ptr++)
  6456. {
  6457. case CHAR_MINUS: optset = &unset; break;
  6458. case CHAR_J: /* Record that it changed in the external options */
  6459. *optset |= PCRE_DUPNAMES;
  6460. cd->external_flags |= PCRE_JCHANGED;
  6461. break;
  6462. case CHAR_i: *optset |= PCRE_CASELESS; break;
  6463. case CHAR_m: *optset |= PCRE_MULTILINE; break;
  6464. case CHAR_s: *optset |= PCRE_DOTALL; break;
  6465. case CHAR_x: *optset |= PCRE_EXTENDED; break;
  6466. case CHAR_U: *optset |= PCRE_UNGREEDY; break;
  6467. case CHAR_X: *optset |= PCRE_EXTRA; break;
  6468. default: *errorcodeptr = ERR12;
  6469. ptr--; /* Correct the offset */
  6470. goto FAILED;
  6471. }
  6472. }
  6473. /* Set up the changed option bits, but don't change anything yet. */
  6474. newoptions = (options | set) & (~unset);
  6475. /* If the options ended with ')' this is not the start of a nested
  6476. group with option changes, so the options change at this level.
  6477. If we are not at the pattern start, reset the greedy defaults and the
  6478. case value for firstchar and reqchar. */
  6479. if (*ptr == CHAR_RIGHT_PARENTHESIS)
  6480. {
  6481. greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
  6482. greedy_non_default = greedy_default ^ 1;
  6483. req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
  6484. /* Change options at this level, and pass them back for use
  6485. in subsequent branches. */
  6486. *optionsptr = options = newoptions;
  6487. previous = NULL; /* This item can't be repeated */
  6488. continue; /* It is complete */
  6489. }
  6490. /* If the options ended with ':' we are heading into a nested group
  6491. with possible change of options. Such groups are non-capturing and are
  6492. not assertions of any kind. All we need to do is skip over the ':';
  6493. the newoptions value is handled below. */
  6494. bravalue = OP_BRA;
  6495. ptr++;
  6496. } /* End of switch for character following (? */
  6497. } /* End of (? handling */
  6498. /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
  6499. is set, all unadorned brackets become non-capturing and behave like (?:...)
  6500. brackets. */
  6501. else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)
  6502. {
  6503. bravalue = OP_BRA;
  6504. }
  6505. /* Else we have a capturing group. */
  6506. else
  6507. {
  6508. NUMBERED_GROUP:
  6509. cd->bracount += 1;
  6510. PUT2(code, 1+LINK_SIZE, cd->bracount);
  6511. skipbytes = IMM2_SIZE;
  6512. }
  6513. /* Process nested bracketed regex. First check for parentheses nested too
  6514. deeply. */
  6515. if ((cd->parens_depth += 1) > PARENS_NEST_LIMIT)
  6516. {
  6517. *errorcodeptr = ERR82;
  6518. goto FAILED;
  6519. }
  6520. /* All assertions used not to be repeatable, but this was changed for Perl
  6521. compatibility. All kinds can now be repeated except for assertions that are
  6522. conditions (Perl also forbids these to be repeated). We copy code into a
  6523. non-register variable (tempcode) in order to be able to pass its address
  6524. because some compilers complain otherwise. At the start of a conditional
  6525. group whose condition is an assertion, cd->iscondassert is set. We unset it
  6526. here so as to allow assertions later in the group to be quantified. */
  6527. if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT &&
  6528. cd->iscondassert)
  6529. {
  6530. previous = NULL;
  6531. cd->iscondassert = FALSE;
  6532. }
  6533. else
  6534. {
  6535. previous = code;
  6536. item_hwm_offset = cd->hwm - cd->start_workspace;
  6537. }
  6538. *code = bravalue;
  6539. tempcode = code;
  6540. tempreqvary = cd->req_varyopt; /* Save value before bracket */
  6541. tempbracount = cd->bracount; /* Save value before bracket */
  6542. length_prevgroup = 0; /* Initialize for pre-compile phase */
  6543. if (!compile_regex(
  6544. newoptions, /* The complete new option state */
  6545. &tempcode, /* Where to put code (updated) */
  6546. &ptr, /* Input pointer (updated) */
  6547. errorcodeptr, /* Where to put an error message */
  6548. (bravalue == OP_ASSERTBACK ||
  6549. bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
  6550. reset_bracount, /* True if (?| group */
  6551. skipbytes, /* Skip over bracket number */
  6552. cond_depth +
  6553. ((bravalue == OP_COND)?1:0), /* Depth of condition subpatterns */
  6554. &subfirstchar, /* For possible first char */
  6555. &subfirstcharflags,
  6556. &subreqchar, /* For possible last char */
  6557. &subreqcharflags,
  6558. bcptr, /* Current branch chain */
  6559. cd, /* Tables block */
  6560. (lengthptr == NULL)? NULL : /* Actual compile phase */
  6561. &length_prevgroup /* Pre-compile phase */
  6562. ))
  6563. goto FAILED;
  6564. cd->parens_depth -= 1;
  6565. /* If this was an atomic group and there are no capturing groups within it,
  6566. generate OP_ONCE_NC instead of OP_ONCE. */
  6567. if (bravalue == OP_ONCE && cd->bracount <= tempbracount)
  6568. *code = OP_ONCE_NC;
  6569. if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
  6570. cd->assert_depth -= 1;
  6571. /* At the end of compiling, code is still pointing to the start of the
  6572. group, while tempcode has been updated to point past the end of the group.
  6573. The pattern pointer (ptr) is on the bracket.
  6574. If this is a conditional bracket, check that there are no more than
  6575. two branches in the group, or just one if it's a DEFINE group. We do this
  6576. in the real compile phase, not in the pre-pass, where the whole group may
  6577. not be available. */
  6578. if (bravalue == OP_COND && lengthptr == NULL)
  6579. {
  6580. pcre_uchar *tc = code;
  6581. int condcount = 0;
  6582. do {
  6583. condcount++;
  6584. tc += GET(tc,1);
  6585. }
  6586. while (*tc != OP_KET);
  6587. /* A DEFINE group is never obeyed inline (the "condition" is always
  6588. false). It must have only one branch. */
  6589. if (code[LINK_SIZE+1] == OP_DEF)
  6590. {
  6591. if (condcount > 1)
  6592. {
  6593. *errorcodeptr = ERR54;
  6594. goto FAILED;
  6595. }
  6596. bravalue = OP_DEF; /* Just a flag to suppress char handling below */
  6597. }
  6598. /* A "normal" conditional group. If there is just one branch, we must not
  6599. make use of its firstchar or reqchar, because this is equivalent to an
  6600. empty second branch. */
  6601. else
  6602. {
  6603. if (condcount > 2)
  6604. {
  6605. *errorcodeptr = ERR27;
  6606. goto FAILED;
  6607. }
  6608. if (condcount == 1) subfirstcharflags = subreqcharflags = REQ_NONE;
  6609. }
  6610. }
  6611. /* Error if hit end of pattern */
  6612. if (*ptr != CHAR_RIGHT_PARENTHESIS)
  6613. {
  6614. *errorcodeptr = ERR14;
  6615. goto FAILED;
  6616. }
  6617. /* In the pre-compile phase, update the length by the length of the group,
  6618. less the brackets at either end. Then reduce the compiled code to just a
  6619. set of non-capturing brackets so that it doesn't use much memory if it is
  6620. duplicated by a quantifier.*/
  6621. if (lengthptr != NULL)
  6622. {
  6623. if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE)
  6624. {
  6625. *errorcodeptr = ERR20;
  6626. goto FAILED;
  6627. }
  6628. *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
  6629. code++; /* This already contains bravalue */
  6630. PUTINC(code, 0, 1 + LINK_SIZE);
  6631. *code++ = OP_KET;
  6632. PUTINC(code, 0, 1 + LINK_SIZE);
  6633. break; /* No need to waste time with special character handling */
  6634. }
  6635. /* Otherwise update the main code pointer to the end of the group. */
  6636. code = tempcode;
  6637. /* For a DEFINE group, required and first character settings are not
  6638. relevant. */
  6639. if (bravalue == OP_DEF) break;
  6640. /* Handle updating of the required and first characters for other types of
  6641. group. Update for normal brackets of all kinds, and conditions with two
  6642. branches (see code above). If the bracket is followed by a quantifier with
  6643. zero repeat, we have to back off. Hence the definition of zeroreqchar and
  6644. zerofirstchar outside the main loop so that they can be accessed for the
  6645. back off. */
  6646. zeroreqchar = reqchar;
  6647. zeroreqcharflags = reqcharflags;
  6648. zerofirstchar = firstchar;
  6649. zerofirstcharflags = firstcharflags;
  6650. groupsetfirstchar = FALSE;
  6651. if (bravalue >= OP_ONCE)
  6652. {
  6653. /* If we have not yet set a firstchar in this branch, take it from the
  6654. subpattern, remembering that it was set here so that a repeat of more
  6655. than one can replicate it as reqchar if necessary. If the subpattern has
  6656. no firstchar, set "none" for the whole branch. In both cases, a zero
  6657. repeat forces firstchar to "none". */
  6658. if (firstcharflags == REQ_UNSET)
  6659. {
  6660. if (subfirstcharflags >= 0)
  6661. {
  6662. firstchar = subfirstchar;
  6663. firstcharflags = subfirstcharflags;
  6664. groupsetfirstchar = TRUE;
  6665. }
  6666. else firstcharflags = REQ_NONE;
  6667. zerofirstcharflags = REQ_NONE;
  6668. }
  6669. /* If firstchar was previously set, convert the subpattern's firstchar
  6670. into reqchar if there wasn't one, using the vary flag that was in
  6671. existence beforehand. */
  6672. else if (subfirstcharflags >= 0 && subreqcharflags < 0)
  6673. {
  6674. subreqchar = subfirstchar;
  6675. subreqcharflags = subfirstcharflags | tempreqvary;
  6676. }
  6677. /* If the subpattern set a required byte (or set a first byte that isn't
  6678. really the first byte - see above), set it. */
  6679. if (subreqcharflags >= 0)
  6680. {
  6681. reqchar = subreqchar;
  6682. reqcharflags = subreqcharflags;
  6683. }
  6684. }
  6685. /* For a forward assertion, we take the reqchar, if set, provided that the
  6686. group has also set a first char. This can be helpful if the pattern that
  6687. follows the assertion doesn't set a different char. For example, it's
  6688. useful for /(?=abcde).+/. We can't set firstchar for an assertion, however
  6689. because it leads to incorrect effect for patterns such as /(?=a)a.+/ when
  6690. the "real" "a" would then become a reqchar instead of a firstchar. This is
  6691. overcome by a scan at the end if there's no firstchar, looking for an
  6692. asserted first char. */
  6693. else if (bravalue == OP_ASSERT && subreqcharflags >= 0 &&
  6694. subfirstcharflags >= 0)
  6695. {
  6696. reqchar = subreqchar;
  6697. reqcharflags = subreqcharflags;
  6698. }
  6699. break; /* End of processing '(' */
  6700. /* ===================================================================*/
  6701. /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
  6702. are arranged to be the negation of the corresponding OP_values in the
  6703. default case when PCRE_UCP is not set. For the back references, the values
  6704. are negative the reference number. Only back references and those types
  6705. that consume a character may be repeated. We can test for values between
  6706. ESC_b and ESC_Z for the latter; this may have to change if any new ones are
  6707. ever created. */
  6708. case CHAR_BACKSLASH:
  6709. tempptr = ptr;
  6710. escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options, FALSE);
  6711. if (*errorcodeptr != 0) goto FAILED;
  6712. if (escape == 0) /* The escape coded a single character */
  6713. c = ec;
  6714. else
  6715. {
  6716. /* For metasequences that actually match a character, we disable the
  6717. setting of a first character if it hasn't already been set. */
  6718. if (firstcharflags == REQ_UNSET && escape > ESC_b && escape < ESC_Z)
  6719. firstcharflags = REQ_NONE;
  6720. /* Set values to reset to if this is followed by a zero repeat. */
  6721. zerofirstchar = firstchar;
  6722. zerofirstcharflags = firstcharflags;
  6723. zeroreqchar = reqchar;
  6724. zeroreqcharflags = reqcharflags;
  6725. /* \g<name> or \g'name' is a subroutine call by name and \g<n> or \g'n'
  6726. is a subroutine call by number (Oniguruma syntax). In fact, the value
  6727. ESC_g is returned only for these cases. So we don't need to check for <
  6728. or ' if the value is ESC_g. For the Perl syntax \g{n} the value is
  6729. -n, and for the Perl syntax \g{name} the result is ESC_k (as
  6730. that is a synonym for a named back reference). */
  6731. if (escape == ESC_g)
  6732. {
  6733. const pcre_uchar *p;
  6734. pcre_uint32 cf;
  6735. item_hwm_offset = cd->hwm - cd->start_workspace; /* Normally this is set when '(' is read */
  6736. terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
  6737. CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
  6738. /* These two statements stop the compiler for warning about possibly
  6739. unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In
  6740. fact, because we do the check for a number below, the paths that
  6741. would actually be in error are never taken. */
  6742. skipbytes = 0;
  6743. reset_bracount = FALSE;
  6744. /* If it's not a signed or unsigned number, treat it as a name. */
  6745. cf = ptr[1];
  6746. if (cf != CHAR_PLUS && cf != CHAR_MINUS && !IS_DIGIT(cf))
  6747. {
  6748. is_recurse = TRUE;
  6749. goto NAMED_REF_OR_RECURSE;
  6750. }
  6751. /* Signed or unsigned number (cf = ptr[1]) is known to be plus or minus
  6752. or a digit. */
  6753. p = ptr + 2;
  6754. while (IS_DIGIT(*p)) p++;
  6755. if (*p != (pcre_uchar)terminator)
  6756. {
  6757. *errorcodeptr = ERR57;
  6758. goto FAILED;
  6759. }
  6760. ptr++;
  6761. goto HANDLE_NUMERICAL_RECURSION;
  6762. }
  6763. /* \k<name> or \k'name' is a back reference by name (Perl syntax).
  6764. We also support \k{name} (.NET syntax). */
  6765. if (escape == ESC_k)
  6766. {
  6767. if ((ptr[1] != CHAR_LESS_THAN_SIGN &&
  6768. ptr[1] != CHAR_APOSTROPHE && ptr[1] != CHAR_LEFT_CURLY_BRACKET))
  6769. {
  6770. *errorcodeptr = ERR69;
  6771. goto FAILED;
  6772. }
  6773. is_recurse = FALSE;
  6774. terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
  6775. CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)?
  6776. CHAR_APOSTROPHE : CHAR_RIGHT_CURLY_BRACKET;
  6777. goto NAMED_REF_OR_RECURSE;
  6778. }
  6779. /* Back references are handled specially; must disable firstchar if
  6780. not set to cope with cases like (?=(\w+))\1: which would otherwise set
  6781. ':' later. */
  6782. if (escape < 0)
  6783. {
  6784. open_capitem *oc;
  6785. recno = -escape;
  6786. /* Come here from named backref handling when the reference is to a
  6787. single group (i.e. not to a duplicated name. */
  6788. HANDLE_REFERENCE:
  6789. if (firstcharflags == REQ_UNSET) zerofirstcharflags = firstcharflags = REQ_NONE;
  6790. previous = code;
  6791. item_hwm_offset = cd->hwm - cd->start_workspace;
  6792. *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
  6793. PUT2INC(code, 0, recno);
  6794. cd->backref_map |= (recno < 32)? (1U << recno) : 1;
  6795. if (recno > cd->top_backref) cd->top_backref = recno;
  6796. /* Check to see if this back reference is recursive, that it, it
  6797. is inside the group that it references. A flag is set so that the
  6798. group can be made atomic. */
  6799. for (oc = cd->open_caps; oc != NULL; oc = oc->next)
  6800. {
  6801. if (oc->number == recno)
  6802. {
  6803. oc->flag = TRUE;
  6804. break;
  6805. }
  6806. }
  6807. }
  6808. /* So are Unicode property matches, if supported. */
  6809. #ifdef SUPPORT_UCP
  6810. else if (escape == ESC_P || escape == ESC_p)
  6811. {
  6812. BOOL negated;
  6813. unsigned int ptype = 0, pdata = 0;
  6814. if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
  6815. goto FAILED;
  6816. previous = code;
  6817. item_hwm_offset = cd->hwm - cd->start_workspace;
  6818. *code++ = ((escape == ESC_p) != negated)? OP_PROP : OP_NOTPROP;
  6819. *code++ = ptype;
  6820. *code++ = pdata;
  6821. }
  6822. #else
  6823. /* If Unicode properties are not supported, \X, \P, and \p are not
  6824. allowed. */
  6825. else if (escape == ESC_X || escape == ESC_P || escape == ESC_p)
  6826. {
  6827. *errorcodeptr = ERR45;
  6828. goto FAILED;
  6829. }
  6830. #endif
  6831. /* For the rest (including \X when Unicode properties are supported), we
  6832. can obtain the OP value by negating the escape value in the default
  6833. situation when PCRE_UCP is not set. When it *is* set, we substitute
  6834. Unicode property tests. Note that \b and \B do a one-character
  6835. lookbehind, and \A also behaves as if it does. */
  6836. else
  6837. {
  6838. if ((escape == ESC_b || escape == ESC_B || escape == ESC_A) &&
  6839. cd->max_lookbehind == 0)
  6840. cd->max_lookbehind = 1;
  6841. #ifdef SUPPORT_UCP
  6842. if (escape >= ESC_DU && escape <= ESC_wu)
  6843. {
  6844. nestptr = ptr + 1; /* Where to resume */
  6845. ptr = substitutes[escape - ESC_DU] - 1; /* Just before substitute */
  6846. }
  6847. else
  6848. #endif
  6849. /* In non-UTF-8 mode, we turn \C into OP_ALLANY instead of OP_ANYBYTE
  6850. so that it works in DFA mode and in lookbehinds. */
  6851. {
  6852. previous = (escape > ESC_b && escape < ESC_Z)? code : NULL;
  6853. item_hwm_offset = cd->hwm - cd->start_workspace;
  6854. *code++ = (!utf && escape == ESC_C)? OP_ALLANY : escape;
  6855. }
  6856. }
  6857. continue;
  6858. }
  6859. /* We have a data character whose value is in c. In UTF-8 mode it may have
  6860. a value > 127. We set its representation in the length/buffer, and then
  6861. handle it as a data character. */
  6862. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  6863. if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
  6864. mclength = PRIV(ord2utf)(c, mcbuffer);
  6865. else
  6866. #endif
  6867. {
  6868. mcbuffer[0] = c;
  6869. mclength = 1;
  6870. }
  6871. goto ONE_CHAR;
  6872. /* ===================================================================*/
  6873. /* Handle a literal character. It is guaranteed not to be whitespace or #
  6874. when the extended flag is set. If we are in a UTF mode, it may be a
  6875. multi-unit literal character. */
  6876. default:
  6877. NORMAL_CHAR:
  6878. mclength = 1;
  6879. mcbuffer[0] = c;
  6880. #ifdef SUPPORT_UTF
  6881. if (utf && HAS_EXTRALEN(c))
  6882. ACROSSCHAR(TRUE, ptr[1], mcbuffer[mclength++] = *(++ptr));
  6883. #endif
  6884. /* At this point we have the character's bytes in mcbuffer, and the length
  6885. in mclength. When not in UTF-8 mode, the length is always 1. */
  6886. ONE_CHAR:
  6887. previous = code;
  6888. item_hwm_offset = cd->hwm - cd->start_workspace;
  6889. /* For caseless UTF-8 mode when UCP support is available, check whether
  6890. this character has more than one other case. If so, generate a special
  6891. OP_PROP item instead of OP_CHARI. */
  6892. #ifdef SUPPORT_UCP
  6893. if (utf && (options & PCRE_CASELESS) != 0)
  6894. {
  6895. GETCHAR(c, mcbuffer);
  6896. if ((c = UCD_CASESET(c)) != 0)
  6897. {
  6898. *code++ = OP_PROP;
  6899. *code++ = PT_CLIST;
  6900. *code++ = c;
  6901. if (firstcharflags == REQ_UNSET)
  6902. firstcharflags = zerofirstcharflags = REQ_NONE;
  6903. break;
  6904. }
  6905. }
  6906. #endif
  6907. /* Caseful matches, or not one of the multicase characters. */
  6908. *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR;
  6909. for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
  6910. /* Remember if \r or \n were seen */
  6911. if (mcbuffer[0] == CHAR_CR || mcbuffer[0] == CHAR_NL)
  6912. cd->external_flags |= PCRE_HASCRORLF;
  6913. /* Set the first and required bytes appropriately. If no previous first
  6914. byte, set it from this character, but revert to none on a zero repeat.
  6915. Otherwise, leave the firstchar value alone, and don't change it on a zero
  6916. repeat. */
  6917. if (firstcharflags == REQ_UNSET)
  6918. {
  6919. zerofirstcharflags = REQ_NONE;
  6920. zeroreqchar = reqchar;
  6921. zeroreqcharflags = reqcharflags;
  6922. /* If the character is more than one byte long, we can set firstchar
  6923. only if it is not to be matched caselessly. */
  6924. if (mclength == 1 || req_caseopt == 0)
  6925. {
  6926. firstchar = mcbuffer[0];
  6927. firstcharflags = req_caseopt;
  6928. if (mclength != 1)
  6929. {
  6930. reqchar = code[-1];
  6931. reqcharflags = cd->req_varyopt;
  6932. }
  6933. }
  6934. else firstcharflags = reqcharflags = REQ_NONE;
  6935. }
  6936. /* firstchar was previously set; we can set reqchar only if the length is
  6937. 1 or the matching is caseful. */
  6938. else
  6939. {
  6940. zerofirstchar = firstchar;
  6941. zerofirstcharflags = firstcharflags;
  6942. zeroreqchar = reqchar;
  6943. zeroreqcharflags = reqcharflags;
  6944. if (mclength == 1 || req_caseopt == 0)
  6945. {
  6946. reqchar = code[-1];
  6947. reqcharflags = req_caseopt | cd->req_varyopt;
  6948. }
  6949. }
  6950. break; /* End of literal character handling */
  6951. }
  6952. } /* end of big loop */
  6953. /* Control never reaches here by falling through, only by a goto for all the
  6954. error states. Pass back the position in the pattern so that it can be displayed
  6955. to the user for diagnosing the error. */
  6956. FAILED:
  6957. *ptrptr = ptr;
  6958. return FALSE;
  6959. }
  6960. /*************************************************
  6961. * Compile sequence of alternatives *
  6962. *************************************************/
  6963. /* On entry, ptr is pointing past the bracket character, but on return it
  6964. points to the closing bracket, or vertical bar, or end of string. The code
  6965. variable is pointing at the byte into which the BRA operator has been stored.
  6966. This function is used during the pre-compile phase when we are trying to find
  6967. out the amount of memory needed, as well as during the real compile phase. The
  6968. value of lengthptr distinguishes the two phases.
  6969. Arguments:
  6970. options option bits, including any changes for this subpattern
  6971. codeptr -> the address of the current code pointer
  6972. ptrptr -> the address of the current pattern pointer
  6973. errorcodeptr -> pointer to error code variable
  6974. lookbehind TRUE if this is a lookbehind assertion
  6975. reset_bracount TRUE to reset the count for each branch
  6976. skipbytes skip this many bytes at start (for brackets and OP_COND)
  6977. cond_depth depth of nesting for conditional subpatterns
  6978. firstcharptr place to put the first required character
  6979. firstcharflagsptr place to put the first character flags, or a negative number
  6980. reqcharptr place to put the last required character
  6981. reqcharflagsptr place to put the last required character flags, or a negative number
  6982. bcptr pointer to the chain of currently open branches
  6983. cd points to the data block with tables pointers etc.
  6984. lengthptr NULL during the real compile phase
  6985. points to length accumulator during pre-compile phase
  6986. Returns: TRUE on success
  6987. */
  6988. static BOOL
  6989. compile_regex(int options, pcre_uchar **codeptr, const pcre_uchar **ptrptr,
  6990. int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
  6991. int cond_depth,
  6992. pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
  6993. pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
  6994. branch_chain *bcptr, compile_data *cd, int *lengthptr)
  6995. {
  6996. const pcre_uchar *ptr = *ptrptr;
  6997. pcre_uchar *code = *codeptr;
  6998. pcre_uchar *last_branch = code;
  6999. pcre_uchar *start_bracket = code;
  7000. pcre_uchar *reverse_count = NULL;
  7001. open_capitem capitem;
  7002. int capnumber = 0;
  7003. pcre_uint32 firstchar, reqchar;
  7004. pcre_int32 firstcharflags, reqcharflags;
  7005. pcre_uint32 branchfirstchar, branchreqchar;
  7006. pcre_int32 branchfirstcharflags, branchreqcharflags;
  7007. int length;
  7008. unsigned int orig_bracount;
  7009. unsigned int max_bracount;
  7010. branch_chain bc;
  7011. size_t save_hwm_offset;
  7012. /* If set, call the external function that checks for stack availability. */
  7013. if (PUBL(stack_guard) != NULL && PUBL(stack_guard)())
  7014. {
  7015. *errorcodeptr= ERR85;
  7016. return FALSE;
  7017. }
  7018. /* Miscellaneous initialization */
  7019. bc.outer = bcptr;
  7020. bc.current_branch = code;
  7021. firstchar = reqchar = 0;
  7022. firstcharflags = reqcharflags = REQ_UNSET;
  7023. save_hwm_offset = cd->hwm - cd->start_workspace;
  7024. /* Accumulate the length for use in the pre-compile phase. Start with the
  7025. length of the BRA and KET and any extra bytes that are required at the
  7026. beginning. We accumulate in a local variable to save frequent testing of
  7027. lenthptr for NULL. We cannot do this by looking at the value of code at the
  7028. start and end of each alternative, because compiled items are discarded during
  7029. the pre-compile phase so that the work space is not exceeded. */
  7030. length = 2 + 2*LINK_SIZE + skipbytes;
  7031. /* WARNING: If the above line is changed for any reason, you must also change
  7032. the code that abstracts option settings at the start of the pattern and makes
  7033. them global. It tests the value of length for (2 + 2*LINK_SIZE) in the
  7034. pre-compile phase to find out whether anything has yet been compiled or not. */
  7035. /* If this is a capturing subpattern, add to the chain of open capturing items
  7036. so that we can detect them if (*ACCEPT) is encountered. This is also used to
  7037. detect groups that contain recursive back references to themselves. Note that
  7038. only OP_CBRA need be tested here; changing this opcode to one of its variants,
  7039. e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
  7040. if (*code == OP_CBRA)
  7041. {
  7042. capnumber = GET2(code, 1 + LINK_SIZE);
  7043. capitem.number = capnumber;
  7044. capitem.next = cd->open_caps;
  7045. capitem.flag = FALSE;
  7046. cd->open_caps = &capitem;
  7047. }
  7048. /* Offset is set zero to mark that this bracket is still open */
  7049. PUT(code, 1, 0);
  7050. code += 1 + LINK_SIZE + skipbytes;
  7051. /* Loop for each alternative branch */
  7052. orig_bracount = max_bracount = cd->bracount;
  7053. for (;;)
  7054. {
  7055. /* For a (?| group, reset the capturing bracket count so that each branch
  7056. uses the same numbers. */
  7057. if (reset_bracount) cd->bracount = orig_bracount;
  7058. /* Set up dummy OP_REVERSE if lookbehind assertion */
  7059. if (lookbehind)
  7060. {
  7061. *code++ = OP_REVERSE;
  7062. reverse_count = code;
  7063. PUTINC(code, 0, 0);
  7064. length += 1 + LINK_SIZE;
  7065. }
  7066. /* Now compile the branch; in the pre-compile phase its length gets added
  7067. into the length. */
  7068. if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstchar,
  7069. &branchfirstcharflags, &branchreqchar, &branchreqcharflags, &bc,
  7070. cond_depth, cd, (lengthptr == NULL)? NULL : &length))
  7071. {
  7072. *ptrptr = ptr;
  7073. return FALSE;
  7074. }
  7075. /* Keep the highest bracket count in case (?| was used and some branch
  7076. has fewer than the rest. */
  7077. if (cd->bracount > max_bracount) max_bracount = cd->bracount;
  7078. /* In the real compile phase, there is some post-processing to be done. */
  7079. if (lengthptr == NULL)
  7080. {
  7081. /* If this is the first branch, the firstchar and reqchar values for the
  7082. branch become the values for the regex. */
  7083. if (*last_branch != OP_ALT)
  7084. {
  7085. firstchar = branchfirstchar;
  7086. firstcharflags = branchfirstcharflags;
  7087. reqchar = branchreqchar;
  7088. reqcharflags = branchreqcharflags;
  7089. }
  7090. /* If this is not the first branch, the first char and reqchar have to
  7091. match the values from all the previous branches, except that if the
  7092. previous value for reqchar didn't have REQ_VARY set, it can still match,
  7093. and we set REQ_VARY for the regex. */
  7094. else
  7095. {
  7096. /* If we previously had a firstchar, but it doesn't match the new branch,
  7097. we have to abandon the firstchar for the regex, but if there was
  7098. previously no reqchar, it takes on the value of the old firstchar. */
  7099. if (firstcharflags >= 0 &&
  7100. (firstcharflags != branchfirstcharflags || firstchar != branchfirstchar))
  7101. {
  7102. if (reqcharflags < 0)
  7103. {
  7104. reqchar = firstchar;
  7105. reqcharflags = firstcharflags;
  7106. }
  7107. firstcharflags = REQ_NONE;
  7108. }
  7109. /* If we (now or from before) have no firstchar, a firstchar from the
  7110. branch becomes a reqchar if there isn't a branch reqchar. */
  7111. if (firstcharflags < 0 && branchfirstcharflags >= 0 && branchreqcharflags < 0)
  7112. {
  7113. branchreqchar = branchfirstchar;
  7114. branchreqcharflags = branchfirstcharflags;
  7115. }
  7116. /* Now ensure that the reqchars match */
  7117. if (((reqcharflags & ~REQ_VARY) != (branchreqcharflags & ~REQ_VARY)) ||
  7118. reqchar != branchreqchar)
  7119. reqcharflags = REQ_NONE;
  7120. else
  7121. {
  7122. reqchar = branchreqchar;
  7123. reqcharflags |= branchreqcharflags; /* To "or" REQ_VARY */
  7124. }
  7125. }
  7126. /* If lookbehind, check that this branch matches a fixed-length string, and
  7127. put the length into the OP_REVERSE item. Temporarily mark the end of the
  7128. branch with OP_END. If the branch contains OP_RECURSE, the result is -3
  7129. because there may be forward references that we can't check here. Set a
  7130. flag to cause another lookbehind check at the end. Why not do it all at the
  7131. end? Because common, erroneous checks are picked up here and the offset of
  7132. the problem can be shown. */
  7133. if (lookbehind)
  7134. {
  7135. int fixed_length;
  7136. *code = OP_END;
  7137. fixed_length = find_fixedlength(last_branch, (options & PCRE_UTF8) != 0,
  7138. FALSE, cd, NULL);
  7139. DPRINTF(("fixed length = %d\n", fixed_length));
  7140. if (fixed_length == -3)
  7141. {
  7142. cd->check_lookbehind = TRUE;
  7143. }
  7144. else if (fixed_length < 0)
  7145. {
  7146. *errorcodeptr = (fixed_length == -2)? ERR36 :
  7147. (fixed_length == -4)? ERR70: ERR25;
  7148. *ptrptr = ptr;
  7149. return FALSE;
  7150. }
  7151. else
  7152. {
  7153. if (fixed_length > cd->max_lookbehind)
  7154. cd->max_lookbehind = fixed_length;
  7155. PUT(reverse_count, 0, fixed_length);
  7156. }
  7157. }
  7158. }
  7159. /* Reached end of expression, either ')' or end of pattern. In the real
  7160. compile phase, go back through the alternative branches and reverse the chain
  7161. of offsets, with the field in the BRA item now becoming an offset to the
  7162. first alternative. If there are no alternatives, it points to the end of the
  7163. group. The length in the terminating ket is always the length of the whole
  7164. bracketed item. Return leaving the pointer at the terminating char. */
  7165. if (*ptr != CHAR_VERTICAL_LINE)
  7166. {
  7167. if (lengthptr == NULL)
  7168. {
  7169. int branch_length = (int)(code - last_branch);
  7170. do
  7171. {
  7172. int prev_length = GET(last_branch, 1);
  7173. PUT(last_branch, 1, branch_length);
  7174. branch_length = prev_length;
  7175. last_branch -= branch_length;
  7176. }
  7177. while (branch_length > 0);
  7178. }
  7179. /* Fill in the ket */
  7180. *code = OP_KET;
  7181. PUT(code, 1, (int)(code - start_bracket));
  7182. code += 1 + LINK_SIZE;
  7183. /* If it was a capturing subpattern, check to see if it contained any
  7184. recursive back references. If so, we must wrap it in atomic brackets.
  7185. Because we are moving code along, we must ensure that any pending recursive
  7186. references are updated. In any event, remove the block from the chain. */
  7187. if (capnumber > 0)
  7188. {
  7189. if (cd->open_caps->flag)
  7190. {
  7191. *code = OP_END;
  7192. adjust_recurse(start_bracket, 1 + LINK_SIZE,
  7193. (options & PCRE_UTF8) != 0, cd, save_hwm_offset);
  7194. memmove(start_bracket + 1 + LINK_SIZE, start_bracket,
  7195. IN_UCHARS(code - start_bracket));
  7196. *start_bracket = OP_ONCE;
  7197. code += 1 + LINK_SIZE;
  7198. PUT(start_bracket, 1, (int)(code - start_bracket));
  7199. *code = OP_KET;
  7200. PUT(code, 1, (int)(code - start_bracket));
  7201. code += 1 + LINK_SIZE;
  7202. length += 2 + 2*LINK_SIZE;
  7203. }
  7204. cd->open_caps = cd->open_caps->next;
  7205. }
  7206. /* Retain the highest bracket number, in case resetting was used. */
  7207. cd->bracount = max_bracount;
  7208. /* Set values to pass back */
  7209. *codeptr = code;
  7210. *ptrptr = ptr;
  7211. *firstcharptr = firstchar;
  7212. *firstcharflagsptr = firstcharflags;
  7213. *reqcharptr = reqchar;
  7214. *reqcharflagsptr = reqcharflags;
  7215. if (lengthptr != NULL)
  7216. {
  7217. if (OFLOW_MAX - *lengthptr < length)
  7218. {
  7219. *errorcodeptr = ERR20;
  7220. return FALSE;
  7221. }
  7222. *lengthptr += length;
  7223. }
  7224. return TRUE;
  7225. }
  7226. /* Another branch follows. In the pre-compile phase, we can move the code
  7227. pointer back to where it was for the start of the first branch. (That is,
  7228. pretend that each branch is the only one.)
  7229. In the real compile phase, insert an ALT node. Its length field points back
  7230. to the previous branch while the bracket remains open. At the end the chain
  7231. is reversed. It's done like this so that the start of the bracket has a
  7232. zero offset until it is closed, making it possible to detect recursion. */
  7233. if (lengthptr != NULL)
  7234. {
  7235. code = *codeptr + 1 + LINK_SIZE + skipbytes;
  7236. length += 1 + LINK_SIZE;
  7237. }
  7238. else
  7239. {
  7240. *code = OP_ALT;
  7241. PUT(code, 1, (int)(code - last_branch));
  7242. bc.current_branch = last_branch = code;
  7243. code += 1 + LINK_SIZE;
  7244. }
  7245. ptr++;
  7246. }
  7247. /* Control never reaches here */
  7248. }
  7249. /*************************************************
  7250. * Check for anchored expression *
  7251. *************************************************/
  7252. /* Try to find out if this is an anchored regular expression. Consider each
  7253. alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket
  7254. all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then
  7255. it's anchored. However, if this is a multiline pattern, then only OP_SOD will
  7256. be found, because ^ generates OP_CIRCM in that mode.
  7257. We can also consider a regex to be anchored if OP_SOM starts all its branches.
  7258. This is the code for \G, which means "match at start of match position, taking
  7259. into account the match offset".
  7260. A branch is also implicitly anchored if it starts with .* and DOTALL is set,
  7261. because that will try the rest of the pattern at all possible matching points,
  7262. so there is no point trying again.... er ....
  7263. .... except when the .* appears inside capturing parentheses, and there is a
  7264. subsequent back reference to those parentheses. We haven't enough information
  7265. to catch that case precisely.
  7266. At first, the best we could do was to detect when .* was in capturing brackets
  7267. and the highest back reference was greater than or equal to that level.
  7268. However, by keeping a bitmap of the first 31 back references, we can catch some
  7269. of the more common cases more precisely.
  7270. ... A second exception is when the .* appears inside an atomic group, because
  7271. this prevents the number of characters it matches from being adjusted.
  7272. Arguments:
  7273. code points to start of expression (the bracket)
  7274. bracket_map a bitmap of which brackets we are inside while testing; this
  7275. handles up to substring 31; after that we just have to take
  7276. the less precise approach
  7277. cd points to the compile data block
  7278. atomcount atomic group level
  7279. Returns: TRUE or FALSE
  7280. */
  7281. static BOOL
  7282. is_anchored(register const pcre_uchar *code, unsigned int bracket_map,
  7283. compile_data *cd, int atomcount)
  7284. {
  7285. do {
  7286. const pcre_uchar *scode = first_significant_code(
  7287. code + PRIV(OP_lengths)[*code], FALSE);
  7288. register int op = *scode;
  7289. /* Non-capturing brackets */
  7290. if (op == OP_BRA || op == OP_BRAPOS ||
  7291. op == OP_SBRA || op == OP_SBRAPOS)
  7292. {
  7293. if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
  7294. }
  7295. /* Capturing brackets */
  7296. else if (op == OP_CBRA || op == OP_CBRAPOS ||
  7297. op == OP_SCBRA || op == OP_SCBRAPOS)
  7298. {
  7299. int n = GET2(scode, 1+LINK_SIZE);
  7300. int new_map = bracket_map | ((n < 32)? (1U << n) : 1);
  7301. if (!is_anchored(scode, new_map, cd, atomcount)) return FALSE;
  7302. }
  7303. /* Positive forward assertion */
  7304. else if (op == OP_ASSERT)
  7305. {
  7306. if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
  7307. }
  7308. /* Condition; not anchored if no second branch */
  7309. else if (op == OP_COND)
  7310. {
  7311. if (scode[GET(scode,1)] != OP_ALT) return FALSE;
  7312. if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
  7313. }
  7314. /* Atomic groups */
  7315. else if (op == OP_ONCE || op == OP_ONCE_NC)
  7316. {
  7317. if (!is_anchored(scode, bracket_map, cd, atomcount + 1))
  7318. return FALSE;
  7319. }
  7320. /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and
  7321. it isn't in brackets that are or may be referenced or inside an atomic
  7322. group. */
  7323. else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR ||
  7324. op == OP_TYPEPOSSTAR))
  7325. {
  7326. if (scode[1] != OP_ALLANY || (bracket_map & cd->backref_map) != 0 ||
  7327. atomcount > 0 || cd->had_pruneorskip)
  7328. return FALSE;
  7329. }
  7330. /* Check for explicit anchoring */
  7331. else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE;
  7332. code += GET(code, 1);
  7333. }
  7334. while (*code == OP_ALT); /* Loop for each alternative */
  7335. return TRUE;
  7336. }
  7337. /*************************************************
  7338. * Check for starting with ^ or .* *
  7339. *************************************************/
  7340. /* This is called to find out if every branch starts with ^ or .* so that
  7341. "first char" processing can be done to speed things up in multiline
  7342. matching and for non-DOTALL patterns that start with .* (which must start at
  7343. the beginning or after \n). As in the case of is_anchored() (see above), we
  7344. have to take account of back references to capturing brackets that contain .*
  7345. because in that case we can't make the assumption. Also, the appearance of .*
  7346. inside atomic brackets or in an assertion, or in a pattern that contains *PRUNE
  7347. or *SKIP does not count, because once again the assumption no longer holds.
  7348. Arguments:
  7349. code points to start of expression (the bracket)
  7350. bracket_map a bitmap of which brackets we are inside while testing; this
  7351. handles up to substring 31; after that we just have to take
  7352. the less precise approach
  7353. cd points to the compile data
  7354. atomcount atomic group level
  7355. inassert TRUE if in an assertion
  7356. Returns: TRUE or FALSE
  7357. */
  7358. static BOOL
  7359. is_startline(const pcre_uchar *code, unsigned int bracket_map,
  7360. compile_data *cd, int atomcount, BOOL inassert)
  7361. {
  7362. do {
  7363. const pcre_uchar *scode = first_significant_code(
  7364. code + PRIV(OP_lengths)[*code], FALSE);
  7365. register int op = *scode;
  7366. /* If we are at the start of a conditional assertion group, *both* the
  7367. conditional assertion *and* what follows the condition must satisfy the test
  7368. for start of line. Other kinds of condition fail. Note that there may be an
  7369. auto-callout at the start of a condition. */
  7370. if (op == OP_COND)
  7371. {
  7372. scode += 1 + LINK_SIZE;
  7373. if (*scode == OP_CALLOUT) scode += PRIV(OP_lengths)[OP_CALLOUT];
  7374. switch (*scode)
  7375. {
  7376. case OP_CREF:
  7377. case OP_DNCREF:
  7378. case OP_RREF:
  7379. case OP_DNRREF:
  7380. case OP_DEF:
  7381. case OP_FAIL:
  7382. return FALSE;
  7383. default: /* Assertion */
  7384. if (!is_startline(scode, bracket_map, cd, atomcount, TRUE)) return FALSE;
  7385. do scode += GET(scode, 1); while (*scode == OP_ALT);
  7386. scode += 1 + LINK_SIZE;
  7387. break;
  7388. }
  7389. scode = first_significant_code(scode, FALSE);
  7390. op = *scode;
  7391. }
  7392. /* Non-capturing brackets */
  7393. if (op == OP_BRA || op == OP_BRAPOS ||
  7394. op == OP_SBRA || op == OP_SBRAPOS)
  7395. {
  7396. if (!is_startline(scode, bracket_map, cd, atomcount, inassert)) return FALSE;
  7397. }
  7398. /* Capturing brackets */
  7399. else if (op == OP_CBRA || op == OP_CBRAPOS ||
  7400. op == OP_SCBRA || op == OP_SCBRAPOS)
  7401. {
  7402. int n = GET2(scode, 1+LINK_SIZE);
  7403. int new_map = bracket_map | ((n < 32)? (1U << n) : 1);
  7404. if (!is_startline(scode, new_map, cd, atomcount, inassert)) return FALSE;
  7405. }
  7406. /* Positive forward assertions */
  7407. else if (op == OP_ASSERT)
  7408. {
  7409. if (!is_startline(scode, bracket_map, cd, atomcount, TRUE)) return FALSE;
  7410. }
  7411. /* Atomic brackets */
  7412. else if (op == OP_ONCE || op == OP_ONCE_NC)
  7413. {
  7414. if (!is_startline(scode, bracket_map, cd, atomcount + 1, inassert)) return FALSE;
  7415. }
  7416. /* .* means "start at start or after \n" if it isn't in atomic brackets or
  7417. brackets that may be referenced or an assertion, as long as the pattern does
  7418. not contain *PRUNE or *SKIP, because these break the feature. Consider, for
  7419. example, /.*?a(*PRUNE)b/ with the subject "aab", which matches "ab", i.e.
  7420. not at the start of a line. */
  7421. else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR || op == OP_TYPEPOSSTAR)
  7422. {
  7423. if (scode[1] != OP_ANY || (bracket_map & cd->backref_map) != 0 ||
  7424. atomcount > 0 || cd->had_pruneorskip || inassert)
  7425. return FALSE;
  7426. }
  7427. /* Check for explicit circumflex; anything else gives a FALSE result. Note
  7428. in particular that this includes atomic brackets OP_ONCE and OP_ONCE_NC
  7429. because the number of characters matched by .* cannot be adjusted inside
  7430. them. */
  7431. else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;
  7432. /* Move on to the next alternative */
  7433. code += GET(code, 1);
  7434. }
  7435. while (*code == OP_ALT); /* Loop for each alternative */
  7436. return TRUE;
  7437. }
  7438. /*************************************************
  7439. * Check for asserted fixed first char *
  7440. *************************************************/
  7441. /* During compilation, the "first char" settings from forward assertions are
  7442. discarded, because they can cause conflicts with actual literals that follow.
  7443. However, if we end up without a first char setting for an unanchored pattern,
  7444. it is worth scanning the regex to see if there is an initial asserted first
  7445. char. If all branches start with the same asserted char, or with a
  7446. non-conditional bracket all of whose alternatives start with the same asserted
  7447. char (recurse ad lib), then we return that char, with the flags set to zero or
  7448. REQ_CASELESS; otherwise return zero with REQ_NONE in the flags.
  7449. Arguments:
  7450. code points to start of expression (the bracket)
  7451. flags points to the first char flags, or to REQ_NONE
  7452. inassert TRUE if in an assertion
  7453. Returns: the fixed first char, or 0 with REQ_NONE in flags
  7454. */
  7455. static pcre_uint32
  7456. find_firstassertedchar(const pcre_uchar *code, pcre_int32 *flags,
  7457. BOOL inassert)
  7458. {
  7459. register pcre_uint32 c = 0;
  7460. int cflags = REQ_NONE;
  7461. *flags = REQ_NONE;
  7462. do {
  7463. pcre_uint32 d;
  7464. int dflags;
  7465. int xl = (*code == OP_CBRA || *code == OP_SCBRA ||
  7466. *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? IMM2_SIZE:0;
  7467. const pcre_uchar *scode = first_significant_code(code + 1+LINK_SIZE + xl,
  7468. TRUE);
  7469. register pcre_uchar op = *scode;
  7470. switch(op)
  7471. {
  7472. default:
  7473. return 0;
  7474. case OP_BRA:
  7475. case OP_BRAPOS:
  7476. case OP_CBRA:
  7477. case OP_SCBRA:
  7478. case OP_CBRAPOS:
  7479. case OP_SCBRAPOS:
  7480. case OP_ASSERT:
  7481. case OP_ONCE:
  7482. case OP_ONCE_NC:
  7483. d = find_firstassertedchar(scode, &dflags, op == OP_ASSERT);
  7484. if (dflags < 0)
  7485. return 0;
  7486. if (cflags < 0) { c = d; cflags = dflags; } else if (c != d || cflags != dflags) return 0;
  7487. break;
  7488. case OP_EXACT:
  7489. scode += IMM2_SIZE;
  7490. /* Fall through */
  7491. case OP_CHAR:
  7492. case OP_PLUS:
  7493. case OP_MINPLUS:
  7494. case OP_POSPLUS:
  7495. if (!inassert) return 0;
  7496. if (cflags < 0) { c = scode[1]; cflags = 0; }
  7497. else if (c != scode[1]) return 0;
  7498. break;
  7499. case OP_EXACTI:
  7500. scode += IMM2_SIZE;
  7501. /* Fall through */
  7502. case OP_CHARI:
  7503. case OP_PLUSI:
  7504. case OP_MINPLUSI:
  7505. case OP_POSPLUSI:
  7506. if (!inassert) return 0;
  7507. if (cflags < 0) { c = scode[1]; cflags = REQ_CASELESS; }
  7508. else if (c != scode[1]) return 0;
  7509. break;
  7510. }
  7511. code += GET(code, 1);
  7512. }
  7513. while (*code == OP_ALT);
  7514. *flags = cflags;
  7515. return c;
  7516. }
  7517. /*************************************************
  7518. * Add an entry to the name/number table *
  7519. *************************************************/
  7520. /* This function is called between compiling passes to add an entry to the
  7521. name/number table, maintaining alphabetical order. Checking for permitted
  7522. and forbidden duplicates has already been done.
  7523. Arguments:
  7524. cd the compile data block
  7525. name the name to add
  7526. length the length of the name
  7527. groupno the group number
  7528. Returns: nothing
  7529. */
  7530. static void
  7531. add_name(compile_data *cd, const pcre_uchar *name, int length,
  7532. unsigned int groupno)
  7533. {
  7534. int i;
  7535. pcre_uchar *slot = cd->name_table;
  7536. for (i = 0; i < cd->names_found; i++)
  7537. {
  7538. int crc = strncmp(name, slot+IMM2_SIZE, IN_UCHARS(length));
  7539. if (crc == 0 && slot[IMM2_SIZE+length] != 0)
  7540. crc = -1; /* Current name is a substring */
  7541. /* Make space in the table and break the loop for an earlier name. For a
  7542. duplicate or later name, carry on. We do this for duplicates so that in the
  7543. simple case (when ?(| is not used) they are in order of their numbers. In all
  7544. cases they are in the order in which they appear in the pattern. */
  7545. if (crc < 0)
  7546. {
  7547. memmove(slot + cd->name_entry_size, slot,
  7548. IN_UCHARS((cd->names_found - i) * cd->name_entry_size));
  7549. break;
  7550. }
  7551. /* Continue the loop for a later or duplicate name */
  7552. slot += cd->name_entry_size;
  7553. }
  7554. PUT2(slot, 0, groupno);
  7555. memcpy(slot + IMM2_SIZE, name, IN_UCHARS(length));
  7556. slot[IMM2_SIZE + length] = 0;
  7557. cd->names_found++;
  7558. }
  7559. /*************************************************
  7560. * Compile a Regular Expression *
  7561. *************************************************/
  7562. /* This function takes a string and returns a pointer to a block of store
  7563. holding a compiled version of the expression. The original API for this
  7564. function had no error code return variable; it is retained for backwards
  7565. compatibility. The new function is given a new name.
  7566. Arguments:
  7567. pattern the regular expression
  7568. options various option bits
  7569. errorcodeptr pointer to error code variable (pcre_compile2() only)
  7570. can be NULL if you don't want a code value
  7571. errorptr pointer to pointer to error text
  7572. erroroffset ptr offset in pattern where error was detected
  7573. tables pointer to character tables or NULL
  7574. Returns: pointer to compiled data block, or NULL on error,
  7575. with errorptr and erroroffset set
  7576. */
  7577. #if defined COMPILE_PCRE8
  7578. PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
  7579. pcre_compile(const char *pattern, int options, const char **errorptr,
  7580. int *erroroffset, const unsigned char *tables)
  7581. #elif defined COMPILE_PCRE16
  7582. PCRE_EXP_DEFN pcre16 * PCRE_CALL_CONVENTION
  7583. pcre16_compile(PCRE_SPTR16 pattern, int options, const char **errorptr,
  7584. int *erroroffset, const unsigned char *tables)
  7585. #elif defined COMPILE_PCRE32
  7586. PCRE_EXP_DEFN pcre32 * PCRE_CALL_CONVENTION
  7587. pcre32_compile(PCRE_SPTR32 pattern, int options, const char **errorptr,
  7588. int *erroroffset, const unsigned char *tables)
  7589. #endif
  7590. {
  7591. #if defined COMPILE_PCRE8
  7592. return pcre_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
  7593. #elif defined COMPILE_PCRE16
  7594. return pcre16_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
  7595. #elif defined COMPILE_PCRE32
  7596. return pcre32_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
  7597. #endif
  7598. }
  7599. #if defined COMPILE_PCRE8
  7600. PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
  7601. pcre_compile2(const char *pattern, int options, int *errorcodeptr,
  7602. const char **errorptr, int *erroroffset, const unsigned char *tables)
  7603. #elif defined COMPILE_PCRE16
  7604. PCRE_EXP_DEFN pcre16 * PCRE_CALL_CONVENTION
  7605. pcre16_compile2(PCRE_SPTR16 pattern, int options, int *errorcodeptr,
  7606. const char **errorptr, int *erroroffset, const unsigned char *tables)
  7607. #elif defined COMPILE_PCRE32
  7608. PCRE_EXP_DEFN pcre32 * PCRE_CALL_CONVENTION
  7609. pcre32_compile2(PCRE_SPTR32 pattern, int options, int *errorcodeptr,
  7610. const char **errorptr, int *erroroffset, const unsigned char *tables)
  7611. #endif
  7612. {
  7613. REAL_PCRE *re;
  7614. int length = 1; /* For final END opcode */
  7615. pcre_int32 firstcharflags, reqcharflags;
  7616. pcre_uint32 firstchar, reqchar;
  7617. pcre_uint32 limit_match = PCRE_UINT32_MAX;
  7618. pcre_uint32 limit_recursion = PCRE_UINT32_MAX;
  7619. int newline;
  7620. int errorcode = 0;
  7621. int skipatstart = 0;
  7622. BOOL utf;
  7623. BOOL never_utf = FALSE;
  7624. size_t size;
  7625. pcre_uchar *code;
  7626. const pcre_uchar *codestart;
  7627. const pcre_uchar *ptr;
  7628. compile_data compile_block;
  7629. compile_data *cd = &compile_block;
  7630. /* This space is used for "compiling" into during the first phase, when we are
  7631. computing the amount of memory that is needed. Compiled items are thrown away
  7632. as soon as possible, so that a fairly large buffer should be sufficient for
  7633. this purpose. The same space is used in the second phase for remembering where
  7634. to fill in forward references to subpatterns. That may overflow, in which case
  7635. new memory is obtained from malloc(). */
  7636. pcre_uchar cworkspace[COMPILE_WORK_SIZE];
  7637. /* This vector is used for remembering name groups during the pre-compile. In a
  7638. similar way to cworkspace, it can be expanded using malloc() if necessary. */
  7639. named_group named_groups[NAMED_GROUP_LIST_SIZE];
  7640. cd->named_groups = named_groups;
  7641. cd->named_group_list_size = NAMED_GROUP_LIST_SIZE;
  7642. /* Set this early so that early errors get offset 0. */
  7643. ptr = (const pcre_uchar *)pattern;
  7644. /* We can't pass back an error message if errorptr is NULL; I guess the best we
  7645. can do is just return NULL, but we can set a code value if there is a code
  7646. pointer. */
  7647. if (errorptr == NULL)
  7648. {
  7649. if (errorcodeptr != NULL) *errorcodeptr = 99;
  7650. return NULL;
  7651. }
  7652. *errorptr = NULL;
  7653. if (errorcodeptr != NULL) *errorcodeptr = ERR0;
  7654. /* However, we can give a message for this error */
  7655. if (erroroffset == NULL)
  7656. {
  7657. errorcode = ERR16;
  7658. goto PCRE_EARLY_ERROR_RETURN2;
  7659. }
  7660. *erroroffset = 0;
  7661. /* Set up pointers to the individual character tables */
  7662. if (tables == NULL) tables = PRIV(default_tables);
  7663. cd->lcc = tables + lcc_offset;
  7664. cd->fcc = tables + fcc_offset;
  7665. cd->cbits = tables + cbits_offset;
  7666. cd->ctypes = tables + ctypes_offset;
  7667. /* Check that all undefined public option bits are zero */
  7668. if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0)
  7669. {
  7670. errorcode = ERR17;
  7671. goto PCRE_EARLY_ERROR_RETURN;
  7672. }
  7673. /* If PCRE_NEVER_UTF is set, remember it. */
  7674. if ((options & PCRE_NEVER_UTF) != 0) never_utf = TRUE;
  7675. /* Check for global one-time settings at the start of the pattern, and remember
  7676. the offset for later. */
  7677. cd->external_flags = 0; /* Initialize here for LIMIT_MATCH/RECURSION */
  7678. while (ptr[skipatstart] == CHAR_LEFT_PARENTHESIS &&
  7679. ptr[skipatstart+1] == CHAR_ASTERISK)
  7680. {
  7681. int newnl = 0;
  7682. int newbsr = 0;
  7683. /* For completeness and backward compatibility, (*UTFn) is supported in the
  7684. relevant libraries, but (*UTF) is generic and always supported. Note that
  7685. PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. */
  7686. #ifdef COMPILE_PCRE8
  7687. if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF8_RIGHTPAR, 5) == 0)
  7688. { skipatstart += 7; options |= PCRE_UTF8; continue; }
  7689. #endif
  7690. #ifdef COMPILE_PCRE16
  7691. if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF16_RIGHTPAR, 6) == 0)
  7692. { skipatstart += 8; options |= PCRE_UTF16; continue; }
  7693. #endif
  7694. #ifdef COMPILE_PCRE32
  7695. if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF32_RIGHTPAR, 6) == 0)
  7696. { skipatstart += 8; options |= PCRE_UTF32; continue; }
  7697. #endif
  7698. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF_RIGHTPAR, 4) == 0)
  7699. { skipatstart += 6; options |= PCRE_UTF8; continue; }
  7700. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UCP_RIGHTPAR, 4) == 0)
  7701. { skipatstart += 6; options |= PCRE_UCP; continue; }
  7702. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_AUTO_POSSESS_RIGHTPAR, 16) == 0)
  7703. { skipatstart += 18; options |= PCRE_NO_AUTO_POSSESS; continue; }
  7704. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_START_OPT_RIGHTPAR, 13) == 0)
  7705. { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }
  7706. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_MATCH_EQ, 12) == 0)
  7707. {
  7708. pcre_uint32 c = 0;
  7709. int p = skipatstart + 14;
  7710. while (isdigit(ptr[p]))
  7711. {
  7712. if (c > PCRE_UINT32_MAX / 10 - 1) break; /* Integer overflow */
  7713. c = c*10 + ptr[p++] - CHAR_0;
  7714. }
  7715. if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break;
  7716. if (c < limit_match)
  7717. {
  7718. limit_match = c;
  7719. cd->external_flags |= PCRE_MLSET;
  7720. }
  7721. skipatstart = p;
  7722. continue;
  7723. }
  7724. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_RECURSION_EQ, 16) == 0)
  7725. {
  7726. pcre_uint32 c = 0;
  7727. int p = skipatstart + 18;
  7728. while (isdigit(ptr[p]))
  7729. {
  7730. if (c > PCRE_UINT32_MAX / 10 - 1) break; /* Integer overflow check */
  7731. c = c*10 + ptr[p++] - CHAR_0;
  7732. }
  7733. if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break;
  7734. if (c < limit_recursion)
  7735. {
  7736. limit_recursion = c;
  7737. cd->external_flags |= PCRE_RLSET;
  7738. }
  7739. skipatstart = p;
  7740. continue;
  7741. }
  7742. if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CR_RIGHTPAR, 3) == 0)
  7743. { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
  7744. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LF_RIGHTPAR, 3) == 0)
  7745. { skipatstart += 5; newnl = PCRE_NEWLINE_LF; }
  7746. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CRLF_RIGHTPAR, 5) == 0)
  7747. { skipatstart += 7; newnl = PCRE_NEWLINE_CR + PCRE_NEWLINE_LF; }
  7748. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANY_RIGHTPAR, 4) == 0)
  7749. { skipatstart += 6; newnl = PCRE_NEWLINE_ANY; }
  7750. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANYCRLF_RIGHTPAR, 8) == 0)
  7751. { skipatstart += 10; newnl = PCRE_NEWLINE_ANYCRLF; }
  7752. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_ANYCRLF_RIGHTPAR, 12) == 0)
  7753. { skipatstart += 14; newbsr = PCRE_BSR_ANYCRLF; }
  7754. else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_UNICODE_RIGHTPAR, 12) == 0)
  7755. { skipatstart += 14; newbsr = PCRE_BSR_UNICODE; }
  7756. if (newnl != 0)
  7757. options = (options & ~PCRE_NEWLINE_BITS) | newnl;
  7758. else if (newbsr != 0)
  7759. options = (options & ~(PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) | newbsr;
  7760. else break;
  7761. }
  7762. /* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */
  7763. utf = (options & PCRE_UTF8) != 0;
  7764. if (utf && never_utf)
  7765. {
  7766. errorcode = ERR78;
  7767. goto PCRE_EARLY_ERROR_RETURN2;
  7768. }
  7769. /* Can't support UTF unless PCRE has been compiled to include the code. The
  7770. return of an error code from PRIV(valid_utf)() is a new feature, introduced in
  7771. release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
  7772. not used here. */
  7773. #ifdef SUPPORT_UTF
  7774. if (utf && (options & PCRE_NO_UTF8_CHECK) == 0 &&
  7775. (errorcode = PRIV(valid_utf)((PCRE_PUCHAR)pattern, -1, erroroffset)) != 0)
  7776. {
  7777. #if defined COMPILE_PCRE8
  7778. errorcode = ERR44;
  7779. #elif defined COMPILE_PCRE16
  7780. errorcode = ERR74;
  7781. #elif defined COMPILE_PCRE32
  7782. errorcode = ERR77;
  7783. #endif
  7784. goto PCRE_EARLY_ERROR_RETURN2;
  7785. }
  7786. #else
  7787. if (utf)
  7788. {
  7789. errorcode = ERR32;
  7790. goto PCRE_EARLY_ERROR_RETURN;
  7791. }
  7792. #endif
  7793. /* Can't support UCP unless PCRE has been compiled to include the code. */
  7794. #ifndef SUPPORT_UCP
  7795. if ((options & PCRE_UCP) != 0)
  7796. {
  7797. errorcode = ERR67;
  7798. goto PCRE_EARLY_ERROR_RETURN;
  7799. }
  7800. #endif
  7801. /* Check validity of \R options. */
  7802. if ((options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) ==
  7803. (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
  7804. {
  7805. errorcode = ERR56;
  7806. goto PCRE_EARLY_ERROR_RETURN;
  7807. }
  7808. /* Handle different types of newline. The three bits give seven cases. The
  7809. current code allows for fixed one- or two-byte sequences, plus "any" and
  7810. "anycrlf". */
  7811. switch (options & PCRE_NEWLINE_BITS)
  7812. {
  7813. case 0: newline = NEWLINE; break; /* Build-time default */
  7814. case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
  7815. case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
  7816. case PCRE_NEWLINE_CR+
  7817. PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
  7818. case PCRE_NEWLINE_ANY: newline = -1; break;
  7819. case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
  7820. default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN;
  7821. }
  7822. if (newline == -2)
  7823. {
  7824. cd->nltype = NLTYPE_ANYCRLF;
  7825. }
  7826. else if (newline < 0)
  7827. {
  7828. cd->nltype = NLTYPE_ANY;
  7829. }
  7830. else
  7831. {
  7832. cd->nltype = NLTYPE_FIXED;
  7833. if (newline > 255)
  7834. {
  7835. cd->nllen = 2;
  7836. cd->nl[0] = (newline >> 8) & 255;
  7837. cd->nl[1] = newline & 255;
  7838. }
  7839. else
  7840. {
  7841. cd->nllen = 1;
  7842. cd->nl[0] = newline;
  7843. }
  7844. }
  7845. /* Maximum back reference and backref bitmap. The bitmap records up to 31 back
  7846. references to help in deciding whether (.*) can be treated as anchored or not.
  7847. */
  7848. cd->top_backref = 0;
  7849. cd->backref_map = 0;
  7850. /* Reflect pattern for debugging output */
  7851. DPRINTF(("------------------------------------------------------------------\n"));
  7852. #ifdef PCRE_DEBUG
  7853. print_puchar(stdout, (PCRE_PUCHAR)pattern);
  7854. #endif
  7855. DPRINTF(("\n"));
  7856. /* Pretend to compile the pattern while actually just accumulating the length
  7857. of memory required. This behaviour is triggered by passing a non-NULL final
  7858. argument to compile_regex(). We pass a block of workspace (cworkspace) for it
  7859. to compile parts of the pattern into; the compiled code is discarded when it is
  7860. no longer needed, so hopefully this workspace will never overflow, though there
  7861. is a test for its doing so. */
  7862. cd->bracount = cd->final_bracount = 0;
  7863. cd->names_found = 0;
  7864. cd->name_entry_size = 0;
  7865. cd->name_table = NULL;
  7866. cd->dupnames = FALSE;
  7867. cd->dupgroups = FALSE;
  7868. cd->namedrefcount = 0;
  7869. cd->start_code = cworkspace;
  7870. cd->hwm = cworkspace;
  7871. cd->iscondassert = FALSE;
  7872. cd->start_workspace = cworkspace;
  7873. cd->workspace_size = COMPILE_WORK_SIZE;
  7874. cd->start_pattern = (const pcre_uchar *)pattern;
  7875. cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));
  7876. cd->req_varyopt = 0;
  7877. cd->parens_depth = 0;
  7878. cd->assert_depth = 0;
  7879. cd->max_lookbehind = 0;
  7880. cd->external_options = options;
  7881. cd->open_caps = NULL;
  7882. /* Now do the pre-compile. On error, errorcode will be set non-zero, so we
  7883. don't need to look at the result of the function here. The initial options have
  7884. been put into the cd block so that they can be changed if an option setting is
  7885. found within the regex right at the beginning. Bringing initial option settings
  7886. outside can help speed up starting point checks. */
  7887. ptr += skipatstart;
  7888. code = cworkspace;
  7889. *code = OP_BRA;
  7890. (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,
  7891. FALSE, 0, 0, &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL,
  7892. cd, &length);
  7893. if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
  7894. DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
  7895. (int)(cd->hwm - cworkspace)));
  7896. if (length > MAX_PATTERN_SIZE)
  7897. {
  7898. errorcode = ERR20;
  7899. goto PCRE_EARLY_ERROR_RETURN;
  7900. }
  7901. /* Compute the size of the data block for storing the compiled pattern. Integer
  7902. overflow should no longer be possible because nowadays we limit the maximum
  7903. value of cd->names_found and cd->name_entry_size. */
  7904. size = sizeof(REAL_PCRE) +
  7905. (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar);
  7906. /* Get the memory. */
  7907. re = (REAL_PCRE *)(PUBL(malloc))(size);
  7908. if (re == NULL)
  7909. {
  7910. errorcode = ERR21;
  7911. goto PCRE_EARLY_ERROR_RETURN;
  7912. }
  7913. /* Put in the magic number, and save the sizes, initial options, internal
  7914. flags, and character table pointer. NULL is used for the default character
  7915. tables. The nullpad field is at the end; it's there to help in the case when a
  7916. regex compiled on a system with 4-byte pointers is run on another with 8-byte
  7917. pointers. */
  7918. re->magic_number = MAGIC_NUMBER;
  7919. re->size = (int)size;
  7920. re->options = cd->external_options;
  7921. re->flags = cd->external_flags;
  7922. re->limit_match = limit_match;
  7923. re->limit_recursion = limit_recursion;
  7924. re->first_char = 0;
  7925. re->req_char = 0;
  7926. re->name_table_offset = sizeof(REAL_PCRE) / sizeof(pcre_uchar);
  7927. re->name_entry_size = cd->name_entry_size;
  7928. re->name_count = cd->names_found;
  7929. re->ref_count = 0;
  7930. re->tables = (tables == PRIV(default_tables))? NULL : tables;
  7931. re->nullpad = NULL;
  7932. #ifdef COMPILE_PCRE32
  7933. re->dummy = 0;
  7934. #else
  7935. re->dummy1 = re->dummy2 = re->dummy3 = 0;
  7936. #endif
  7937. /* The starting points of the name/number translation table and of the code are
  7938. passed around in the compile data block. The start/end pattern and initial
  7939. options are already set from the pre-compile phase, as is the name_entry_size
  7940. field. Reset the bracket count and the names_found field. Also reset the hwm
  7941. field; this time it's used for remembering forward references to subpatterns.
  7942. */
  7943. cd->final_bracount = cd->bracount; /* Save for checking forward references */
  7944. cd->parens_depth = 0;
  7945. cd->assert_depth = 0;
  7946. cd->bracount = 0;
  7947. cd->max_lookbehind = 0;
  7948. cd->name_table = (pcre_uchar *)re + re->name_table_offset;
  7949. codestart = cd->name_table + re->name_entry_size * re->name_count;
  7950. cd->start_code = codestart;
  7951. cd->hwm = (pcre_uchar *)(cd->start_workspace);
  7952. cd->iscondassert = FALSE;
  7953. cd->req_varyopt = 0;
  7954. cd->had_accept = FALSE;
  7955. cd->had_pruneorskip = FALSE;
  7956. cd->check_lookbehind = FALSE;
  7957. cd->open_caps = NULL;
  7958. /* If any named groups were found, create the name/number table from the list
  7959. created in the first pass. */
  7960. if (cd->names_found > 0)
  7961. {
  7962. int i = cd->names_found;
  7963. named_group *ng = cd->named_groups;
  7964. cd->names_found = 0;
  7965. for (; i > 0; i--, ng++)
  7966. add_name(cd, ng->name, ng->length, ng->number);
  7967. if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
  7968. (PUBL(free))((void *)cd->named_groups);
  7969. cd->named_group_list_size = 0; /* So we don't free it twice */
  7970. }
  7971. /* Set up a starting, non-extracting bracket, then compile the expression. On
  7972. error, errorcode will be set non-zero, so we don't need to look at the result
  7973. of the function here. */
  7974. ptr = (const pcre_uchar *)pattern + skipatstart;
  7975. code = (pcre_uchar *)codestart;
  7976. *code = OP_BRA;
  7977. (void)compile_regex(re->options, &code, &ptr, &errorcode, FALSE, FALSE, 0, 0,
  7978. &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL, cd, NULL);
  7979. re->top_bracket = cd->bracount;
  7980. re->top_backref = cd->top_backref;
  7981. re->max_lookbehind = cd->max_lookbehind;
  7982. re->flags = cd->external_flags | PCRE_MODE;
  7983. if (cd->had_accept)
  7984. {
  7985. reqchar = 0; /* Must disable after (*ACCEPT) */
  7986. reqcharflags = REQ_NONE;
  7987. }
  7988. /* If not reached end of pattern on success, there's an excess bracket. */
  7989. if (errorcode == 0 && *ptr != CHAR_NULL) errorcode = ERR22;
  7990. /* Fill in the terminating state and check for disastrous overflow, but
  7991. if debugging, leave the test till after things are printed out. */
  7992. *code++ = OP_END;
  7993. #ifndef PCRE_DEBUG
  7994. if (code - codestart > length) errorcode = ERR23;
  7995. #endif
  7996. #ifdef SUPPORT_VALGRIND
  7997. /* If the estimated length exceeds the really used length, mark the extra
  7998. allocated memory as unaddressable, so that any out-of-bound reads can be
  7999. detected. */
  8000. VALGRIND_MAKE_MEM_NOACCESS(code, (length - (code - codestart)) * sizeof(pcre_uchar));
  8001. #endif
  8002. /* Fill in any forward references that are required. There may be repeated
  8003. references; optimize for them, as searching a large regex takes time. */
  8004. if (cd->hwm > cd->start_workspace)
  8005. {
  8006. int prev_recno = -1;
  8007. const pcre_uchar *groupptr = NULL;
  8008. while (errorcode == 0 && cd->hwm > cd->start_workspace)
  8009. {
  8010. int offset, recno;
  8011. cd->hwm -= LINK_SIZE;
  8012. offset = GET(cd->hwm, 0);
  8013. /* Check that the hwm handling hasn't gone wrong. This whole area is
  8014. rewritten in PCRE2 because there are some obscure cases. */
  8015. if (offset == 0 || codestart[offset-1] != OP_RECURSE)
  8016. {
  8017. errorcode = ERR10;
  8018. break;
  8019. }
  8020. recno = GET(codestart, offset);
  8021. if (recno != prev_recno)
  8022. {
  8023. groupptr = PRIV(find_bracket)(codestart, utf, recno);
  8024. prev_recno = recno;
  8025. }
  8026. if (groupptr == NULL) errorcode = ERR53;
  8027. else PUT(((pcre_uchar *)codestart), offset, (int)(groupptr - codestart));
  8028. }
  8029. }
  8030. /* If the workspace had to be expanded, free the new memory. Set the pointer to
  8031. NULL to indicate that forward references have been filled in. */
  8032. if (cd->workspace_size > COMPILE_WORK_SIZE)
  8033. (PUBL(free))((void *)cd->start_workspace);
  8034. cd->start_workspace = NULL;
  8035. /* Give an error if there's back reference to a non-existent capturing
  8036. subpattern. */
  8037. if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
  8038. /* Unless disabled, check whether any single character iterators can be
  8039. auto-possessified. The function overwrites the appropriate opcode values, so
  8040. the type of the pointer must be cast. NOTE: the intermediate variable "temp" is
  8041. used in this code because at least one compiler gives a warning about loss of
  8042. "const" attribute if the cast (pcre_uchar *)codestart is used directly in the
  8043. function call. */
  8044. if (errorcode == 0 && (options & PCRE_NO_AUTO_POSSESS) == 0)
  8045. {
  8046. pcre_uchar *temp = (pcre_uchar *)codestart;
  8047. auto_possessify(temp, utf, cd);
  8048. }
  8049. /* If there were any lookbehind assertions that contained OP_RECURSE
  8050. (recursions or subroutine calls), a flag is set for them to be checked here,
  8051. because they may contain forward references. Actual recursions cannot be fixed
  8052. length, but subroutine calls can. It is done like this so that those without
  8053. OP_RECURSE that are not fixed length get a diagnosic with a useful offset. The
  8054. exceptional ones forgo this. We scan the pattern to check that they are fixed
  8055. length, and set their lengths. */
  8056. if (errorcode == 0 && cd->check_lookbehind)
  8057. {
  8058. pcre_uchar *cc = (pcre_uchar *)codestart;
  8059. /* Loop, searching for OP_REVERSE items, and process those that do not have
  8060. their length set. (Actually, it will also re-process any that have a length
  8061. of zero, but that is a pathological case, and it does no harm.) When we find
  8062. one, we temporarily terminate the branch it is in while we scan it. */
  8063. for (cc = (pcre_uchar *)PRIV(find_bracket)(codestart, utf, -1);
  8064. cc != NULL;
  8065. cc = (pcre_uchar *)PRIV(find_bracket)(cc, utf, -1))
  8066. {
  8067. if (GET(cc, 1) == 0)
  8068. {
  8069. int fixed_length;
  8070. pcre_uchar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);
  8071. int end_op = *be;
  8072. *be = OP_END;
  8073. fixed_length = find_fixedlength(cc, (re->options & PCRE_UTF8) != 0, TRUE,
  8074. cd, NULL);
  8075. *be = end_op;
  8076. DPRINTF(("fixed length = %d\n", fixed_length));
  8077. if (fixed_length < 0)
  8078. {
  8079. errorcode = (fixed_length == -2)? ERR36 :
  8080. (fixed_length == -4)? ERR70 : ERR25;
  8081. break;
  8082. }
  8083. if (fixed_length > cd->max_lookbehind) cd->max_lookbehind = fixed_length;
  8084. PUT(cc, 1, fixed_length);
  8085. }
  8086. cc += 1 + LINK_SIZE;
  8087. }
  8088. }
  8089. /* Failed to compile, or error while post-processing */
  8090. if (errorcode != 0)
  8091. {
  8092. (PUBL(free))(re);
  8093. PCRE_EARLY_ERROR_RETURN:
  8094. if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
  8095. (PUBL(free))((void *)cd->named_groups);
  8096. *erroroffset = (int)(ptr - (const pcre_uchar *)pattern);
  8097. PCRE_EARLY_ERROR_RETURN2:
  8098. *errorptr = find_error_text(errorcode);
  8099. if (errorcodeptr != NULL) *errorcodeptr = errorcode;
  8100. return NULL;
  8101. }
  8102. /* If the anchored option was not passed, set the flag if we can determine that
  8103. the pattern is anchored by virtue of ^ characters or \A or anything else, such
  8104. as starting with non-atomic .* when DOTALL is set and there are no occurrences
  8105. of *PRUNE or *SKIP.
  8106. Otherwise, if we know what the first byte has to be, save it, because that
  8107. speeds up unanchored matches no end. If not, see if we can set the
  8108. PCRE_STARTLINE flag. This is helpful for multiline matches when all branches
  8109. start with ^. and also when all branches start with non-atomic .* for
  8110. non-DOTALL matches when *PRUNE and SKIP are not present. */
  8111. if ((re->options & PCRE_ANCHORED) == 0)
  8112. {
  8113. if (is_anchored(codestart, 0, cd, 0)) re->options |= PCRE_ANCHORED;
  8114. else
  8115. {
  8116. if (firstcharflags < 0)
  8117. firstchar = find_firstassertedchar(codestart, &firstcharflags, FALSE);
  8118. if (firstcharflags >= 0) /* Remove caseless flag for non-caseable chars */
  8119. {
  8120. #if defined COMPILE_PCRE8
  8121. re->first_char = firstchar & 0xff;
  8122. #elif defined COMPILE_PCRE16
  8123. re->first_char = firstchar & 0xffff;
  8124. #elif defined COMPILE_PCRE32
  8125. re->first_char = firstchar;
  8126. #endif
  8127. if ((firstcharflags & REQ_CASELESS) != 0)
  8128. {
  8129. #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
  8130. /* We ignore non-ASCII first chars in 8 bit mode. */
  8131. if (utf)
  8132. {
  8133. if (re->first_char < 128)
  8134. {
  8135. if (cd->fcc[re->first_char] != re->first_char)
  8136. re->flags |= PCRE_FCH_CASELESS;
  8137. }
  8138. else if (UCD_OTHERCASE(re->first_char) != re->first_char)
  8139. re->flags |= PCRE_FCH_CASELESS;
  8140. }
  8141. else
  8142. #endif
  8143. if (MAX_255(re->first_char)
  8144. && cd->fcc[re->first_char] != re->first_char)
  8145. re->flags |= PCRE_FCH_CASELESS;
  8146. }
  8147. re->flags |= PCRE_FIRSTSET;
  8148. }
  8149. else if (is_startline(codestart, 0, cd, 0, FALSE)) re->flags |= PCRE_STARTLINE;
  8150. }
  8151. }
  8152. /* For an anchored pattern, we use the "required byte" only if it follows a
  8153. variable length item in the regex. Remove the caseless flag for non-caseable
  8154. bytes. */
  8155. if (reqcharflags >= 0 &&
  8156. ((re->options & PCRE_ANCHORED) == 0 || (reqcharflags & REQ_VARY) != 0))
  8157. {
  8158. #if defined COMPILE_PCRE8
  8159. re->req_char = reqchar & 0xff;
  8160. #elif defined COMPILE_PCRE16
  8161. re->req_char = reqchar & 0xffff;
  8162. #elif defined COMPILE_PCRE32
  8163. re->req_char = reqchar;
  8164. #endif
  8165. if ((reqcharflags & REQ_CASELESS) != 0)
  8166. {
  8167. #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
  8168. /* We ignore non-ASCII first chars in 8 bit mode. */
  8169. if (utf)
  8170. {
  8171. if (re->req_char < 128)
  8172. {
  8173. if (cd->fcc[re->req_char] != re->req_char)
  8174. re->flags |= PCRE_RCH_CASELESS;
  8175. }
  8176. else if (UCD_OTHERCASE(re->req_char) != re->req_char)
  8177. re->flags |= PCRE_RCH_CASELESS;
  8178. }
  8179. else
  8180. #endif
  8181. if (MAX_255(re->req_char) && cd->fcc[re->req_char] != re->req_char)
  8182. re->flags |= PCRE_RCH_CASELESS;
  8183. }
  8184. re->flags |= PCRE_REQCHSET;
  8185. }
  8186. /* Print out the compiled data if debugging is enabled. This is never the
  8187. case when building a production library. */
  8188. #ifdef PCRE_DEBUG
  8189. printf("Length = %d top_bracket = %d top_backref = %d\n",
  8190. length, re->top_bracket, re->top_backref);
  8191. printf("Options=%08x\n", re->options);
  8192. if ((re->flags & PCRE_FIRSTSET) != 0)
  8193. {
  8194. pcre_uchar ch = re->first_char;
  8195. const char *caseless =
  8196. ((re->flags & PCRE_FCH_CASELESS) == 0)? "" : " (caseless)";
  8197. if (PRINTABLE(ch)) printf("First char = %c%s\n", ch, caseless);
  8198. else printf("First char = \\x%02x%s\n", ch, caseless);
  8199. }
  8200. if ((re->flags & PCRE_REQCHSET) != 0)
  8201. {
  8202. pcre_uchar ch = re->req_char;
  8203. const char *caseless =
  8204. ((re->flags & PCRE_RCH_CASELESS) == 0)? "" : " (caseless)";
  8205. if (PRINTABLE(ch)) printf("Req char = %c%s\n", ch, caseless);
  8206. else printf("Req char = \\x%02x%s\n", ch, caseless);
  8207. }
  8208. #if defined COMPILE_PCRE8
  8209. pcre_printint((pcre *)re, stdout, TRUE);
  8210. #elif defined COMPILE_PCRE16
  8211. pcre16_printint((pcre *)re, stdout, TRUE);
  8212. #elif defined COMPILE_PCRE32
  8213. pcre32_printint((pcre *)re, stdout, TRUE);
  8214. #endif
  8215. /* This check is done here in the debugging case so that the code that
  8216. was compiled can be seen. */
  8217. if (code - codestart > length)
  8218. {
  8219. (PUBL(free))(re);
  8220. *errorptr = find_error_text(ERR23);
  8221. *erroroffset = ptr - (pcre_uchar *)pattern;
  8222. if (errorcodeptr != NULL) *errorcodeptr = ERR23;
  8223. return NULL;
  8224. }
  8225. #endif /* PCRE_DEBUG */
  8226. /* Check for a pattern than can match an empty string, so that this information
  8227. can be provided to applications. */
  8228. do
  8229. {
  8230. if (could_be_empty_branch(codestart, code, utf, cd, NULL))
  8231. {
  8232. re->flags |= PCRE_MATCH_EMPTY;
  8233. break;
  8234. }
  8235. codestart += GET(codestart, 1);
  8236. }
  8237. while (*codestart == OP_ALT);
  8238. #if defined COMPILE_PCRE8
  8239. return (pcre *)re;
  8240. #elif defined COMPILE_PCRE16
  8241. return (pcre16 *)re;
  8242. #elif defined COMPILE_PCRE32
  8243. return (pcre32 *)re;
  8244. #endif
  8245. }
  8246. /* End of pcre_compile.c */