pcre2_compile.c 323 KB

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