pcre2_compile.c 342 KB

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