pcre_compile.c 313 KB

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