pcre_compile.c 304 KB

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