pcre_exec.c 214 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222
  1. /* This module contains pcre_exec(), the externally visible function that does
  2. pattern matching using an NFA algorithm, trying to mimic Perl as closely as
  3. possible. There are also some static supporting functions. */
  4. #include "re_config.h"
  5. #define NLBLOCK md /* Block containing newline information */
  6. #define PSSTART start_subject /* Field containing processed string start */
  7. #define PSEND end_subject /* Field containing processed string end */
  8. #include "pcre_internal.h"
  9. /* Undefine some potentially clashing cpp symbols */
  10. #undef min
  11. #undef max
  12. /* Flag bits for the match() function */
  13. #define match_condassert 0x01 /* Called to check a condition assertion */
  14. #define match_cbegroup 0x02 /* Could-be-empty unlimited repeat group */
  15. /* Non-error returns from the match() function. Error returns are externally
  16. defined PCRE_ERROR_xxx codes, which are all negative. */
  17. #define MATCH_MATCH 1
  18. #define MATCH_NOMATCH 0
  19. /* Special internal returns from the match() function. Make them sufficiently
  20. negative to avoid the external error codes. */
  21. #define MATCH_COMMIT (-999)
  22. #define MATCH_PRUNE (-998)
  23. #define MATCH_SKIP (-997)
  24. #define MATCH_THEN (-996)
  25. /* Maximum number of ints of offset to save on the stack for recursive calls.
  26. If the offset vector is bigger, malloc is used. This should be a multiple of 3,
  27. because the offset vector is always a multiple of 3 long. */
  28. #define REC_STACK_SAVE_MAX 30
  29. /* Min and max values for the common repeats; for the maxima, 0 => infinity */
  30. static const char rep_min[] = {0, 0, 1, 1, 0, 0};
  31. static const char rep_max[] = {0, 0, 0, 0, 1, 1};
  32. #ifdef DEBUG
  33. /*************************************************
  34. * Debugging function to print chars *
  35. *************************************************/
  36. /* Print a sequence of chars in printable format, stopping at the end of the
  37. subject if the requested.
  38. Arguments:
  39. p points to characters
  40. length number to print
  41. is_subject TRUE if printing from within md->start_subject
  42. md pointer to matching data block, if is_subject is TRUE
  43. Returns: nothing
  44. */
  45. static void pchars(const uschar* p,
  46. int length,
  47. BOOL is_subject,
  48. match_data* md) {
  49. unsigned int c;
  50. if (is_subject && length > md->end_subject - p)
  51. length = md->end_subject - p;
  52. while (length-- > 0)
  53. if (isprint(c = *(p++)))
  54. printf("%c", c);
  55. else
  56. printf("\\x%02x", c);
  57. }
  58. #endif
  59. /*************************************************
  60. * Match a back-reference *
  61. *************************************************/
  62. /* If a back reference hasn't been set, the length that is passed is greater
  63. than the number of characters left in the string, so the match fails.
  64. Arguments:
  65. offset index into the offset vector
  66. eptr points into the subject
  67. length length to be matched
  68. md points to match data block
  69. ims the ims flags
  70. Returns: TRUE if matched
  71. */
  72. static BOOL match_ref(int offset,
  73. register USPTR eptr,
  74. int length,
  75. match_data* md,
  76. unsigned long int ims) {
  77. USPTR p = md->start_subject + md->offset_vector[offset];
  78. #ifdef DEBUG
  79. if (eptr >= md->end_subject)
  80. printf("matching subject <null>");
  81. else {
  82. printf("matching subject ");
  83. pchars(eptr, length, TRUE, md);
  84. }
  85. printf(" against backref ");
  86. pchars(p, length, FALSE, md);
  87. printf("\n");
  88. #endif
  89. /* Always fail if not enough characters left */
  90. if (length > md->end_subject - eptr)
  91. return FALSE;
  92. /* Separate the caselesss case for speed */
  93. if ((ims & PCRE_CASELESS) != 0) {
  94. while (length-- > 0)
  95. if (md->lcc[*p++] != md->lcc[*eptr++])
  96. return FALSE;
  97. } else {
  98. while (length-- > 0)
  99. if (*p++ != *eptr++)
  100. return FALSE;
  101. }
  102. return TRUE;
  103. }
  104. /***************************************************************************
  105. ****************************************************************************
  106. RECURSION IN THE match() FUNCTION
  107. The match() function is highly recursive, though not every recursive call
  108. increases the recursive depth. Nevertheless, some regular expressions can cause
  109. it to recurse to a great depth. I was writing for Unix, so I just let it call
  110. itself recursively. This uses the stack for saving everything that has to be
  111. saved for a recursive call. On Unix, the stack can be large, and this works
  112. fine.
  113. It turns out that on some non-Unix-like systems there are problems with
  114. programs that use a lot of stack. (This despite the fact that every last chip
  115. has oodles of memory these days, and techniques for extending the stack have
  116. been known for decades.) So....
  117. There is a fudge, triggered by defining NO_RECURSE, which avoids recursive
  118. calls by keeping local variables that need to be preserved in blocks of memory
  119. obtained from malloc() instead instead of on the stack. Macros are used to
  120. achieve this so that the actual code doesn't look very different to what it
  121. always used to.
  122. The original heap-recursive code used longjmp(). However, it seems that this
  123. can be very slow on some operating systems. Following a suggestion from Stan
  124. Switzer, the use of longjmp() has been abolished, at the cost of having to
  125. provide a unique number for each call to RMATCH. There is no way of generating
  126. a sequence of numbers at compile time in C. I have given them names, to make
  127. them stand out more clearly.
  128. Crude tests on x86 Linux show a small speedup of around 5-8%. However, on
  129. FreeBSD, avoiding longjmp() more than halves the time taken to run the standard
  130. tests. Furthermore, not using longjmp() means that local dynamic variables
  131. don't have indeterminate values; this has meant that the frame size can be
  132. reduced because the result can be "passed back" by straight setting of the
  133. variable instead of being passed in the frame.
  134. ****************************************************************************
  135. ***************************************************************************/
  136. /* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN
  137. below must be updated in sync. */
  138. enum {
  139. RM1 = 1,
  140. RM2,
  141. RM3,
  142. RM4,
  143. RM5,
  144. RM6,
  145. RM7,
  146. RM8,
  147. RM9,
  148. RM10,
  149. RM11,
  150. RM12,
  151. RM13,
  152. RM14,
  153. RM15,
  154. RM16,
  155. RM17,
  156. RM18,
  157. RM19,
  158. RM20,
  159. RM21,
  160. RM22,
  161. RM23,
  162. RM24,
  163. RM25,
  164. RM26,
  165. RM27,
  166. RM28,
  167. RM29,
  168. RM30,
  169. RM31,
  170. RM32,
  171. RM33,
  172. RM34,
  173. RM35,
  174. RM36,
  175. RM37,
  176. RM38,
  177. RM39,
  178. RM40,
  179. RM41,
  180. RM42,
  181. RM43,
  182. RM44,
  183. RM45,
  184. RM46,
  185. RM47,
  186. RM48,
  187. RM49,
  188. RM50,
  189. RM51,
  190. RM52,
  191. RM53,
  192. RM54
  193. };
  194. /* These versions of the macros use the stack, as normal. There are debugging
  195. versions and production versions. Note that the "rw" argument of RMATCH isn't
  196. actuall used in this definition. */
  197. #ifndef NO_RECURSE
  198. #define REGISTER register
  199. #ifdef DEBUG
  200. #define RMATCH(ra, rb, rc, rd, re, rf, rg, rw) \
  201. { \
  202. printf("match() called in line %d\n", __LINE__); \
  203. rrc = match(ra, rb, mstart, rc, rd, re, rf, rg, rdepth + 1); \
  204. printf("to line %d\n", __LINE__); \
  205. }
  206. #define RRETURN(ra) \
  207. { \
  208. printf("match() returned %d from line %d ", ra, __LINE__); \
  209. return ra; \
  210. }
  211. #else
  212. #define RMATCH(ra, rb, rc, rd, re, rf, rg, rw) \
  213. rrc = match(ra, rb, mstart, rc, rd, re, rf, rg, rdepth + 1)
  214. #define RRETURN(ra) return ra
  215. #endif
  216. #else
  217. /* These versions of the macros manage a private stack on the heap. Note that
  218. the "rd" argument of RMATCH isn't actually used in this definition. It's the md
  219. argument of match(), which never changes. */
  220. #define REGISTER
  221. #define RMATCH(ra, rb, rc, rd, re, rf, rg, rw) \
  222. { \
  223. heapframe* newframe = (pcre_stack_malloc)(sizeof(heapframe)); \
  224. frame->Xwhere = rw; \
  225. newframe->Xeptr = ra; \
  226. newframe->Xecode = rb; \
  227. newframe->Xmstart = mstart; \
  228. newframe->Xoffset_top = rc; \
  229. newframe->Xims = re; \
  230. newframe->Xeptrb = rf; \
  231. newframe->Xflags = rg; \
  232. newframe->Xrdepth = frame->Xrdepth + 1; \
  233. newframe->Xprevframe = frame; \
  234. frame = newframe; \
  235. DPRINTF(("restarting from line %d\n", __LINE__)); \
  236. goto HEAP_RECURSE; \
  237. L_##rw : DPRINTF(("jumped back to line %d\n", __LINE__)); \
  238. }
  239. #define RRETURN(ra) \
  240. { \
  241. heapframe* newframe = frame; \
  242. frame = newframe->Xprevframe; \
  243. (pcre_stack_free)(newframe); \
  244. if (frame != NULL) { \
  245. rrc = ra; \
  246. goto HEAP_RETURN; \
  247. } \
  248. return ra; \
  249. }
  250. /* Structure for remembering the local variables in a private frame */
  251. typedef struct heapframe {
  252. struct heapframe* Xprevframe;
  253. /* Function arguments that may change */
  254. const uschar* Xeptr;
  255. const uschar* Xecode;
  256. const uschar* Xmstart;
  257. int Xoffset_top;
  258. long int Xims;
  259. eptrblock* Xeptrb;
  260. int Xflags;
  261. unsigned int Xrdepth;
  262. /* Function local variables */
  263. const uschar* Xcallpat;
  264. const uschar* Xcharptr;
  265. const uschar* Xdata;
  266. const uschar* Xnext;
  267. const uschar* Xpp;
  268. const uschar* Xprev;
  269. const uschar* Xsaved_eptr;
  270. recursion_info Xnew_recursive;
  271. BOOL Xcur_is_word;
  272. BOOL Xcondition;
  273. BOOL Xprev_is_word;
  274. unsigned long int Xoriginal_ims;
  275. #ifdef SUPPORT_UCP
  276. int Xprop_type;
  277. int Xprop_value;
  278. int Xprop_fail_result;
  279. int Xprop_category;
  280. int Xprop_chartype;
  281. int Xprop_script;
  282. int Xoclength;
  283. uschar Xocchars[8];
  284. #endif
  285. int Xctype;
  286. unsigned int Xfc;
  287. int Xfi;
  288. int Xlength;
  289. int Xmax;
  290. int Xmin;
  291. int Xnumber;
  292. int Xoffset;
  293. int Xop;
  294. int Xsave_capture_last;
  295. int Xsave_offset1, Xsave_offset2, Xsave_offset3;
  296. int Xstacksave[REC_STACK_SAVE_MAX];
  297. eptrblock Xnewptrb;
  298. /* Where to jump back to */
  299. int Xwhere;
  300. } heapframe;
  301. #endif
  302. /***************************************************************************
  303. ***************************************************************************/
  304. /*************************************************
  305. * Match from current position *
  306. *************************************************/
  307. /* This function is called recursively in many circumstances. Whenever it
  308. returns a negative (error) response, the outer incarnation must also return the
  309. same response.
  310. Performance note: It might be tempting to extract commonly used fields from the
  311. md structure (e.g. utf8, end_subject) into individual variables to improve
  312. performance. Tests using gcc on a SPARC disproved this; in the first case, it
  313. made performance worse.
  314. Arguments:
  315. eptr pointer to current character in subject
  316. ecode pointer to current position in compiled code
  317. mstart pointer to the current match start position (can be modified
  318. by encountering \K)
  319. offset_top current top pointer
  320. md pointer to "static" info for the match
  321. ims current /i, /m, and /s options
  322. eptrb pointer to chain of blocks containing eptr at start of
  323. brackets - for testing for empty matches
  324. flags can contain
  325. match_condassert - this is an assertion condition
  326. match_cbegroup - this is the start of an unlimited repeat
  327. group that can match an empty string
  328. rdepth the recursion depth
  329. Returns: MATCH_MATCH if matched ) these values are >= 0
  330. MATCH_NOMATCH if failed to match )
  331. a negative PCRE_ERROR_xxx value if aborted by an error condition
  332. (e.g. stopped by repeated call or recursion limit)
  333. */
  334. static int match(REGISTER USPTR eptr,
  335. REGISTER const uschar* ecode,
  336. const uschar* mstart,
  337. int offset_top,
  338. match_data* md,
  339. unsigned long int ims,
  340. eptrblock* eptrb,
  341. int flags,
  342. unsigned int rdepth) {
  343. /* These variables do not need to be preserved over recursion in this
  344. function, so they can be ordinary variables in all cases. Mark some of them
  345. with "register" because they are used a lot in loops. */
  346. register int rrc; /* Returns from recursive calls */
  347. register int i; /* Used for loops not involving calls to RMATCH() */
  348. register unsigned int c; /* Character values not kept over RMATCH() calls */
  349. register BOOL utf8; /* Local copy of UTF-8 flag for speed */
  350. BOOL minimize, possessive; /* Quantifier options */
  351. /* When recursion is not being used, all "local" variables that have to be
  352. preserved over calls to RMATCH() are part of a "frame" which is obtained
  353. from heap storage. Set up the top-level frame here; others are obtained from
  354. the heap whenever RMATCH() does a "recursion". See the macro definitions
  355. above. */
  356. #ifdef NO_RECURSE
  357. heapframe* frame = (pcre_stack_malloc)(sizeof(heapframe));
  358. frame->Xprevframe = NULL; /* Marks the top level */
  359. /* Copy in the original argument variables */
  360. frame->Xeptr = eptr;
  361. frame->Xecode = ecode;
  362. frame->Xmstart = mstart;
  363. frame->Xoffset_top = offset_top;
  364. frame->Xims = ims;
  365. frame->Xeptrb = eptrb;
  366. frame->Xflags = flags;
  367. frame->Xrdepth = rdepth;
  368. /* This is where control jumps back to to effect "recursion" */
  369. HEAP_RECURSE:
  370. /* Macros make the argument variables come from the current frame */
  371. #define eptr frame->Xeptr
  372. #define ecode frame->Xecode
  373. #define mstart frame->Xmstart
  374. #define offset_top frame->Xoffset_top
  375. #define ims frame->Xims
  376. #define eptrb frame->Xeptrb
  377. #define flags frame->Xflags
  378. #define rdepth frame->Xrdepth
  379. /* Ditto for the local variables */
  380. #ifdef SUPPORT_UTF8
  381. #define charptr frame->Xcharptr
  382. #endif
  383. #define callpat frame->Xcallpat
  384. #define data frame->Xdata
  385. #define next frame->Xnext
  386. #define pp frame->Xpp
  387. #define prev frame->Xprev
  388. #define saved_eptr frame->Xsaved_eptr
  389. #define new_recursive frame->Xnew_recursive
  390. #define cur_is_word frame->Xcur_is_word
  391. #define condition frame->Xcondition
  392. #define prev_is_word frame->Xprev_is_word
  393. #define original_ims frame->Xoriginal_ims
  394. #ifdef SUPPORT_UCP
  395. #define prop_type frame->Xprop_type
  396. #define prop_value frame->Xprop_value
  397. #define prop_fail_result frame->Xprop_fail_result
  398. #define prop_category frame->Xprop_category
  399. #define prop_chartype frame->Xprop_chartype
  400. #define prop_script frame->Xprop_script
  401. #define oclength frame->Xoclength
  402. #define occhars frame->Xocchars
  403. #endif
  404. #define ctype frame->Xctype
  405. #define fc frame->Xfc
  406. #define fi frame->Xfi
  407. #define length frame->Xlength
  408. #define max frame->Xmax
  409. #define min frame->Xmin
  410. #define number frame->Xnumber
  411. #define offset frame->Xoffset
  412. #define op frame->Xop
  413. #define save_capture_last frame->Xsave_capture_last
  414. #define save_offset1 frame->Xsave_offset1
  415. #define save_offset2 frame->Xsave_offset2
  416. #define save_offset3 frame->Xsave_offset3
  417. #define stacksave frame->Xstacksave
  418. #define newptrb frame->Xnewptrb
  419. /* When recursion is being used, local variables are allocated on the stack
  420. and get preserved during recursion in the normal way. In this environment,
  421. fi and i, and fc and c, can be the same variables. */
  422. #else /* NO_RECURSE not defined */
  423. #define fi i
  424. #define fc c
  425. #ifdef SUPPORT_UTF8 /* Many of these variables are used only */
  426. const uschar* charptr; /* in small blocks of the code. My normal */
  427. #endif /* style of coding would have declared */
  428. const uschar* callpat; /* them within each of those blocks. */
  429. const uschar* data; /* However, in order to accommodate the */
  430. const uschar* next; /* version of this code that uses an */
  431. USPTR pp; /* external "stack" implemented on the */
  432. const uschar* prev; /* heap, it is easier to declare them all */
  433. USPTR saved_eptr; /* here, so the declarations can be cut */
  434. /* out in a block. The only declarations */
  435. recursion_info new_recursive; /* within blocks below are for variables */
  436. /* that do not have to be preserved over */
  437. BOOL cur_is_word; /* a recursive call to RMATCH(). */
  438. BOOL condition;
  439. BOOL prev_is_word;
  440. unsigned long int original_ims;
  441. #ifdef SUPPORT_UCP
  442. int prop_type;
  443. int prop_value;
  444. int prop_fail_result;
  445. int prop_category;
  446. int prop_chartype;
  447. int prop_script;
  448. int oclength;
  449. uschar occhars[8];
  450. #endif
  451. int ctype;
  452. int length;
  453. int max;
  454. int min;
  455. int number;
  456. int offset;
  457. int op;
  458. int save_capture_last;
  459. int save_offset1, save_offset2, save_offset3;
  460. int stacksave[REC_STACK_SAVE_MAX];
  461. eptrblock newptrb;
  462. #endif /* NO_RECURSE */
  463. /* These statements are here to stop the compiler complaining about
  464. unitialized variables. */
  465. #ifdef SUPPORT_UCP
  466. prop_value = 0;
  467. prop_fail_result = 0;
  468. #endif
  469. /* This label is used for tail recursion, which is used in a few cases even
  470. when NO_RECURSE is not defined, in order to reduce the amount of stack that
  471. is used. Thanks to Ian Taylor for noticing this possibility and sending the
  472. original patch. */
  473. TAIL_RECURSE:
  474. /* OK, now we can get on with the real code of the function. Recursive calls
  475. are specified by the macro RMATCH and RRETURN is used to return. When
  476. NO_RECURSE is *not* defined, these just turn into a recursive call to
  477. match() and a "return", respectively (possibly with some debugging if DEBUG
  478. is defined). However, RMATCH isn't like a function call because it's quite a
  479. complicated macro. It has to be used in one particular way. This shouldn't,
  480. however, impact performance when true recursion is being used. */
  481. #ifdef SUPPORT_UTF8
  482. utf8 = md->utf8; /* Local copy of the flag */
  483. #else
  484. utf8 = FALSE;
  485. #endif
  486. /* First check that we haven't called match() too many times, or that we
  487. haven't exceeded the recursive call limit. */
  488. if (md->match_call_count++ >= md->match_limit)
  489. RRETURN(PCRE_ERROR_MATCHLIMIT);
  490. if (rdepth >= md->match_limit_recursion)
  491. RRETURN(PCRE_ERROR_RECURSIONLIMIT);
  492. original_ims = ims; /* Save for resetting on ')' */
  493. /* At the start of a group with an unlimited repeat that may match an empty
  494. string, the match_cbegroup flag is set. When this is the case, add the
  495. current subject pointer to the chain of such remembered pointers, to be
  496. checked when we hit the closing ket, in order to break infinite loops that
  497. match no characters. When match() is called in other circumstances, don't
  498. add to the chain. The match_cbegroup flag must NOT be used with tail
  499. recursion, because the memory block that is used is on the stack, so a new
  500. one may be required for each match(). */
  501. if ((flags & match_cbegroup) != 0) {
  502. newptrb.epb_saved_eptr = eptr;
  503. newptrb.epb_prev = eptrb;
  504. eptrb = &newptrb;
  505. }
  506. /* Now start processing the opcodes. */
  507. for (;;) {
  508. minimize = possessive = FALSE;
  509. op = *ecode;
  510. /* For partial matching, remember if we ever hit the end of the subject
  511. after matching at least one subject character. */
  512. if (md->partial && eptr >= md->end_subject && eptr > mstart)
  513. md->hitend = TRUE;
  514. switch (op) {
  515. case OP_FAIL:
  516. RRETURN(MATCH_NOMATCH);
  517. case OP_PRUNE:
  518. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
  519. ims, eptrb, flags, RM51);
  520. if (rrc != MATCH_NOMATCH)
  521. RRETURN(rrc);
  522. RRETURN(MATCH_PRUNE);
  523. case OP_COMMIT:
  524. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
  525. ims, eptrb, flags, RM52);
  526. if (rrc != MATCH_NOMATCH)
  527. RRETURN(rrc);
  528. RRETURN(MATCH_COMMIT);
  529. case OP_SKIP:
  530. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
  531. ims, eptrb, flags, RM53);
  532. if (rrc != MATCH_NOMATCH)
  533. RRETURN(rrc);
  534. md->start_match_ptr = eptr; /* Pass back current position */
  535. RRETURN(MATCH_SKIP);
  536. case OP_THEN:
  537. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
  538. ims, eptrb, flags, RM54);
  539. if (rrc != MATCH_NOMATCH)
  540. RRETURN(rrc);
  541. RRETURN(MATCH_THEN);
  542. /* Handle a capturing bracket. If there is space in the offset
  543. vector, save the current subject position in the working slot at
  544. the top of the vector. We mustn't change the current values of
  545. the data slot, because they may be set from a previous iteration
  546. of this group, and be referred to by a reference inside the
  547. group.
  548. If the bracket fails to match, we need to restore this value and
  549. also the values of the final offsets, in case they were set by a
  550. previous iteration of the same bracket.
  551. If there isn't enough space in the offset vector, treat this as
  552. if it were a non-capturing bracket. Don't worry about setting
  553. the flag for the error case here; that is handled in the code
  554. for KET. */
  555. case OP_CBRA:
  556. case OP_SCBRA:
  557. number = GET2(ecode, 1 + LINK_SIZE);
  558. offset = number << 1;
  559. #ifdef DEBUG
  560. printf("start bracket %d\n", number);
  561. printf("subject=");
  562. pchars(eptr, 16, TRUE, md);
  563. printf("\n");
  564. #endif
  565. if (offset < md->offset_max) {
  566. save_offset1 = md->offset_vector[offset];
  567. save_offset2 = md->offset_vector[offset + 1];
  568. save_offset3 = md->offset_vector[md->offset_end - number];
  569. save_capture_last = md->capture_last;
  570. DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2,
  571. save_offset3));
  572. md->offset_vector[md->offset_end - number] =
  573. eptr - md->start_subject;
  574. flags = (op == OP_SCBRA) ? match_cbegroup : 0;
  575. do {
  576. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode],
  577. offset_top, md, ims, eptrb, flags, RM1);
  578. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
  579. RRETURN(rrc);
  580. md->capture_last = save_capture_last;
  581. ecode += GET(ecode, 1);
  582. } while (*ecode == OP_ALT);
  583. DPRINTF(("bracket %d failed\n", number));
  584. md->offset_vector[offset] = save_offset1;
  585. md->offset_vector[offset + 1] = save_offset2;
  586. md->offset_vector[md->offset_end - number] = save_offset3;
  587. RRETURN(MATCH_NOMATCH);
  588. }
  589. /* FALL THROUGH ... Insufficient room for saving captured
  590. contents. Treat as a non-capturing bracket. */
  591. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  592. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  593. DPRINTF(
  594. ("insufficient capture room: treat as non-capturing\n"));
  595. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  596. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  597. /* Non-capturing bracket. Loop for all the alternatives. When we
  598. get to the final alternative within the brackets, we would
  599. return the result of a recursive call to match() whatever
  600. happened. We can reduce stack usage by turning this into a tail
  601. recursion, except in the case when match_cbegroup is set.*/
  602. case OP_BRA:
  603. case OP_SBRA:
  604. DPRINTF(("start non-capturing bracket\n"));
  605. flags = (op >= OP_SBRA) ? match_cbegroup : 0;
  606. for (;;) {
  607. if (ecode[GET(ecode, 1)] != OP_ALT) /* Final alternative */
  608. {
  609. if (flags == 0) /* Not a possibly empty group */
  610. {
  611. ecode += _pcre_OP_lengths[*ecode];
  612. DPRINTF(("bracket 0 tail recursion\n"));
  613. goto TAIL_RECURSE;
  614. }
  615. /* Possibly empty group; can't use tail recursion. */
  616. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode],
  617. offset_top, md, ims, eptrb, flags, RM48);
  618. RRETURN(rrc);
  619. }
  620. /* For non-final alternatives, continue the loop for a
  621. NOMATCH result; otherwise return. */
  622. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top,
  623. md, ims, eptrb, flags, RM2);
  624. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
  625. RRETURN(rrc);
  626. ecode += GET(ecode, 1);
  627. }
  628. /* Control never reaches here. */
  629. /* Conditional group: compilation checked that there are no more
  630. than two branches. If the condition is false, skipping the first
  631. branch takes us past the end if there is only one branch, but
  632. that's OK because that is exactly what going to the ket would
  633. do. As there is only one branch to be obeyed, we can use tail
  634. recursion to avoid using another stack frame. */
  635. case OP_COND:
  636. case OP_SCOND:
  637. if (ecode[LINK_SIZE + 1] == OP_RREF) /* Recursion test */
  638. {
  639. offset =
  640. GET2(ecode, LINK_SIZE + 2); /* Recursion group number*/
  641. condition = md->recursive != NULL &&
  642. (offset == RREF_ANY ||
  643. offset == md->recursive->group_num);
  644. ecode += condition ? 3 : GET(ecode, 1);
  645. }
  646. else if (ecode[LINK_SIZE + 1] == OP_CREF) /* Group used test */
  647. {
  648. offset = GET2(ecode, LINK_SIZE + 2)
  649. << 1; /* Doubled ref number */
  650. condition =
  651. offset < offset_top && md->offset_vector[offset] >= 0;
  652. ecode += condition ? 3 : GET(ecode, 1);
  653. }
  654. else if (ecode[LINK_SIZE + 1] ==
  655. OP_DEF) /* DEFINE - always false */
  656. {
  657. condition = FALSE;
  658. ecode += GET(ecode, 1);
  659. }
  660. /* The condition is an assertion. Call match() to evaluate it -
  661. setting the final argument match_condassert causes it to stop at
  662. the end of an assertion. */
  663. else {
  664. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,
  665. NULL, match_condassert, RM3);
  666. if (rrc == MATCH_MATCH) {
  667. condition = TRUE;
  668. ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
  669. while (*ecode == OP_ALT)
  670. ecode += GET(ecode, 1);
  671. } else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) {
  672. RRETURN(
  673. rrc); /* Need braces because of following else */
  674. } else {
  675. condition = FALSE;
  676. ecode += GET(ecode, 1);
  677. }
  678. }
  679. /* We are now at the branch that is to be obeyed. As there is
  680. only one, we can use tail recursion to avoid using another stack
  681. frame, except when match_cbegroup is required for an unlimited
  682. repeat of a possibly empty group. If the second alternative
  683. doesn't exist, we can just plough on. */
  684. if (condition || *ecode == OP_ALT) {
  685. ecode += 1 + LINK_SIZE;
  686. if (op == OP_SCOND) /* Possibly empty group */
  687. {
  688. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  689. match_cbegroup, RM49);
  690. RRETURN(rrc);
  691. } else /* Group must match something */
  692. {
  693. flags = 0;
  694. goto TAIL_RECURSE;
  695. }
  696. } else /* Condition false & no 2nd alternative */
  697. {
  698. ecode += 1 + LINK_SIZE;
  699. }
  700. break;
  701. /* End of the pattern, either real or forced. If we are in a
  702. top-level recursion, we should restore the offsets appropriately
  703. and continue from after the call. */
  704. case OP_ACCEPT:
  705. case OP_END:
  706. if (md->recursive != NULL && md->recursive->group_num == 0) {
  707. recursion_info* rec = md->recursive;
  708. DPRINTF(("End of pattern in a (?0) recursion\n"));
  709. md->recursive = rec->prevrec;
  710. memmove(md->offset_vector, rec->offset_save,
  711. rec->saved_max * sizeof(int));
  712. mstart = rec->save_start;
  713. ims = original_ims;
  714. ecode = rec->after_call;
  715. break;
  716. }
  717. /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched
  718. an empty string - backtracking will then try other alternatives,
  719. if any. */
  720. if (md->notempty && eptr == mstart)
  721. RRETURN(MATCH_NOMATCH);
  722. md->end_match_ptr = eptr; /* Record where we ended */
  723. md->end_offset_top =
  724. offset_top; /* and how many extracts were taken */
  725. md->start_match_ptr =
  726. mstart; /* and the start (\K can modify) */
  727. RRETURN(MATCH_MATCH);
  728. /* Change option settings */
  729. case OP_OPT:
  730. ims = ecode[1];
  731. ecode += 2;
  732. DPRINTF(("ims set to %02lx\n", ims));
  733. break;
  734. /* Assertion brackets. Check the alternative branches in turn -
  735. the matching won't pass the KET for an assertion. If any one
  736. branch matches, the assertion is true. Lookbehind assertions
  737. have an OP_REVERSE item at the start of each branch to move the
  738. current point backwards, so the code at this level is identical
  739. to the lookahead case. */
  740. case OP_ASSERT:
  741. case OP_ASSERTBACK:
  742. do {
  743. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,
  744. NULL, 0, RM4);
  745. if (rrc == MATCH_MATCH)
  746. break;
  747. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
  748. RRETURN(rrc);
  749. ecode += GET(ecode, 1);
  750. } while (*ecode == OP_ALT);
  751. if (*ecode == OP_KET)
  752. RRETURN(MATCH_NOMATCH);
  753. /* If checking an assertion for a condition, return MATCH_MATCH.
  754. */
  755. if ((flags & match_condassert) != 0)
  756. RRETURN(MATCH_MATCH);
  757. /* Continue from after the assertion, updating the offsets high
  758. water mark, since extracts may have been taken during the
  759. assertion. */
  760. do
  761. ecode += GET(ecode, 1);
  762. while (*ecode == OP_ALT);
  763. ecode += 1 + LINK_SIZE;
  764. offset_top = md->end_offset_top;
  765. continue;
  766. /* Negative assertion: all branches must fail to match */
  767. case OP_ASSERT_NOT:
  768. case OP_ASSERTBACK_NOT:
  769. do {
  770. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,
  771. NULL, 0, RM5);
  772. if (rrc == MATCH_MATCH)
  773. RRETURN(MATCH_NOMATCH);
  774. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
  775. RRETURN(rrc);
  776. ecode += GET(ecode, 1);
  777. } while (*ecode == OP_ALT);
  778. if ((flags & match_condassert) != 0)
  779. RRETURN(MATCH_MATCH);
  780. ecode += 1 + LINK_SIZE;
  781. continue;
  782. /* Move the subject pointer back. This occurs only at the start
  783. of each branch of a lookbehind assertion. If we are too close to
  784. the start to move back, this match function fails. When working
  785. with UTF-8 we move back a number of characters, not bytes. */
  786. case OP_REVERSE:
  787. #ifdef SUPPORT_UTF8
  788. if (utf8) {
  789. i = GET(ecode, 1);
  790. while (i-- > 0) {
  791. eptr--;
  792. if (eptr < md->start_subject)
  793. RRETURN(MATCH_NOMATCH);
  794. BACKCHAR(eptr);
  795. }
  796. } else
  797. #endif
  798. /* No UTF-8 support, or not in UTF-8 mode: count is byte count
  799. */
  800. {
  801. eptr -= GET(ecode, 1);
  802. if (eptr < md->start_subject)
  803. RRETURN(MATCH_NOMATCH);
  804. }
  805. /* Skip to next op code */
  806. ecode += 1 + LINK_SIZE;
  807. break;
  808. /* The callout item calls an external function, if one is
  809. provided, passing details of the match so far. This is mainly
  810. for debugging, though the function is able to force a failure.
  811. */
  812. case OP_CALLOUT:
  813. if (pcre_callout != NULL) {
  814. pcre_callout_block cb;
  815. cb.version = 1; /* Version 1 of the callout block */
  816. cb.callout_number = ecode[1];
  817. cb.offset_vector = md->offset_vector;
  818. cb.subject = (PCRE_SPTR)md->start_subject;
  819. cb.subject_length = md->end_subject - md->start_subject;
  820. cb.start_match = mstart - md->start_subject;
  821. cb.current_position = eptr - md->start_subject;
  822. cb.pattern_position = GET(ecode, 2);
  823. cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
  824. cb.capture_top = offset_top / 2;
  825. cb.capture_last = md->capture_last;
  826. cb.callout_data = md->callout_data;
  827. if ((rrc = (*pcre_callout)(&cb)) > 0)
  828. RRETURN(MATCH_NOMATCH);
  829. if (rrc < 0)
  830. RRETURN(rrc);
  831. }
  832. ecode += 2 + 2 * LINK_SIZE;
  833. break;
  834. /* Recursion either matches the current regex, or some
  835. subexpression. The offset data is the offset to the starting
  836. bracket from the start of the whole pattern. (This is so that it
  837. works from duplicated subpatterns.)
  838. If there are any capturing brackets started but not finished, we
  839. have to save their starting points and reinstate them after the
  840. recursion. However, we don't know how many such there are
  841. (offset_top records the completed total) so we just have to save
  842. all the potential data. There may be up to 65535 such values,
  843. which is too large to put on the stack, but using malloc for
  844. small numbers seems expensive. As a compromise, the stack is
  845. used when there are no more than REC_STACK_SAVE_MAX values to
  846. store; otherwise malloc is used. A problem is what to do if the
  847. malloc fails ... there is no way of returning to the top level
  848. with an error. Save the top REC_STACK_SAVE_MAX values on the
  849. stack, and accept that the rest may be wrong.
  850. There are also other values that have to be saved. We use a
  851. chained sequence of blocks that actually live on the stack.
  852. Thanks to Robin Houston for the original version of this logic.
  853. */
  854. case OP_RECURSE: {
  855. callpat = md->start_code + GET(ecode, 1);
  856. new_recursive.group_num = (callpat == md->start_code)
  857. ? 0
  858. : GET2(callpat, 1 + LINK_SIZE);
  859. /* Add to "recursing stack" */
  860. new_recursive.prevrec = md->recursive;
  861. md->recursive = &new_recursive;
  862. /* Find where to continue from afterwards */
  863. ecode += 1 + LINK_SIZE;
  864. new_recursive.after_call = ecode;
  865. /* Now save the offset data. */
  866. new_recursive.saved_max = md->offset_end;
  867. if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)
  868. new_recursive.offset_save = stacksave;
  869. else {
  870. new_recursive.offset_save =
  871. (int*)(pcre_malloc)(new_recursive.saved_max *
  872. sizeof(int));
  873. if (new_recursive.offset_save == NULL)
  874. RRETURN(PCRE_ERROR_NOMEMORY);
  875. }
  876. memcpy(new_recursive.offset_save, md->offset_vector,
  877. new_recursive.saved_max * sizeof(int));
  878. new_recursive.save_start = mstart;
  879. mstart = eptr;
  880. /* OK, now we can do the recursion. For each top-level
  881. alternative we restore the offset and recursion data. */
  882. DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
  883. flags = (*callpat >= OP_SBRA) ? match_cbegroup : 0;
  884. do {
  885. RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat],
  886. offset_top, md, ims, eptrb, flags, RM6);
  887. if (rrc == MATCH_MATCH) {
  888. DPRINTF(("Recursion matched\n"));
  889. md->recursive = new_recursive.prevrec;
  890. if (new_recursive.offset_save != stacksave)
  891. (pcre_free)(new_recursive.offset_save);
  892. RRETURN(MATCH_MATCH);
  893. } else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) {
  894. DPRINTF(("Recursion gave error %d\n", rrc));
  895. RRETURN(rrc);
  896. }
  897. md->recursive = &new_recursive;
  898. memcpy(md->offset_vector, new_recursive.offset_save,
  899. new_recursive.saved_max * sizeof(int));
  900. callpat += GET(callpat, 1);
  901. } while (*callpat == OP_ALT);
  902. DPRINTF(("Recursion didn't match\n"));
  903. md->recursive = new_recursive.prevrec;
  904. if (new_recursive.offset_save != stacksave)
  905. (pcre_free)(new_recursive.offset_save);
  906. RRETURN(MATCH_NOMATCH);
  907. }
  908. /* Control never reaches here */
  909. /* "Once" brackets are like assertion brackets except that after
  910. a match, the point in the subject string is not moved back. Thus
  911. there can never be a move back into the brackets. Friedl calls
  912. these "atomic" subpatterns. Check the alternative branches in
  913. turn - the matching won't pass the KET for this kind of
  914. subpattern. If any one branch matches, we carry on as at the end
  915. of a normal bracket, leaving the subject pointer. */
  916. case OP_ONCE:
  917. prev = ecode;
  918. saved_eptr = eptr;
  919. do {
  920. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,
  921. eptrb, 0, RM7);
  922. if (rrc == MATCH_MATCH)
  923. break;
  924. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
  925. RRETURN(rrc);
  926. ecode += GET(ecode, 1);
  927. } while (*ecode == OP_ALT);
  928. /* If hit the end of the group (which could be repeated), fail
  929. */
  930. if (*ecode != OP_ONCE && *ecode != OP_ALT)
  931. RRETURN(MATCH_NOMATCH);
  932. /* Continue as from after the assertion, updating the offsets
  933. high water mark, since extracts may have been taken. */
  934. do
  935. ecode += GET(ecode, 1);
  936. while (*ecode == OP_ALT);
  937. offset_top = md->end_offset_top;
  938. eptr = md->end_match_ptr;
  939. /* For a non-repeating ket, just continue at this level. This
  940. also happens for a repeating ket if no characters were matched
  941. in the group. This is the forcible breaking of infinite loops as
  942. implemented in Perl 5.005. If there is an options reset, it will
  943. get obeyed in the normal course of events. */
  944. if (*ecode == OP_KET || eptr == saved_eptr) {
  945. ecode += 1 + LINK_SIZE;
  946. break;
  947. }
  948. /* The repeating kets try the rest of the pattern or restart
  949. from the preceding bracket, in the appropriate order. The second
  950. "call" of match() uses tail recursion, to avoid using another
  951. stack frame. We need to reset any options that changed within
  952. the bracket before re-running it, so check the next opcode. */
  953. if (ecode[1 + LINK_SIZE] == OP_OPT) {
  954. ims = (ims & ~PCRE_IMS) | ecode[4];
  955. DPRINTF(("ims set to %02lx at group repeat\n", ims));
  956. }
  957. if (*ecode == OP_KETRMIN) {
  958. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,
  959. eptrb, 0, RM8);
  960. if (rrc != MATCH_NOMATCH)
  961. RRETURN(rrc);
  962. ecode = prev;
  963. flags = 0;
  964. goto TAIL_RECURSE;
  965. } else /* OP_KETRMAX */
  966. {
  967. RMATCH(eptr, prev, offset_top, md, ims, eptrb,
  968. match_cbegroup, RM9);
  969. if (rrc != MATCH_NOMATCH)
  970. RRETURN(rrc);
  971. ecode += 1 + LINK_SIZE;
  972. flags = 0;
  973. goto TAIL_RECURSE;
  974. }
  975. /* Control never gets here */
  976. /* An alternation is the end of a branch; scan along to find the
  977. end of the bracketed group and go to there. */
  978. case OP_ALT:
  979. do
  980. ecode += GET(ecode, 1);
  981. while (*ecode == OP_ALT);
  982. break;
  983. /* BRAZERO and BRAMINZERO occur just before a bracket group,
  984. indicating that it may occur zero times. It may repeat
  985. infinitely, or not at all - i.e. it could be ()* or ()? in the
  986. pattern. Brackets with fixed upper repeat limits are compiled as
  987. a number of copies, with the optional ones preceded by BRAZERO
  988. or BRAMINZERO. */
  989. case OP_BRAZERO: {
  990. next = ecode + 1;
  991. RMATCH(eptr, next, offset_top, md, ims, eptrb, 0, RM10);
  992. if (rrc != MATCH_NOMATCH)
  993. RRETURN(rrc);
  994. do
  995. next += GET(next, 1);
  996. while (*next == OP_ALT);
  997. ecode = next + 1 + LINK_SIZE;
  998. } break;
  999. case OP_BRAMINZERO: {
  1000. next = ecode + 1;
  1001. do
  1002. next += GET(next, 1);
  1003. while (*next == OP_ALT);
  1004. RMATCH(eptr, next + 1 + LINK_SIZE, offset_top, md, ims, eptrb,
  1005. 0, RM11);
  1006. if (rrc != MATCH_NOMATCH)
  1007. RRETURN(rrc);
  1008. ecode++;
  1009. } break;
  1010. /* End of a group, repeated or non-repeating. */
  1011. case OP_KET:
  1012. case OP_KETRMIN:
  1013. case OP_KETRMAX:
  1014. prev = ecode - GET(ecode, 1);
  1015. /* If this was a group that remembered the subject start, in
  1016. order to break infinite repeats of empty string matches,
  1017. retrieve the subject start from the chain. Otherwise, set it
  1018. NULL. */
  1019. if (*prev >= OP_SBRA) {
  1020. saved_eptr =
  1021. eptrb->epb_saved_eptr; /* Value at start of group */
  1022. eptrb = eptrb->epb_prev; /* Backup to previous group */
  1023. } else
  1024. saved_eptr = NULL;
  1025. /* If we are at the end of an assertion group, stop matching and
  1026. return MATCH_MATCH, but record the current high water mark for
  1027. use by positive assertions. Do this also for the "once" (atomic)
  1028. groups. */
  1029. if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
  1030. *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||
  1031. *prev == OP_ONCE) {
  1032. md->end_match_ptr = eptr; /* For ONCE */
  1033. md->end_offset_top = offset_top;
  1034. RRETURN(MATCH_MATCH);
  1035. }
  1036. /* For capturing groups we have to check the group number back
  1037. at the start and if necessary complete handling an extraction by
  1038. setting the offsets and bumping the high water mark. Note that
  1039. whole-pattern recursion is coded as a recurse into group 0, so
  1040. it won't be picked up here. Instead, we catch it when the OP_END
  1041. is reached. Other recursion is handled here. */
  1042. if (*prev == OP_CBRA || *prev == OP_SCBRA) {
  1043. number = GET2(prev, 1 + LINK_SIZE);
  1044. offset = number << 1;
  1045. #ifdef DEBUG
  1046. printf("end bracket %d", number);
  1047. printf("\n");
  1048. #endif
  1049. md->capture_last = number;
  1050. if (offset >= md->offset_max)
  1051. md->offset_overflow = TRUE;
  1052. else {
  1053. md->offset_vector[offset] =
  1054. md->offset_vector[md->offset_end - number];
  1055. md->offset_vector[offset + 1] =
  1056. eptr - md->start_subject;
  1057. if (offset_top <= offset)
  1058. offset_top = offset + 2;
  1059. }
  1060. /* Handle a recursively called group. Restore the offsets
  1061. appropriately and continue from after the call. */
  1062. if (md->recursive != NULL &&
  1063. md->recursive->group_num == number) {
  1064. recursion_info* rec = md->recursive;
  1065. DPRINTF(("Recursion (%d) succeeded - continuing\n",
  1066. number));
  1067. md->recursive = rec->prevrec;
  1068. mstart = rec->save_start;
  1069. memcpy(md->offset_vector, rec->offset_save,
  1070. rec->saved_max * sizeof(int));
  1071. ecode = rec->after_call;
  1072. ims = original_ims;
  1073. break;
  1074. }
  1075. }
  1076. /* For both capturing and non-capturing groups, reset the value
  1077. of the ims flags, in case they got changed during the group. */
  1078. ims = original_ims;
  1079. DPRINTF(("ims reset to %02lx\n", ims));
  1080. /* For a non-repeating ket, just continue at this level. This
  1081. also happens for a repeating ket if no characters were matched
  1082. in the group. This is the forcible breaking of infinite loops as
  1083. implemented in Perl 5.005. If there is an options reset, it will
  1084. get obeyed in the normal course of events. */
  1085. if (*ecode == OP_KET || eptr == saved_eptr) {
  1086. ecode += 1 + LINK_SIZE;
  1087. break;
  1088. }
  1089. /* The repeating kets try the rest of the pattern or restart
  1090. from the preceding bracket, in the appropriate order. In the
  1091. second case, we can use tail recursion to avoid using another
  1092. stack frame, unless we have an unlimited repeat of a group that
  1093. can match an empty string. */
  1094. flags = (*prev >= OP_SBRA) ? match_cbegroup : 0;
  1095. if (*ecode == OP_KETRMIN) {
  1096. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,
  1097. eptrb, 0, RM12);
  1098. if (rrc != MATCH_NOMATCH)
  1099. RRETURN(rrc);
  1100. if (flags != 0) /* Could match an empty string */
  1101. {
  1102. RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags,
  1103. RM50);
  1104. RRETURN(rrc);
  1105. }
  1106. ecode = prev;
  1107. goto TAIL_RECURSE;
  1108. } else /* OP_KETRMAX */
  1109. {
  1110. RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM13);
  1111. if (rrc != MATCH_NOMATCH)
  1112. RRETURN(rrc);
  1113. ecode += 1 + LINK_SIZE;
  1114. flags = 0;
  1115. goto TAIL_RECURSE;
  1116. }
  1117. /* Control never gets here */
  1118. /* Start of subject unless notbol, or after internal newline if
  1119. * multiline */
  1120. case OP_CIRC:
  1121. if (md->notbol && eptr == md->start_subject)
  1122. RRETURN(MATCH_NOMATCH);
  1123. if ((ims & PCRE_MULTILINE) != 0) {
  1124. if (eptr != md->start_subject &&
  1125. (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
  1126. RRETURN(MATCH_NOMATCH);
  1127. ecode++;
  1128. break;
  1129. }
  1130. /* ... else fall through */
  1131. /* Start of subject assertion */
  1132. case OP_SOD:
  1133. if (eptr != md->start_subject)
  1134. RRETURN(MATCH_NOMATCH);
  1135. ecode++;
  1136. break;
  1137. /* Start of match assertion */
  1138. case OP_SOM:
  1139. if (eptr != md->start_subject + md->start_offset)
  1140. RRETURN(MATCH_NOMATCH);
  1141. ecode++;
  1142. break;
  1143. /* Reset the start of match point */
  1144. case OP_SET_SOM:
  1145. mstart = eptr;
  1146. ecode++;
  1147. break;
  1148. /* Assert before internal newline if multiline, or before a
  1149. terminating newline unless endonly is set, else end of subject
  1150. unless noteol is set. */
  1151. case OP_DOLL:
  1152. if ((ims & PCRE_MULTILINE) != 0) {
  1153. if (eptr < md->end_subject) {
  1154. if (!IS_NEWLINE(eptr))
  1155. RRETURN(MATCH_NOMATCH);
  1156. } else {
  1157. if (md->noteol)
  1158. RRETURN(MATCH_NOMATCH);
  1159. }
  1160. ecode++;
  1161. break;
  1162. } else {
  1163. if (md->noteol)
  1164. RRETURN(MATCH_NOMATCH);
  1165. if (!md->endonly) {
  1166. if (eptr != md->end_subject &&
  1167. (!IS_NEWLINE(eptr) ||
  1168. eptr != md->end_subject - md->nllen))
  1169. RRETURN(MATCH_NOMATCH);
  1170. ecode++;
  1171. break;
  1172. }
  1173. }
  1174. /* ... else fall through for endonly */
  1175. /* End of subject assertion (\z) */
  1176. case OP_EOD:
  1177. if (eptr < md->end_subject)
  1178. RRETURN(MATCH_NOMATCH);
  1179. ecode++;
  1180. break;
  1181. /* End of subject or ending \n assertion (\Z) */
  1182. case OP_EODN:
  1183. if (eptr != md->end_subject &&
  1184. (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
  1185. RRETURN(MATCH_NOMATCH);
  1186. ecode++;
  1187. break;
  1188. /* Word boundary assertions */
  1189. case OP_NOT_WORD_BOUNDARY:
  1190. case OP_WORD_BOUNDARY: {
  1191. /* Find out if the previous and current characters are "word"
  1192. characters. It takes a bit more work in UTF-8 mode. Characters >
  1193. 255 are assumed to be "non-word" characters. */
  1194. #ifdef SUPPORT_UTF8
  1195. if (utf8) {
  1196. if (eptr == md->start_subject)
  1197. prev_is_word = FALSE;
  1198. else {
  1199. const uschar* lastptr = eptr - 1;
  1200. while ((*lastptr & 0xc0) == 0x80)
  1201. lastptr--;
  1202. GETCHAR(c, lastptr);
  1203. prev_is_word =
  1204. c < 256 && (md->ctypes[c] & ctype_word) != 0;
  1205. }
  1206. if (eptr >= md->end_subject)
  1207. cur_is_word = FALSE;
  1208. else {
  1209. GETCHAR(c, eptr);
  1210. cur_is_word =
  1211. c < 256 && (md->ctypes[c] & ctype_word) != 0;
  1212. }
  1213. } else
  1214. #endif
  1215. /* More streamlined when not in UTF-8 mode */
  1216. {
  1217. prev_is_word = (eptr != md->start_subject) &&
  1218. ((md->ctypes[eptr[-1]] & ctype_word) != 0);
  1219. cur_is_word = (eptr < md->end_subject) &&
  1220. ((md->ctypes[*eptr] & ctype_word) != 0);
  1221. }
  1222. /* Now see if the situation is what we want */
  1223. if ((*ecode++ == OP_WORD_BOUNDARY)
  1224. ? cur_is_word == prev_is_word
  1225. : cur_is_word != prev_is_word)
  1226. RRETURN(MATCH_NOMATCH);
  1227. } break;
  1228. /* Match a single character type; inline for speed */
  1229. case OP_ANY:
  1230. if ((ims & PCRE_DOTALL) == 0) {
  1231. if (IS_NEWLINE(eptr))
  1232. RRETURN(MATCH_NOMATCH);
  1233. }
  1234. if (eptr++ >= md->end_subject)
  1235. RRETURN(MATCH_NOMATCH);
  1236. if (utf8)
  1237. while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80)
  1238. eptr++;
  1239. ecode++;
  1240. break;
  1241. /* Match a single byte, even in UTF-8 mode. This opcode really
  1242. does match any byte, even newline, independent of the setting of
  1243. PCRE_DOTALL. */
  1244. case OP_ANYBYTE:
  1245. if (eptr++ >= md->end_subject)
  1246. RRETURN(MATCH_NOMATCH);
  1247. ecode++;
  1248. break;
  1249. case OP_NOT_DIGIT:
  1250. if (eptr >= md->end_subject)
  1251. RRETURN(MATCH_NOMATCH);
  1252. GETCHARINCTEST(c, eptr);
  1253. if (
  1254. #ifdef SUPPORT_UTF8
  1255. c < 256 &&
  1256. #endif
  1257. (md->ctypes[c] & ctype_digit) != 0)
  1258. RRETURN(MATCH_NOMATCH);
  1259. ecode++;
  1260. break;
  1261. case OP_DIGIT:
  1262. if (eptr >= md->end_subject)
  1263. RRETURN(MATCH_NOMATCH);
  1264. GETCHARINCTEST(c, eptr);
  1265. if (
  1266. #ifdef SUPPORT_UTF8
  1267. c >= 256 ||
  1268. #endif
  1269. (md->ctypes[c] & ctype_digit) == 0)
  1270. RRETURN(MATCH_NOMATCH);
  1271. ecode++;
  1272. break;
  1273. case OP_NOT_WHITESPACE:
  1274. if (eptr >= md->end_subject)
  1275. RRETURN(MATCH_NOMATCH);
  1276. GETCHARINCTEST(c, eptr);
  1277. if (
  1278. #ifdef SUPPORT_UTF8
  1279. c < 256 &&
  1280. #endif
  1281. (md->ctypes[c] & ctype_space) != 0)
  1282. RRETURN(MATCH_NOMATCH);
  1283. ecode++;
  1284. break;
  1285. case OP_WHITESPACE:
  1286. if (eptr >= md->end_subject)
  1287. RRETURN(MATCH_NOMATCH);
  1288. GETCHARINCTEST(c, eptr);
  1289. if (
  1290. #ifdef SUPPORT_UTF8
  1291. c >= 256 ||
  1292. #endif
  1293. (md->ctypes[c] & ctype_space) == 0)
  1294. RRETURN(MATCH_NOMATCH);
  1295. ecode++;
  1296. break;
  1297. case OP_NOT_WORDCHAR:
  1298. if (eptr >= md->end_subject)
  1299. RRETURN(MATCH_NOMATCH);
  1300. GETCHARINCTEST(c, eptr);
  1301. if (
  1302. #ifdef SUPPORT_UTF8
  1303. c < 256 &&
  1304. #endif
  1305. (md->ctypes[c] & ctype_word) != 0)
  1306. RRETURN(MATCH_NOMATCH);
  1307. ecode++;
  1308. break;
  1309. case OP_WORDCHAR:
  1310. if (eptr >= md->end_subject)
  1311. RRETURN(MATCH_NOMATCH);
  1312. GETCHARINCTEST(c, eptr);
  1313. if (
  1314. #ifdef SUPPORT_UTF8
  1315. c >= 256 ||
  1316. #endif
  1317. (md->ctypes[c] & ctype_word) == 0)
  1318. RRETURN(MATCH_NOMATCH);
  1319. ecode++;
  1320. break;
  1321. case OP_ANYNL:
  1322. if (eptr >= md->end_subject)
  1323. RRETURN(MATCH_NOMATCH);
  1324. GETCHARINCTEST(c, eptr);
  1325. switch (c) {
  1326. default:
  1327. RRETURN(MATCH_NOMATCH);
  1328. case 0x000d:
  1329. if (eptr < md->end_subject && *eptr == 0x0a)
  1330. eptr++;
  1331. break;
  1332. case 0x000a:
  1333. break;
  1334. case 0x000b:
  1335. case 0x000c:
  1336. case 0x0085:
  1337. case 0x2028:
  1338. case 0x2029:
  1339. if (md->bsr_anycrlf)
  1340. RRETURN(MATCH_NOMATCH);
  1341. break;
  1342. }
  1343. ecode++;
  1344. break;
  1345. case OP_NOT_HSPACE:
  1346. if (eptr >= md->end_subject)
  1347. RRETURN(MATCH_NOMATCH);
  1348. GETCHARINCTEST(c, eptr);
  1349. switch (c) {
  1350. default:
  1351. break;
  1352. case 0x09: /* HT */
  1353. case 0x20: /* SPACE */
  1354. case 0xa0: /* NBSP */
  1355. case 0x1680: /* OGHAM SPACE MARK */
  1356. case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
  1357. case 0x2000: /* EN QUAD */
  1358. case 0x2001: /* EM QUAD */
  1359. case 0x2002: /* EN SPACE */
  1360. case 0x2003: /* EM SPACE */
  1361. case 0x2004: /* THREE-PER-EM SPACE */
  1362. case 0x2005: /* FOUR-PER-EM SPACE */
  1363. case 0x2006: /* SIX-PER-EM SPACE */
  1364. case 0x2007: /* FIGURE SPACE */
  1365. case 0x2008: /* PUNCTUATION SPACE */
  1366. case 0x2009: /* THIN SPACE */
  1367. case 0x200A: /* HAIR SPACE */
  1368. case 0x202f: /* NARROW NO-BREAK SPACE */
  1369. case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
  1370. case 0x3000: /* IDEOGRAPHIC SPACE */
  1371. RRETURN(MATCH_NOMATCH);
  1372. }
  1373. ecode++;
  1374. break;
  1375. case OP_HSPACE:
  1376. if (eptr >= md->end_subject)
  1377. RRETURN(MATCH_NOMATCH);
  1378. GETCHARINCTEST(c, eptr);
  1379. switch (c) {
  1380. default:
  1381. RRETURN(MATCH_NOMATCH);
  1382. case 0x09: /* HT */
  1383. case 0x20: /* SPACE */
  1384. case 0xa0: /* NBSP */
  1385. case 0x1680: /* OGHAM SPACE MARK */
  1386. case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
  1387. case 0x2000: /* EN QUAD */
  1388. case 0x2001: /* EM QUAD */
  1389. case 0x2002: /* EN SPACE */
  1390. case 0x2003: /* EM SPACE */
  1391. case 0x2004: /* THREE-PER-EM SPACE */
  1392. case 0x2005: /* FOUR-PER-EM SPACE */
  1393. case 0x2006: /* SIX-PER-EM SPACE */
  1394. case 0x2007: /* FIGURE SPACE */
  1395. case 0x2008: /* PUNCTUATION SPACE */
  1396. case 0x2009: /* THIN SPACE */
  1397. case 0x200A: /* HAIR SPACE */
  1398. case 0x202f: /* NARROW NO-BREAK SPACE */
  1399. case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
  1400. case 0x3000: /* IDEOGRAPHIC SPACE */
  1401. break;
  1402. }
  1403. ecode++;
  1404. break;
  1405. case OP_NOT_VSPACE:
  1406. if (eptr >= md->end_subject)
  1407. RRETURN(MATCH_NOMATCH);
  1408. GETCHARINCTEST(c, eptr);
  1409. switch (c) {
  1410. default:
  1411. break;
  1412. case 0x0a: /* LF */
  1413. case 0x0b: /* VT */
  1414. case 0x0c: /* FF */
  1415. case 0x0d: /* CR */
  1416. case 0x85: /* NEL */
  1417. case 0x2028: /* LINE SEPARATOR */
  1418. case 0x2029: /* PARAGRAPH SEPARATOR */
  1419. RRETURN(MATCH_NOMATCH);
  1420. }
  1421. ecode++;
  1422. break;
  1423. case OP_VSPACE:
  1424. if (eptr >= md->end_subject)
  1425. RRETURN(MATCH_NOMATCH);
  1426. GETCHARINCTEST(c, eptr);
  1427. switch (c) {
  1428. default:
  1429. RRETURN(MATCH_NOMATCH);
  1430. case 0x0a: /* LF */
  1431. case 0x0b: /* VT */
  1432. case 0x0c: /* FF */
  1433. case 0x0d: /* CR */
  1434. case 0x85: /* NEL */
  1435. case 0x2028: /* LINE SEPARATOR */
  1436. case 0x2029: /* PARAGRAPH SEPARATOR */
  1437. break;
  1438. }
  1439. ecode++;
  1440. break;
  1441. #ifdef SUPPORT_UCP
  1442. /* Check the next character by Unicode property. We will get
  1443. here only if the support is in the binary; otherwise a
  1444. compile-time error occurs. */
  1445. case OP_PROP:
  1446. case OP_NOTPROP:
  1447. if (eptr >= md->end_subject)
  1448. RRETURN(MATCH_NOMATCH);
  1449. GETCHARINCTEST(c, eptr);
  1450. {
  1451. int chartype, script;
  1452. int category = _pcre_ucp_findprop(c, &chartype, &script);
  1453. switch (ecode[1]) {
  1454. case PT_ANY:
  1455. if (op == OP_NOTPROP)
  1456. RRETURN(MATCH_NOMATCH);
  1457. break;
  1458. case PT_LAMP:
  1459. if ((chartype == ucp_Lu || chartype == ucp_Ll ||
  1460. chartype == ucp_Lt) == (op == OP_NOTPROP))
  1461. RRETURN(MATCH_NOMATCH);
  1462. break;
  1463. case PT_GC:
  1464. if ((ecode[2] != category) == (op == OP_PROP))
  1465. RRETURN(MATCH_NOMATCH);
  1466. break;
  1467. case PT_PC:
  1468. if ((ecode[2] != chartype) == (op == OP_PROP))
  1469. RRETURN(MATCH_NOMATCH);
  1470. break;
  1471. case PT_SC:
  1472. if ((ecode[2] != script) == (op == OP_PROP))
  1473. RRETURN(MATCH_NOMATCH);
  1474. break;
  1475. default:
  1476. RRETURN(PCRE_ERROR_INTERNAL);
  1477. }
  1478. ecode += 3;
  1479. }
  1480. break;
  1481. /* Match an extended Unicode sequence. We will get here only if
  1482. the support is in the binary; otherwise a compile-time error
  1483. occurs. */
  1484. case OP_EXTUNI:
  1485. if (eptr >= md->end_subject)
  1486. RRETURN(MATCH_NOMATCH);
  1487. GETCHARINCTEST(c, eptr);
  1488. {
  1489. int chartype, script;
  1490. int category = _pcre_ucp_findprop(c, &chartype, &script);
  1491. if (category == ucp_M)
  1492. RRETURN(MATCH_NOMATCH);
  1493. while (eptr < md->end_subject) {
  1494. int len = 1;
  1495. if (!utf8)
  1496. c = *eptr;
  1497. else {
  1498. GETCHARLEN(c, eptr, len);
  1499. }
  1500. category = _pcre_ucp_findprop(c, &chartype, &script);
  1501. if (category != ucp_M)
  1502. break;
  1503. eptr += len;
  1504. }
  1505. }
  1506. ecode++;
  1507. break;
  1508. #endif
  1509. /* Match a back reference, possibly repeatedly. Look past the
  1510. end of the item to see if there is repeat information following.
  1511. The code is similar to that for character classes, but repeated
  1512. for efficiency. Then obey similar code to character type repeats
  1513. - written out again for speed. However, if the referenced string
  1514. is the empty string, always treat it as matched, any number of
  1515. times (otherwise there could be infinite loops). */
  1516. case OP_REF: {
  1517. offset = GET2(ecode, 1) << 1; /* Doubled ref number */
  1518. ecode += 3; /* Advance past item */
  1519. /* If the reference is unset, set the length to be longer than
  1520. the amount of subject left; this ensures that every attempt at a
  1521. match fails. We can't just fail here, because of the possibility
  1522. of quantifiers with zero minima. */
  1523. length = (offset >= offset_top || md->offset_vector[offset] < 0)
  1524. ? md->end_subject - eptr + 1
  1525. : md->offset_vector[offset + 1] -
  1526. md->offset_vector[offset];
  1527. /* Set up for repetition, or handle the non-repeated case */
  1528. switch (*ecode) {
  1529. case OP_CRSTAR:
  1530. case OP_CRMINSTAR:
  1531. case OP_CRPLUS:
  1532. case OP_CRMINPLUS:
  1533. case OP_CRQUERY:
  1534. case OP_CRMINQUERY:
  1535. c = *ecode++ - OP_CRSTAR;
  1536. minimize = (c & 1) != 0;
  1537. min = rep_min[c]; /* Pick up values from tables; */
  1538. max = rep_max[c]; /* zero for max => infinity */
  1539. if (max == 0)
  1540. max = INT_MAX;
  1541. break;
  1542. case OP_CRRANGE:
  1543. case OP_CRMINRANGE:
  1544. minimize = (*ecode == OP_CRMINRANGE);
  1545. min = GET2(ecode, 1);
  1546. max = GET2(ecode, 3);
  1547. if (max == 0)
  1548. max = INT_MAX;
  1549. ecode += 5;
  1550. break;
  1551. default: /* No repeat follows */
  1552. if (!match_ref(offset, eptr, length, md, ims))
  1553. RRETURN(MATCH_NOMATCH);
  1554. eptr += length;
  1555. continue; /* With the main loop */
  1556. }
  1557. /* If the length of the reference is zero, just continue with
  1558. the main loop. */
  1559. if (length == 0)
  1560. continue;
  1561. /* First, ensure the minimum number of matches are present. We
  1562. get back the length of the reference string explicitly rather
  1563. than passing the address of eptr, so that eptr can be a register
  1564. variable. */
  1565. for (i = 1; i <= min; i++) {
  1566. if (!match_ref(offset, eptr, length, md, ims))
  1567. RRETURN(MATCH_NOMATCH);
  1568. eptr += length;
  1569. }
  1570. /* If min = max, continue at the same level without recursion.
  1571. They are not both allowed to be zero. */
  1572. if (min == max)
  1573. continue;
  1574. /* If minimizing, keep trying and advancing the pointer */
  1575. if (minimize) {
  1576. for (fi = min;; fi++) {
  1577. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  1578. RM14);
  1579. if (rrc != MATCH_NOMATCH)
  1580. RRETURN(rrc);
  1581. if (fi >= max ||
  1582. !match_ref(offset, eptr, length, md, ims))
  1583. RRETURN(MATCH_NOMATCH);
  1584. eptr += length;
  1585. }
  1586. /* Control never gets here */
  1587. }
  1588. /* If maximizing, find the longest string and work backwards */
  1589. else {
  1590. pp = eptr;
  1591. for (i = min; i < max; i++) {
  1592. if (!match_ref(offset, eptr, length, md, ims))
  1593. break;
  1594. eptr += length;
  1595. }
  1596. while (eptr >= pp) {
  1597. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  1598. RM15);
  1599. if (rrc != MATCH_NOMATCH)
  1600. RRETURN(rrc);
  1601. eptr -= length;
  1602. }
  1603. RRETURN(MATCH_NOMATCH);
  1604. }
  1605. }
  1606. /* Control never gets here */
  1607. /* Match a bit-mapped character class, possibly repeatedly. This
  1608. op code is used when all the characters in the class have values
  1609. in the range 0-255, and either the matching is caseful, or the
  1610. characters are in the range 0-127 when UTF-8 processing is
  1611. enabled. The only difference between OP_CLASS and OP_NCLASS
  1612. occurs when a data character outside the range is encountered.
  1613. First, look past the end of the item to see if there is repeat
  1614. information following. Then obey similar code to character type
  1615. repeats - written out again for speed. */
  1616. case OP_NCLASS:
  1617. case OP_CLASS: {
  1618. data = ecode + 1; /* Save for matching */
  1619. ecode += 33; /* Advance past the item */
  1620. switch (*ecode) {
  1621. case OP_CRSTAR:
  1622. case OP_CRMINSTAR:
  1623. case OP_CRPLUS:
  1624. case OP_CRMINPLUS:
  1625. case OP_CRQUERY:
  1626. case OP_CRMINQUERY:
  1627. c = *ecode++ - OP_CRSTAR;
  1628. minimize = (c & 1) != 0;
  1629. min = rep_min[c]; /* Pick up values from tables; */
  1630. max = rep_max[c]; /* zero for max => infinity */
  1631. if (max == 0)
  1632. max = INT_MAX;
  1633. break;
  1634. case OP_CRRANGE:
  1635. case OP_CRMINRANGE:
  1636. minimize = (*ecode == OP_CRMINRANGE);
  1637. min = GET2(ecode, 1);
  1638. max = GET2(ecode, 3);
  1639. if (max == 0)
  1640. max = INT_MAX;
  1641. ecode += 5;
  1642. break;
  1643. default: /* No repeat follows */
  1644. min = max = 1;
  1645. break;
  1646. }
  1647. /* First, ensure the minimum number of matches are present.
  1648. */
  1649. #ifdef SUPPORT_UTF8
  1650. /* UTF-8 mode */
  1651. if (utf8) {
  1652. for (i = 1; i <= min; i++) {
  1653. if (eptr >= md->end_subject)
  1654. RRETURN(MATCH_NOMATCH);
  1655. GETCHARINC(c, eptr);
  1656. if (c > 255) {
  1657. if (op == OP_CLASS)
  1658. RRETURN(MATCH_NOMATCH);
  1659. } else {
  1660. if ((data[c / 8] & (1 << (c & 7))) == 0)
  1661. RRETURN(MATCH_NOMATCH);
  1662. }
  1663. }
  1664. } else
  1665. #endif
  1666. /* Not UTF-8 mode */
  1667. {
  1668. for (i = 1; i <= min; i++) {
  1669. if (eptr >= md->end_subject)
  1670. RRETURN(MATCH_NOMATCH);
  1671. c = *eptr++;
  1672. if ((data[c / 8] & (1 << (c & 7))) == 0)
  1673. RRETURN(MATCH_NOMATCH);
  1674. }
  1675. }
  1676. /* If max == min we can continue with the main loop without the
  1677. need to recurse. */
  1678. if (min == max)
  1679. continue;
  1680. /* If minimizing, keep testing the rest of the expression and
  1681. advancing the pointer while it matches the class. */
  1682. if (minimize) {
  1683. #ifdef SUPPORT_UTF8
  1684. /* UTF-8 mode */
  1685. if (utf8) {
  1686. for (fi = min;; fi++) {
  1687. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  1688. RM16);
  1689. if (rrc != MATCH_NOMATCH)
  1690. RRETURN(rrc);
  1691. if (fi >= max || eptr >= md->end_subject)
  1692. RRETURN(MATCH_NOMATCH);
  1693. GETCHARINC(c, eptr);
  1694. if (c > 255) {
  1695. if (op == OP_CLASS)
  1696. RRETURN(MATCH_NOMATCH);
  1697. } else {
  1698. if ((data[c / 8] & (1 << (c & 7))) == 0)
  1699. RRETURN(MATCH_NOMATCH);
  1700. }
  1701. }
  1702. } else
  1703. #endif
  1704. /* Not UTF-8 mode */
  1705. {
  1706. for (fi = min;; fi++) {
  1707. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  1708. RM17);
  1709. if (rrc != MATCH_NOMATCH)
  1710. RRETURN(rrc);
  1711. if (fi >= max || eptr >= md->end_subject)
  1712. RRETURN(MATCH_NOMATCH);
  1713. c = *eptr++;
  1714. if ((data[c / 8] & (1 << (c & 7))) == 0)
  1715. RRETURN(MATCH_NOMATCH);
  1716. }
  1717. }
  1718. /* Control never gets here */
  1719. }
  1720. /* If maximizing, find the longest possible run, then work
  1721. backwards. */
  1722. else {
  1723. pp = eptr;
  1724. #ifdef SUPPORT_UTF8
  1725. /* UTF-8 mode */
  1726. if (utf8) {
  1727. for (i = min; i < max; i++) {
  1728. int len = 1;
  1729. if (eptr >= md->end_subject)
  1730. break;
  1731. GETCHARLEN(c, eptr, len);
  1732. if (c > 255) {
  1733. if (op == OP_CLASS)
  1734. break;
  1735. } else {
  1736. if ((data[c / 8] & (1 << (c & 7))) == 0)
  1737. break;
  1738. }
  1739. eptr += len;
  1740. }
  1741. for (;;) {
  1742. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  1743. RM18);
  1744. if (rrc != MATCH_NOMATCH)
  1745. RRETURN(rrc);
  1746. if (eptr-- == pp)
  1747. break; /* Stop if tried at original pos */
  1748. BACKCHAR(eptr);
  1749. }
  1750. } else
  1751. #endif
  1752. /* Not UTF-8 mode */
  1753. {
  1754. for (i = min; i < max; i++) {
  1755. if (eptr >= md->end_subject)
  1756. break;
  1757. c = *eptr;
  1758. if ((data[c / 8] & (1 << (c & 7))) == 0)
  1759. break;
  1760. eptr++;
  1761. }
  1762. while (eptr >= pp) {
  1763. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  1764. RM19);
  1765. if (rrc != MATCH_NOMATCH)
  1766. RRETURN(rrc);
  1767. eptr--;
  1768. }
  1769. }
  1770. RRETURN(MATCH_NOMATCH);
  1771. }
  1772. }
  1773. /* Control never gets here */
  1774. /* Match an extended character class. This opcode is encountered
  1775. only in UTF-8 mode, because that's the only time it is compiled. */
  1776. #ifdef SUPPORT_UTF8
  1777. case OP_XCLASS: {
  1778. data = ecode + 1 + LINK_SIZE; /* Save for matching */
  1779. ecode += GET(ecode, 1); /* Advance past the item */
  1780. switch (*ecode) {
  1781. case OP_CRSTAR:
  1782. case OP_CRMINSTAR:
  1783. case OP_CRPLUS:
  1784. case OP_CRMINPLUS:
  1785. case OP_CRQUERY:
  1786. case OP_CRMINQUERY:
  1787. c = *ecode++ - OP_CRSTAR;
  1788. minimize = (c & 1) != 0;
  1789. min = rep_min[c]; /* Pick up values from tables; */
  1790. max = rep_max[c]; /* zero for max => infinity */
  1791. if (max == 0)
  1792. max = INT_MAX;
  1793. break;
  1794. case OP_CRRANGE:
  1795. case OP_CRMINRANGE:
  1796. minimize = (*ecode == OP_CRMINRANGE);
  1797. min = GET2(ecode, 1);
  1798. max = GET2(ecode, 3);
  1799. if (max == 0)
  1800. max = INT_MAX;
  1801. ecode += 5;
  1802. break;
  1803. default: /* No repeat follows */
  1804. min = max = 1;
  1805. break;
  1806. }
  1807. /* First, ensure the minimum number of matches are present. */
  1808. for (i = 1; i <= min; i++) {
  1809. if (eptr >= md->end_subject)
  1810. RRETURN(MATCH_NOMATCH);
  1811. GETCHARINC(c, eptr);
  1812. if (!_pcre_xclass(c, data))
  1813. RRETURN(MATCH_NOMATCH);
  1814. }
  1815. /* If max == min we can continue with the main loop without the
  1816. need to recurse. */
  1817. if (min == max)
  1818. continue;
  1819. /* If minimizing, keep testing the rest of the expression and
  1820. advancing the pointer while it matches the class. */
  1821. if (minimize) {
  1822. for (fi = min;; fi++) {
  1823. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  1824. RM20);
  1825. if (rrc != MATCH_NOMATCH)
  1826. RRETURN(rrc);
  1827. if (fi >= max || eptr >= md->end_subject)
  1828. RRETURN(MATCH_NOMATCH);
  1829. GETCHARINC(c, eptr);
  1830. if (!_pcre_xclass(c, data))
  1831. RRETURN(MATCH_NOMATCH);
  1832. }
  1833. /* Control never gets here */
  1834. }
  1835. /* If maximizing, find the longest possible run, then work
  1836. backwards. */
  1837. else {
  1838. pp = eptr;
  1839. for (i = min; i < max; i++) {
  1840. int len = 1;
  1841. if (eptr >= md->end_subject)
  1842. break;
  1843. GETCHARLEN(c, eptr, len);
  1844. if (!_pcre_xclass(c, data))
  1845. break;
  1846. eptr += len;
  1847. }
  1848. for (;;) {
  1849. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  1850. RM21);
  1851. if (rrc != MATCH_NOMATCH)
  1852. RRETURN(rrc);
  1853. if (eptr-- == pp)
  1854. break; /* Stop if tried at original pos */
  1855. if (utf8)
  1856. BACKCHAR(eptr);
  1857. }
  1858. RRETURN(MATCH_NOMATCH);
  1859. }
  1860. /* Control never gets here */
  1861. }
  1862. #endif /* End of XCLASS */
  1863. /* Match a single character, casefully */
  1864. case OP_CHAR:
  1865. #ifdef SUPPORT_UTF8
  1866. if (utf8) {
  1867. length = 1;
  1868. ecode++;
  1869. GETCHARLEN(fc, ecode, length);
  1870. if (length > md->end_subject - eptr)
  1871. RRETURN(MATCH_NOMATCH);
  1872. while (length-- > 0)
  1873. if (*ecode++ != *eptr++)
  1874. RRETURN(MATCH_NOMATCH);
  1875. } else
  1876. #endif
  1877. /* Non-UTF-8 mode */
  1878. {
  1879. if (md->end_subject - eptr < 1)
  1880. RRETURN(MATCH_NOMATCH);
  1881. if (ecode[1] != *eptr++)
  1882. RRETURN(MATCH_NOMATCH);
  1883. ecode += 2;
  1884. }
  1885. break;
  1886. /* Match a single character, caselessly */
  1887. case OP_CHARNC:
  1888. #ifdef SUPPORT_UTF8
  1889. if (utf8) {
  1890. length = 1;
  1891. ecode++;
  1892. GETCHARLEN(fc, ecode, length);
  1893. if (length > md->end_subject - eptr)
  1894. RRETURN(MATCH_NOMATCH);
  1895. /* If the pattern character's value is < 128, we have only
  1896. one byte, and can use the fast lookup table. */
  1897. if (fc < 128) {
  1898. if (md->lcc[*ecode++] != md->lcc[*eptr++])
  1899. RRETURN(MATCH_NOMATCH);
  1900. }
  1901. /* Otherwise we must pick up the subject character */
  1902. else {
  1903. unsigned int dc;
  1904. GETCHARINC(dc, eptr);
  1905. ecode += length;
  1906. /* If we have Unicode property support, we can use it to
  1907. test the other case of the character, if there is one.
  1908. */
  1909. if (fc != dc) {
  1910. #ifdef SUPPORT_UCP
  1911. if (dc != _pcre_ucp_othercase(fc))
  1912. #endif
  1913. RRETURN(MATCH_NOMATCH);
  1914. }
  1915. }
  1916. } else
  1917. #endif /* SUPPORT_UTF8 */
  1918. /* Non-UTF-8 mode */
  1919. {
  1920. if (md->end_subject - eptr < 1)
  1921. RRETURN(MATCH_NOMATCH);
  1922. if (md->lcc[ecode[1]] != md->lcc[*eptr++])
  1923. RRETURN(MATCH_NOMATCH);
  1924. ecode += 2;
  1925. }
  1926. break;
  1927. /* Match a single character repeatedly. */
  1928. case OP_EXACT:
  1929. min = max = GET2(ecode, 1);
  1930. ecode += 3;
  1931. goto REPEATCHAR;
  1932. case OP_POSUPTO:
  1933. possessive = TRUE;
  1934. /* Fall through */
  1935. case OP_UPTO:
  1936. case OP_MINUPTO:
  1937. min = 0;
  1938. max = GET2(ecode, 1);
  1939. minimize = *ecode == OP_MINUPTO;
  1940. ecode += 3;
  1941. goto REPEATCHAR;
  1942. case OP_POSSTAR:
  1943. possessive = TRUE;
  1944. min = 0;
  1945. max = INT_MAX;
  1946. ecode++;
  1947. goto REPEATCHAR;
  1948. case OP_POSPLUS:
  1949. possessive = TRUE;
  1950. min = 1;
  1951. max = INT_MAX;
  1952. ecode++;
  1953. goto REPEATCHAR;
  1954. case OP_POSQUERY:
  1955. possessive = TRUE;
  1956. min = 0;
  1957. max = 1;
  1958. ecode++;
  1959. goto REPEATCHAR;
  1960. case OP_STAR:
  1961. case OP_MINSTAR:
  1962. case OP_PLUS:
  1963. case OP_MINPLUS:
  1964. case OP_QUERY:
  1965. case OP_MINQUERY:
  1966. c = *ecode++ - OP_STAR;
  1967. minimize = (c & 1) != 0;
  1968. min = rep_min[c]; /* Pick up values from tables; */
  1969. max = rep_max[c]; /* zero for max => infinity */
  1970. if (max == 0)
  1971. max = INT_MAX;
  1972. /* Common code for all repeated single-character matches. We can
  1973. give up quickly if there are fewer than the minimum number of
  1974. characters left in the subject. */
  1975. REPEATCHAR:
  1976. #ifdef SUPPORT_UTF8
  1977. if (utf8) {
  1978. length = 1;
  1979. charptr = ecode;
  1980. GETCHARLEN(fc, ecode, length);
  1981. if (min * length > md->end_subject - eptr)
  1982. RRETURN(MATCH_NOMATCH);
  1983. ecode += length;
  1984. /* Handle multibyte character matching specially here. There
  1985. is support for caseless matching if UCP support is present.
  1986. */
  1987. if (length > 1) {
  1988. #ifdef SUPPORT_UCP
  1989. unsigned int othercase;
  1990. if ((ims & PCRE_CASELESS) != 0 &&
  1991. (othercase = _pcre_ucp_othercase(fc)) != NOTACHAR)
  1992. oclength = _pcre_ord2utf8(othercase, occhars);
  1993. else
  1994. oclength = 0;
  1995. #endif /* SUPPORT_UCP */
  1996. for (i = 1; i <= min; i++) {
  1997. if (memcmp(eptr, charptr, length) == 0)
  1998. eptr += length;
  1999. #ifdef SUPPORT_UCP
  2000. /* Need braces because of following else */
  2001. else if (oclength == 0) {
  2002. RRETURN(MATCH_NOMATCH);
  2003. } else {
  2004. if (memcmp(eptr, occhars, oclength) != 0)
  2005. RRETURN(MATCH_NOMATCH);
  2006. eptr += oclength;
  2007. }
  2008. #else /* without SUPPORT_UCP */
  2009. else {
  2010. RRETURN(MATCH_NOMATCH);
  2011. }
  2012. #endif /* SUPPORT_UCP */
  2013. }
  2014. if (min == max)
  2015. continue;
  2016. if (minimize) {
  2017. for (fi = min;; fi++) {
  2018. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2019. 0, RM22);
  2020. if (rrc != MATCH_NOMATCH)
  2021. RRETURN(rrc);
  2022. if (fi >= max || eptr >= md->end_subject)
  2023. RRETURN(MATCH_NOMATCH);
  2024. if (memcmp(eptr, charptr, length) == 0)
  2025. eptr += length;
  2026. #ifdef SUPPORT_UCP
  2027. /* Need braces because of following else */
  2028. else if (oclength == 0) {
  2029. RRETURN(MATCH_NOMATCH);
  2030. } else {
  2031. if (memcmp(eptr, occhars, oclength) != 0)
  2032. RRETURN(MATCH_NOMATCH);
  2033. eptr += oclength;
  2034. }
  2035. #else /* without SUPPORT_UCP */
  2036. else {
  2037. RRETURN(MATCH_NOMATCH);
  2038. }
  2039. #endif /* SUPPORT_UCP */
  2040. }
  2041. /* Control never gets here */
  2042. }
  2043. else /* Maximize */
  2044. {
  2045. pp = eptr;
  2046. for (i = min; i < max; i++) {
  2047. if (eptr > md->end_subject - length)
  2048. break;
  2049. if (memcmp(eptr, charptr, length) == 0)
  2050. eptr += length;
  2051. #ifdef SUPPORT_UCP
  2052. else if (oclength == 0)
  2053. break;
  2054. else {
  2055. if (memcmp(eptr, occhars, oclength) != 0)
  2056. break;
  2057. eptr += oclength;
  2058. }
  2059. #else /* without SUPPORT_UCP */
  2060. else
  2061. break;
  2062. #endif /* SUPPORT_UCP */
  2063. }
  2064. if (possessive)
  2065. continue;
  2066. for (;;) {
  2067. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2068. 0, RM23);
  2069. if (rrc != MATCH_NOMATCH)
  2070. RRETURN(rrc);
  2071. if (eptr == pp)
  2072. RRETURN(MATCH_NOMATCH);
  2073. #ifdef SUPPORT_UCP
  2074. eptr--;
  2075. BACKCHAR(eptr);
  2076. #else /* without SUPPORT_UCP */
  2077. eptr -= length;
  2078. #endif /* SUPPORT_UCP */
  2079. }
  2080. }
  2081. /* Control never gets here */
  2082. }
  2083. /* If the length of a UTF-8 character is 1, we fall through
  2084. here, and obey the code as for non-UTF-8 characters below,
  2085. though in this case the value of fc will always be < 128. */
  2086. } else
  2087. #endif /* SUPPORT_UTF8 */
  2088. /* When not in UTF-8 mode, load a single-byte character. */
  2089. {
  2090. if (min > md->end_subject - eptr)
  2091. RRETURN(MATCH_NOMATCH);
  2092. fc = *ecode++;
  2093. }
  2094. /* The value of fc at this point is always less than 256, though
  2095. we may or may not be in UTF-8 mode. The code is duplicated for
  2096. the caseless and caseful cases, for speed, since matching
  2097. characters is likely to be quite common. First, ensure the
  2098. minimum number of matches are present. If min = max, continue at
  2099. the same level without recursing. Otherwise, if minimizing, keep
  2100. trying the rest of the expression and advancing one matching
  2101. character if failing, up to the maximum. Alternatively, if
  2102. maximizing, find the maximum number of characters and work
  2103. backwards. */
  2104. DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min,
  2105. max, max, eptr));
  2106. if ((ims & PCRE_CASELESS) != 0) {
  2107. fc = md->lcc[fc];
  2108. for (i = 1; i <= min; i++)
  2109. if (fc != md->lcc[*eptr++])
  2110. RRETURN(MATCH_NOMATCH);
  2111. if (min == max)
  2112. continue;
  2113. if (minimize) {
  2114. for (fi = min;; fi++) {
  2115. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  2116. RM24);
  2117. if (rrc != MATCH_NOMATCH)
  2118. RRETURN(rrc);
  2119. if (fi >= max || eptr >= md->end_subject ||
  2120. fc != md->lcc[*eptr++])
  2121. RRETURN(MATCH_NOMATCH);
  2122. }
  2123. /* Control never gets here */
  2124. } else /* Maximize */
  2125. {
  2126. pp = eptr;
  2127. for (i = min; i < max; i++) {
  2128. if (eptr >= md->end_subject || fc != md->lcc[*eptr])
  2129. break;
  2130. eptr++;
  2131. }
  2132. if (possessive)
  2133. continue;
  2134. while (eptr >= pp) {
  2135. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  2136. RM25);
  2137. eptr--;
  2138. if (rrc != MATCH_NOMATCH)
  2139. RRETURN(rrc);
  2140. }
  2141. RRETURN(MATCH_NOMATCH);
  2142. }
  2143. /* Control never gets here */
  2144. }
  2145. /* Caseful comparisons (includes all multi-byte characters) */
  2146. else {
  2147. for (i = 1; i <= min; i++)
  2148. if (fc != *eptr++)
  2149. RRETURN(MATCH_NOMATCH);
  2150. if (min == max)
  2151. continue;
  2152. if (minimize) {
  2153. for (fi = min;; fi++) {
  2154. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  2155. RM26);
  2156. if (rrc != MATCH_NOMATCH)
  2157. RRETURN(rrc);
  2158. if (fi >= max || eptr >= md->end_subject ||
  2159. fc != *eptr++)
  2160. RRETURN(MATCH_NOMATCH);
  2161. }
  2162. /* Control never gets here */
  2163. } else /* Maximize */
  2164. {
  2165. pp = eptr;
  2166. for (i = min; i < max; i++) {
  2167. if (eptr >= md->end_subject || fc != *eptr)
  2168. break;
  2169. eptr++;
  2170. }
  2171. if (possessive)
  2172. continue;
  2173. while (eptr >= pp) {
  2174. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  2175. RM27);
  2176. eptr--;
  2177. if (rrc != MATCH_NOMATCH)
  2178. RRETURN(rrc);
  2179. }
  2180. RRETURN(MATCH_NOMATCH);
  2181. }
  2182. }
  2183. /* Control never gets here */
  2184. /* Match a negated single one-byte character. The character we
  2185. are checking can be multibyte. */
  2186. case OP_NOT:
  2187. if (eptr >= md->end_subject)
  2188. RRETURN(MATCH_NOMATCH);
  2189. ecode++;
  2190. GETCHARINCTEST(c, eptr);
  2191. if ((ims & PCRE_CASELESS) != 0) {
  2192. #ifdef SUPPORT_UTF8
  2193. if (c < 256)
  2194. #endif
  2195. c = md->lcc[c];
  2196. if (md->lcc[*ecode++] == c)
  2197. RRETURN(MATCH_NOMATCH);
  2198. } else {
  2199. if (*ecode++ == c)
  2200. RRETURN(MATCH_NOMATCH);
  2201. }
  2202. break;
  2203. /* Match a negated single one-byte character repeatedly. This is
  2204. almost a repeat of the code for a repeated single character, but
  2205. I haven't found a nice way of commoning these up that doesn't
  2206. require a test of the positive/negative option for each
  2207. character match. Maybe that wouldn't add very much to the time
  2208. taken, but character matching *is* what this is all about... */
  2209. case OP_NOTEXACT:
  2210. min = max = GET2(ecode, 1);
  2211. ecode += 3;
  2212. goto REPEATNOTCHAR;
  2213. case OP_NOTUPTO:
  2214. case OP_NOTMINUPTO:
  2215. min = 0;
  2216. max = GET2(ecode, 1);
  2217. minimize = *ecode == OP_NOTMINUPTO;
  2218. ecode += 3;
  2219. goto REPEATNOTCHAR;
  2220. case OP_NOTPOSSTAR:
  2221. possessive = TRUE;
  2222. min = 0;
  2223. max = INT_MAX;
  2224. ecode++;
  2225. goto REPEATNOTCHAR;
  2226. case OP_NOTPOSPLUS:
  2227. possessive = TRUE;
  2228. min = 1;
  2229. max = INT_MAX;
  2230. ecode++;
  2231. goto REPEATNOTCHAR;
  2232. case OP_NOTPOSQUERY:
  2233. possessive = TRUE;
  2234. min = 0;
  2235. max = 1;
  2236. ecode++;
  2237. goto REPEATNOTCHAR;
  2238. case OP_NOTPOSUPTO:
  2239. possessive = TRUE;
  2240. min = 0;
  2241. max = GET2(ecode, 1);
  2242. ecode += 3;
  2243. goto REPEATNOTCHAR;
  2244. case OP_NOTSTAR:
  2245. case OP_NOTMINSTAR:
  2246. case OP_NOTPLUS:
  2247. case OP_NOTMINPLUS:
  2248. case OP_NOTQUERY:
  2249. case OP_NOTMINQUERY:
  2250. c = *ecode++ - OP_NOTSTAR;
  2251. minimize = (c & 1) != 0;
  2252. min = rep_min[c]; /* Pick up values from tables; */
  2253. max = rep_max[c]; /* zero for max => infinity */
  2254. if (max == 0)
  2255. max = INT_MAX;
  2256. /* Common code for all repeated single-byte matches. We can give
  2257. up quickly if there are fewer than the minimum number of bytes
  2258. left in the subject. */
  2259. REPEATNOTCHAR:
  2260. if (min > md->end_subject - eptr)
  2261. RRETURN(MATCH_NOMATCH);
  2262. fc = *ecode++;
  2263. /* The code is duplicated for the caseless and caseful cases,
  2264. for speed, since matching characters is likely to be quite
  2265. common. First, ensure the minimum number of matches are present.
  2266. If min = max, continue at the same level without recursing.
  2267. Otherwise, if minimizing, keep trying the rest of the expression
  2268. and advancing one matching character if failing, up to the
  2269. maximum. Alternatively, if maximizing, find the maximum number
  2270. of characters and work backwards. */
  2271. DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n",
  2272. fc, min, max, max, eptr));
  2273. if ((ims & PCRE_CASELESS) != 0) {
  2274. fc = md->lcc[fc];
  2275. #ifdef SUPPORT_UTF8
  2276. /* UTF-8 mode */
  2277. if (utf8) {
  2278. register unsigned int d;
  2279. for (i = 1; i <= min; i++) {
  2280. GETCHARINC(d, eptr);
  2281. if (d < 256)
  2282. d = md->lcc[d];
  2283. if (fc == d)
  2284. RRETURN(MATCH_NOMATCH);
  2285. }
  2286. } else
  2287. #endif
  2288. /* Not UTF-8 mode */
  2289. {
  2290. for (i = 1; i <= min; i++)
  2291. if (fc == md->lcc[*eptr++])
  2292. RRETURN(MATCH_NOMATCH);
  2293. }
  2294. if (min == max)
  2295. continue;
  2296. if (minimize) {
  2297. #ifdef SUPPORT_UTF8
  2298. /* UTF-8 mode */
  2299. if (utf8) {
  2300. register unsigned int d;
  2301. for (fi = min;; fi++) {
  2302. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2303. 0, RM28);
  2304. if (rrc != MATCH_NOMATCH)
  2305. RRETURN(rrc);
  2306. GETCHARINC(d, eptr);
  2307. if (d < 256)
  2308. d = md->lcc[d];
  2309. if (fi >= max || eptr >= md->end_subject ||
  2310. fc == d)
  2311. RRETURN(MATCH_NOMATCH);
  2312. }
  2313. } else
  2314. #endif
  2315. /* Not UTF-8 mode */
  2316. {
  2317. for (fi = min;; fi++) {
  2318. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2319. 0, RM29);
  2320. if (rrc != MATCH_NOMATCH)
  2321. RRETURN(rrc);
  2322. if (fi >= max || eptr >= md->end_subject ||
  2323. fc == md->lcc[*eptr++])
  2324. RRETURN(MATCH_NOMATCH);
  2325. }
  2326. }
  2327. /* Control never gets here */
  2328. }
  2329. /* Maximize case */
  2330. else {
  2331. pp = eptr;
  2332. #ifdef SUPPORT_UTF8
  2333. /* UTF-8 mode */
  2334. if (utf8) {
  2335. register unsigned int d;
  2336. for (i = min; i < max; i++) {
  2337. int len = 1;
  2338. if (eptr >= md->end_subject)
  2339. break;
  2340. GETCHARLEN(d, eptr, len);
  2341. if (d < 256)
  2342. d = md->lcc[d];
  2343. if (fc == d)
  2344. break;
  2345. eptr += len;
  2346. }
  2347. if (possessive)
  2348. continue;
  2349. for (;;) {
  2350. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2351. 0, RM30);
  2352. if (rrc != MATCH_NOMATCH)
  2353. RRETURN(rrc);
  2354. if (eptr-- == pp)
  2355. break; /* Stop if tried at original pos */
  2356. BACKCHAR(eptr);
  2357. }
  2358. } else
  2359. #endif
  2360. /* Not UTF-8 mode */
  2361. {
  2362. for (i = min; i < max; i++) {
  2363. if (eptr >= md->end_subject ||
  2364. fc == md->lcc[*eptr])
  2365. break;
  2366. eptr++;
  2367. }
  2368. if (possessive)
  2369. continue;
  2370. while (eptr >= pp) {
  2371. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2372. 0, RM31);
  2373. if (rrc != MATCH_NOMATCH)
  2374. RRETURN(rrc);
  2375. eptr--;
  2376. }
  2377. }
  2378. RRETURN(MATCH_NOMATCH);
  2379. }
  2380. /* Control never gets here */
  2381. }
  2382. /* Caseful comparisons */
  2383. else {
  2384. #ifdef SUPPORT_UTF8
  2385. /* UTF-8 mode */
  2386. if (utf8) {
  2387. register unsigned int d;
  2388. for (i = 1; i <= min; i++) {
  2389. GETCHARINC(d, eptr);
  2390. if (fc == d)
  2391. RRETURN(MATCH_NOMATCH);
  2392. }
  2393. } else
  2394. #endif
  2395. /* Not UTF-8 mode */
  2396. {
  2397. for (i = 1; i <= min; i++)
  2398. if (fc == *eptr++)
  2399. RRETURN(MATCH_NOMATCH);
  2400. }
  2401. if (min == max)
  2402. continue;
  2403. if (minimize) {
  2404. #ifdef SUPPORT_UTF8
  2405. /* UTF-8 mode */
  2406. if (utf8) {
  2407. register unsigned int d;
  2408. for (fi = min;; fi++) {
  2409. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2410. 0, RM32);
  2411. if (rrc != MATCH_NOMATCH)
  2412. RRETURN(rrc);
  2413. GETCHARINC(d, eptr);
  2414. if (fi >= max || eptr >= md->end_subject ||
  2415. fc == d)
  2416. RRETURN(MATCH_NOMATCH);
  2417. }
  2418. } else
  2419. #endif
  2420. /* Not UTF-8 mode */
  2421. {
  2422. for (fi = min;; fi++) {
  2423. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2424. 0, RM33);
  2425. if (rrc != MATCH_NOMATCH)
  2426. RRETURN(rrc);
  2427. if (fi >= max || eptr >= md->end_subject ||
  2428. fc == *eptr++)
  2429. RRETURN(MATCH_NOMATCH);
  2430. }
  2431. }
  2432. /* Control never gets here */
  2433. }
  2434. /* Maximize case */
  2435. else {
  2436. pp = eptr;
  2437. #ifdef SUPPORT_UTF8
  2438. /* UTF-8 mode */
  2439. if (utf8) {
  2440. register unsigned int d;
  2441. for (i = min; i < max; i++) {
  2442. int len = 1;
  2443. if (eptr >= md->end_subject)
  2444. break;
  2445. GETCHARLEN(d, eptr, len);
  2446. if (fc == d)
  2447. break;
  2448. eptr += len;
  2449. }
  2450. if (possessive)
  2451. continue;
  2452. for (;;) {
  2453. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2454. 0, RM34);
  2455. if (rrc != MATCH_NOMATCH)
  2456. RRETURN(rrc);
  2457. if (eptr-- == pp)
  2458. break; /* Stop if tried at original pos */
  2459. BACKCHAR(eptr);
  2460. }
  2461. } else
  2462. #endif
  2463. /* Not UTF-8 mode */
  2464. {
  2465. for (i = min; i < max; i++) {
  2466. if (eptr >= md->end_subject || fc == *eptr)
  2467. break;
  2468. eptr++;
  2469. }
  2470. if (possessive)
  2471. continue;
  2472. while (eptr >= pp) {
  2473. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  2474. 0, RM35);
  2475. if (rrc != MATCH_NOMATCH)
  2476. RRETURN(rrc);
  2477. eptr--;
  2478. }
  2479. }
  2480. RRETURN(MATCH_NOMATCH);
  2481. }
  2482. }
  2483. /* Control never gets here */
  2484. /* Match a single character type repeatedly; several different
  2485. opcodes share code. This is very similar to the code for single
  2486. characters, but we repeat it in the interests of efficiency. */
  2487. case OP_TYPEEXACT:
  2488. min = max = GET2(ecode, 1);
  2489. minimize = TRUE;
  2490. ecode += 3;
  2491. goto REPEATTYPE;
  2492. case OP_TYPEUPTO:
  2493. case OP_TYPEMINUPTO:
  2494. min = 0;
  2495. max = GET2(ecode, 1);
  2496. minimize = *ecode == OP_TYPEMINUPTO;
  2497. ecode += 3;
  2498. goto REPEATTYPE;
  2499. case OP_TYPEPOSSTAR:
  2500. possessive = TRUE;
  2501. min = 0;
  2502. max = INT_MAX;
  2503. ecode++;
  2504. goto REPEATTYPE;
  2505. case OP_TYPEPOSPLUS:
  2506. possessive = TRUE;
  2507. min = 1;
  2508. max = INT_MAX;
  2509. ecode++;
  2510. goto REPEATTYPE;
  2511. case OP_TYPEPOSQUERY:
  2512. possessive = TRUE;
  2513. min = 0;
  2514. max = 1;
  2515. ecode++;
  2516. goto REPEATTYPE;
  2517. case OP_TYPEPOSUPTO:
  2518. possessive = TRUE;
  2519. min = 0;
  2520. max = GET2(ecode, 1);
  2521. ecode += 3;
  2522. goto REPEATTYPE;
  2523. case OP_TYPESTAR:
  2524. case OP_TYPEMINSTAR:
  2525. case OP_TYPEPLUS:
  2526. case OP_TYPEMINPLUS:
  2527. case OP_TYPEQUERY:
  2528. case OP_TYPEMINQUERY:
  2529. c = *ecode++ - OP_TYPESTAR;
  2530. minimize = (c & 1) != 0;
  2531. min = rep_min[c]; /* Pick up values from tables; */
  2532. max = rep_max[c]; /* zero for max => infinity */
  2533. if (max == 0)
  2534. max = INT_MAX;
  2535. /* Common code for all repeated single character type matches.
  2536. Note that in UTF-8 mode, '.' matches a character of any length,
  2537. but for the other character types, the valid characters are all
  2538. one-byte long. */
  2539. REPEATTYPE:
  2540. ctype = *ecode++; /* Code for the character type */
  2541. #ifdef SUPPORT_UCP
  2542. if (ctype == OP_PROP || ctype == OP_NOTPROP) {
  2543. prop_fail_result = ctype == OP_NOTPROP;
  2544. prop_type = *ecode++;
  2545. prop_value = *ecode++;
  2546. } else
  2547. prop_type = -1;
  2548. #endif
  2549. /* First, ensure the minimum number of matches are present. Use
  2550. inline code for maximizing the speed, and do the type test once
  2551. at the start (i.e. keep it out of the loop). Also we can test
  2552. that there are at least the minimum number of bytes before we
  2553. start. This isn't as effective in UTF-8 mode, but it does no
  2554. harm. Separate the UTF-8 code completely as that is tidier. Also
  2555. separate the UCP code, which can be the same for both UTF-8 and
  2556. single-bytes. */
  2557. if (min > md->end_subject - eptr)
  2558. RRETURN(MATCH_NOMATCH);
  2559. if (min > 0) {
  2560. #ifdef SUPPORT_UCP
  2561. if (prop_type >= 0) {
  2562. switch (prop_type) {
  2563. case PT_ANY:
  2564. if (prop_fail_result)
  2565. RRETURN(MATCH_NOMATCH);
  2566. for (i = 1; i <= min; i++) {
  2567. if (eptr >= md->end_subject)
  2568. RRETURN(MATCH_NOMATCH);
  2569. GETCHARINCTEST(c, eptr);
  2570. }
  2571. break;
  2572. case PT_LAMP:
  2573. for (i = 1; i <= min; i++) {
  2574. if (eptr >= md->end_subject)
  2575. RRETURN(MATCH_NOMATCH);
  2576. GETCHARINCTEST(c, eptr);
  2577. prop_category = _pcre_ucp_findprop(
  2578. c, &prop_chartype, &prop_script);
  2579. if ((prop_chartype == ucp_Lu ||
  2580. prop_chartype == ucp_Ll ||
  2581. prop_chartype == ucp_Lt) ==
  2582. prop_fail_result)
  2583. RRETURN(MATCH_NOMATCH);
  2584. }
  2585. break;
  2586. case PT_GC:
  2587. for (i = 1; i <= min; i++) {
  2588. if (eptr >= md->end_subject)
  2589. RRETURN(MATCH_NOMATCH);
  2590. GETCHARINCTEST(c, eptr);
  2591. prop_category = _pcre_ucp_findprop(
  2592. c, &prop_chartype, &prop_script);
  2593. if ((prop_category == prop_value) ==
  2594. prop_fail_result)
  2595. RRETURN(MATCH_NOMATCH);
  2596. }
  2597. break;
  2598. case PT_PC:
  2599. for (i = 1; i <= min; i++) {
  2600. if (eptr >= md->end_subject)
  2601. RRETURN(MATCH_NOMATCH);
  2602. GETCHARINCTEST(c, eptr);
  2603. prop_category = _pcre_ucp_findprop(
  2604. c, &prop_chartype, &prop_script);
  2605. if ((prop_chartype == prop_value) ==
  2606. prop_fail_result)
  2607. RRETURN(MATCH_NOMATCH);
  2608. }
  2609. break;
  2610. case PT_SC:
  2611. for (i = 1; i <= min; i++) {
  2612. if (eptr >= md->end_subject)
  2613. RRETURN(MATCH_NOMATCH);
  2614. GETCHARINCTEST(c, eptr);
  2615. prop_category = _pcre_ucp_findprop(
  2616. c, &prop_chartype, &prop_script);
  2617. if ((prop_script == prop_value) ==
  2618. prop_fail_result)
  2619. RRETURN(MATCH_NOMATCH);
  2620. }
  2621. break;
  2622. default:
  2623. RRETURN(PCRE_ERROR_INTERNAL);
  2624. }
  2625. }
  2626. /* Match extended Unicode sequences. We will get here only
  2627. if the support is in the binary; otherwise a compile-time
  2628. error occurs. */
  2629. else if (ctype == OP_EXTUNI) {
  2630. for (i = 1; i <= min; i++) {
  2631. GETCHARINCTEST(c, eptr);
  2632. prop_category = _pcre_ucp_findprop(
  2633. c, &prop_chartype, &prop_script);
  2634. if (prop_category == ucp_M)
  2635. RRETURN(MATCH_NOMATCH);
  2636. while (eptr < md->end_subject) {
  2637. int len = 1;
  2638. if (!utf8)
  2639. c = *eptr;
  2640. else {
  2641. GETCHARLEN(c, eptr, len);
  2642. }
  2643. prop_category = _pcre_ucp_findprop(
  2644. c, &prop_chartype, &prop_script);
  2645. if (prop_category != ucp_M)
  2646. break;
  2647. eptr += len;
  2648. }
  2649. }
  2650. }
  2651. else
  2652. #endif /* SUPPORT_UCP */
  2653. /* Handle all other cases when the coding is UTF-8 */
  2654. #ifdef SUPPORT_UTF8
  2655. if (utf8)
  2656. switch (ctype) {
  2657. case OP_ANY:
  2658. for (i = 1; i <= min; i++) {
  2659. if (eptr >= md->end_subject ||
  2660. ((ims & PCRE_DOTALL) == 0 &&
  2661. IS_NEWLINE(eptr)))
  2662. RRETURN(MATCH_NOMATCH);
  2663. eptr++;
  2664. while (eptr < md->end_subject &&
  2665. (*eptr & 0xc0) == 0x80)
  2666. eptr++;
  2667. }
  2668. break;
  2669. case OP_ANYBYTE:
  2670. eptr += min;
  2671. break;
  2672. case OP_ANYNL:
  2673. for (i = 1; i <= min; i++) {
  2674. if (eptr >= md->end_subject)
  2675. RRETURN(MATCH_NOMATCH);
  2676. GETCHARINC(c, eptr);
  2677. switch (c) {
  2678. default:
  2679. RRETURN(MATCH_NOMATCH);
  2680. case 0x000d:
  2681. if (eptr < md->end_subject &&
  2682. *eptr == 0x0a)
  2683. eptr++;
  2684. break;
  2685. case 0x000a:
  2686. break;
  2687. case 0x000b:
  2688. case 0x000c:
  2689. case 0x0085:
  2690. case 0x2028:
  2691. case 0x2029:
  2692. if (md->bsr_anycrlf)
  2693. RRETURN(MATCH_NOMATCH);
  2694. break;
  2695. }
  2696. }
  2697. break;
  2698. case OP_NOT_HSPACE:
  2699. for (i = 1; i <= min; i++) {
  2700. if (eptr >= md->end_subject)
  2701. RRETURN(MATCH_NOMATCH);
  2702. GETCHARINC(c, eptr);
  2703. switch (c) {
  2704. default:
  2705. break;
  2706. case 0x09: /* HT */
  2707. case 0x20: /* SPACE */
  2708. case 0xa0: /* NBSP */
  2709. case 0x1680: /* OGHAM SPACE MARK */
  2710. case 0x180e: /* MONGOLIAN VOWEL
  2711. SEPARATOR */
  2712. case 0x2000: /* EN QUAD */
  2713. case 0x2001: /* EM QUAD */
  2714. case 0x2002: /* EN SPACE */
  2715. case 0x2003: /* EM SPACE */
  2716. case 0x2004: /* THREE-PER-EM SPACE */
  2717. case 0x2005: /* FOUR-PER-EM SPACE */
  2718. case 0x2006: /* SIX-PER-EM SPACE */
  2719. case 0x2007: /* FIGURE SPACE */
  2720. case 0x2008: /* PUNCTUATION SPACE */
  2721. case 0x2009: /* THIN SPACE */
  2722. case 0x200A: /* HAIR SPACE */
  2723. case 0x202f: /* NARROW NO-BREAK SPACE */
  2724. case 0x205f: /* MEDIUM MATHEMATICAL
  2725. SPACE */
  2726. case 0x3000: /* IDEOGRAPHIC SPACE */
  2727. RRETURN(MATCH_NOMATCH);
  2728. }
  2729. }
  2730. break;
  2731. case OP_HSPACE:
  2732. for (i = 1; i <= min; i++) {
  2733. if (eptr >= md->end_subject)
  2734. RRETURN(MATCH_NOMATCH);
  2735. GETCHARINC(c, eptr);
  2736. switch (c) {
  2737. default:
  2738. RRETURN(MATCH_NOMATCH);
  2739. case 0x09: /* HT */
  2740. case 0x20: /* SPACE */
  2741. case 0xa0: /* NBSP */
  2742. case 0x1680: /* OGHAM SPACE MARK */
  2743. case 0x180e: /* MONGOLIAN VOWEL
  2744. SEPARATOR */
  2745. case 0x2000: /* EN QUAD */
  2746. case 0x2001: /* EM QUAD */
  2747. case 0x2002: /* EN SPACE */
  2748. case 0x2003: /* EM SPACE */
  2749. case 0x2004: /* THREE-PER-EM SPACE */
  2750. case 0x2005: /* FOUR-PER-EM SPACE */
  2751. case 0x2006: /* SIX-PER-EM SPACE */
  2752. case 0x2007: /* FIGURE SPACE */
  2753. case 0x2008: /* PUNCTUATION SPACE */
  2754. case 0x2009: /* THIN SPACE */
  2755. case 0x200A: /* HAIR SPACE */
  2756. case 0x202f: /* NARROW NO-BREAK SPACE */
  2757. case 0x205f: /* MEDIUM MATHEMATICAL
  2758. SPACE */
  2759. case 0x3000: /* IDEOGRAPHIC SPACE */
  2760. break;
  2761. }
  2762. }
  2763. break;
  2764. case OP_NOT_VSPACE:
  2765. for (i = 1; i <= min; i++) {
  2766. if (eptr >= md->end_subject)
  2767. RRETURN(MATCH_NOMATCH);
  2768. GETCHARINC(c, eptr);
  2769. switch (c) {
  2770. default:
  2771. break;
  2772. case 0x0a: /* LF */
  2773. case 0x0b: /* VT */
  2774. case 0x0c: /* FF */
  2775. case 0x0d: /* CR */
  2776. case 0x85: /* NEL */
  2777. case 0x2028: /* LINE SEPARATOR */
  2778. case 0x2029: /* PARAGRAPH SEPARATOR */
  2779. RRETURN(MATCH_NOMATCH);
  2780. }
  2781. }
  2782. break;
  2783. case OP_VSPACE:
  2784. for (i = 1; i <= min; i++) {
  2785. if (eptr >= md->end_subject)
  2786. RRETURN(MATCH_NOMATCH);
  2787. GETCHARINC(c, eptr);
  2788. switch (c) {
  2789. default:
  2790. RRETURN(MATCH_NOMATCH);
  2791. case 0x0a: /* LF */
  2792. case 0x0b: /* VT */
  2793. case 0x0c: /* FF */
  2794. case 0x0d: /* CR */
  2795. case 0x85: /* NEL */
  2796. case 0x2028: /* LINE SEPARATOR */
  2797. case 0x2029: /* PARAGRAPH SEPARATOR */
  2798. break;
  2799. }
  2800. }
  2801. break;
  2802. case OP_NOT_DIGIT:
  2803. for (i = 1; i <= min; i++) {
  2804. if (eptr >= md->end_subject)
  2805. RRETURN(MATCH_NOMATCH);
  2806. GETCHARINC(c, eptr);
  2807. if (c < 128 &&
  2808. (md->ctypes[c] & ctype_digit) != 0)
  2809. RRETURN(MATCH_NOMATCH);
  2810. }
  2811. break;
  2812. case OP_DIGIT:
  2813. for (i = 1; i <= min; i++) {
  2814. if (eptr >= md->end_subject ||
  2815. *eptr >= 128 ||
  2816. (md->ctypes[*eptr++] & ctype_digit) ==
  2817. 0)
  2818. RRETURN(MATCH_NOMATCH);
  2819. /* No need to skip more bytes - we know it's
  2820. * a 1-byte character */
  2821. }
  2822. break;
  2823. case OP_NOT_WHITESPACE:
  2824. for (i = 1; i <= min; i++) {
  2825. if (eptr >= md->end_subject ||
  2826. (*eptr < 128 && (md->ctypes[*eptr] &
  2827. ctype_space) != 0))
  2828. RRETURN(MATCH_NOMATCH);
  2829. while (++eptr < md->end_subject &&
  2830. (*eptr & 0xc0) == 0x80)
  2831. ;
  2832. }
  2833. break;
  2834. case OP_WHITESPACE:
  2835. for (i = 1; i <= min; i++) {
  2836. if (eptr >= md->end_subject ||
  2837. *eptr >= 128 ||
  2838. (md->ctypes[*eptr++] & ctype_space) ==
  2839. 0)
  2840. RRETURN(MATCH_NOMATCH);
  2841. /* No need to skip more bytes - we know it's
  2842. * a 1-byte character */
  2843. }
  2844. break;
  2845. case OP_NOT_WORDCHAR:
  2846. for (i = 1; i <= min; i++) {
  2847. if (eptr >= md->end_subject ||
  2848. (*eptr < 128 &&
  2849. (md->ctypes[*eptr] & ctype_word) != 0))
  2850. RRETURN(MATCH_NOMATCH);
  2851. while (++eptr < md->end_subject &&
  2852. (*eptr & 0xc0) == 0x80)
  2853. ;
  2854. }
  2855. break;
  2856. case OP_WORDCHAR:
  2857. for (i = 1; i <= min; i++) {
  2858. if (eptr >= md->end_subject ||
  2859. *eptr >= 128 ||
  2860. (md->ctypes[*eptr++] & ctype_word) == 0)
  2861. RRETURN(MATCH_NOMATCH);
  2862. /* No need to skip more bytes - we know it's
  2863. * a 1-byte character */
  2864. }
  2865. break;
  2866. default:
  2867. RRETURN(PCRE_ERROR_INTERNAL);
  2868. } /* End switch(ctype) */
  2869. else
  2870. #endif /* SUPPORT_UTF8 */
  2871. /* Code for the non-UTF-8 case for minimum matching of
  2872. operators other than OP_PROP and OP_NOTPROP. We can
  2873. assume that there are the minimum number of bytes
  2874. present, as this was tested above. */
  2875. switch (ctype) {
  2876. case OP_ANY:
  2877. if ((ims & PCRE_DOTALL) == 0) {
  2878. for (i = 1; i <= min; i++) {
  2879. if (IS_NEWLINE(eptr))
  2880. RRETURN(MATCH_NOMATCH);
  2881. eptr++;
  2882. }
  2883. } else
  2884. eptr += min;
  2885. break;
  2886. case OP_ANYBYTE:
  2887. eptr += min;
  2888. break;
  2889. /* Because of the CRLF case, we can't assume the
  2890. minimum number of bytes are present in this
  2891. case. */
  2892. case OP_ANYNL:
  2893. for (i = 1; i <= min; i++) {
  2894. if (eptr >= md->end_subject)
  2895. RRETURN(MATCH_NOMATCH);
  2896. switch (*eptr++) {
  2897. default:
  2898. RRETURN(MATCH_NOMATCH);
  2899. case 0x000d:
  2900. if (eptr < md->end_subject &&
  2901. *eptr == 0x0a)
  2902. eptr++;
  2903. break;
  2904. case 0x000a:
  2905. break;
  2906. case 0x000b:
  2907. case 0x000c:
  2908. case 0x0085:
  2909. if (md->bsr_anycrlf)
  2910. RRETURN(MATCH_NOMATCH);
  2911. break;
  2912. }
  2913. }
  2914. break;
  2915. case OP_NOT_HSPACE:
  2916. for (i = 1; i <= min; i++) {
  2917. if (eptr >= md->end_subject)
  2918. RRETURN(MATCH_NOMATCH);
  2919. switch (*eptr++) {
  2920. default:
  2921. break;
  2922. case 0x09: /* HT */
  2923. case 0x20: /* SPACE */
  2924. case 0xa0: /* NBSP */
  2925. RRETURN(MATCH_NOMATCH);
  2926. }
  2927. }
  2928. break;
  2929. case OP_HSPACE:
  2930. for (i = 1; i <= min; i++) {
  2931. if (eptr >= md->end_subject)
  2932. RRETURN(MATCH_NOMATCH);
  2933. switch (*eptr++) {
  2934. default:
  2935. RRETURN(MATCH_NOMATCH);
  2936. case 0x09: /* HT */
  2937. case 0x20: /* SPACE */
  2938. case 0xa0: /* NBSP */
  2939. break;
  2940. }
  2941. }
  2942. break;
  2943. case OP_NOT_VSPACE:
  2944. for (i = 1; i <= min; i++) {
  2945. if (eptr >= md->end_subject)
  2946. RRETURN(MATCH_NOMATCH);
  2947. switch (*eptr++) {
  2948. default:
  2949. break;
  2950. case 0x0a: /* LF */
  2951. case 0x0b: /* VT */
  2952. case 0x0c: /* FF */
  2953. case 0x0d: /* CR */
  2954. case 0x85: /* NEL */
  2955. RRETURN(MATCH_NOMATCH);
  2956. }
  2957. }
  2958. break;
  2959. case OP_VSPACE:
  2960. for (i = 1; i <= min; i++) {
  2961. if (eptr >= md->end_subject)
  2962. RRETURN(MATCH_NOMATCH);
  2963. switch (*eptr++) {
  2964. default:
  2965. RRETURN(MATCH_NOMATCH);
  2966. case 0x0a: /* LF */
  2967. case 0x0b: /* VT */
  2968. case 0x0c: /* FF */
  2969. case 0x0d: /* CR */
  2970. case 0x85: /* NEL */
  2971. break;
  2972. }
  2973. }
  2974. break;
  2975. case OP_NOT_DIGIT:
  2976. for (i = 1; i <= min; i++)
  2977. if ((md->ctypes[*eptr++] & ctype_digit) !=
  2978. 0)
  2979. RRETURN(MATCH_NOMATCH);
  2980. break;
  2981. case OP_DIGIT:
  2982. for (i = 1; i <= min; i++)
  2983. if ((md->ctypes[*eptr++] & ctype_digit) ==
  2984. 0)
  2985. RRETURN(MATCH_NOMATCH);
  2986. break;
  2987. case OP_NOT_WHITESPACE:
  2988. for (i = 1; i <= min; i++)
  2989. if ((md->ctypes[*eptr++] & ctype_space) !=
  2990. 0)
  2991. RRETURN(MATCH_NOMATCH);
  2992. break;
  2993. case OP_WHITESPACE:
  2994. for (i = 1; i <= min; i++)
  2995. if ((md->ctypes[*eptr++] & ctype_space) ==
  2996. 0)
  2997. RRETURN(MATCH_NOMATCH);
  2998. break;
  2999. case OP_NOT_WORDCHAR:
  3000. for (i = 1; i <= min; i++)
  3001. if ((md->ctypes[*eptr++] & ctype_word) != 0)
  3002. RRETURN(MATCH_NOMATCH);
  3003. break;
  3004. case OP_WORDCHAR:
  3005. for (i = 1; i <= min; i++)
  3006. if ((md->ctypes[*eptr++] & ctype_word) == 0)
  3007. RRETURN(MATCH_NOMATCH);
  3008. break;
  3009. default:
  3010. RRETURN(PCRE_ERROR_INTERNAL);
  3011. }
  3012. }
  3013. /* If min = max, continue at the same level without recursing */
  3014. if (min == max)
  3015. continue;
  3016. /* If minimizing, we have to test the rest of the pattern before
  3017. each subsequent match. Again, separate the UTF-8 case for speed,
  3018. and also separate the UCP cases. */
  3019. if (minimize) {
  3020. #ifdef SUPPORT_UCP
  3021. if (prop_type >= 0) {
  3022. switch (prop_type) {
  3023. case PT_ANY:
  3024. for (fi = min;; fi++) {
  3025. RMATCH(eptr, ecode, offset_top, md, ims,
  3026. eptrb, 0, RM36);
  3027. if (rrc != MATCH_NOMATCH)
  3028. RRETURN(rrc);
  3029. if (fi >= max || eptr >= md->end_subject)
  3030. RRETURN(MATCH_NOMATCH);
  3031. GETCHARINC(c, eptr);
  3032. if (prop_fail_result)
  3033. RRETURN(MATCH_NOMATCH);
  3034. }
  3035. /* Control never gets here */
  3036. case PT_LAMP:
  3037. for (fi = min;; fi++) {
  3038. RMATCH(eptr, ecode, offset_top, md, ims,
  3039. eptrb, 0, RM37);
  3040. if (rrc != MATCH_NOMATCH)
  3041. RRETURN(rrc);
  3042. if (fi >= max || eptr >= md->end_subject)
  3043. RRETURN(MATCH_NOMATCH);
  3044. GETCHARINC(c, eptr);
  3045. prop_category = _pcre_ucp_findprop(
  3046. c, &prop_chartype, &prop_script);
  3047. if ((prop_chartype == ucp_Lu ||
  3048. prop_chartype == ucp_Ll ||
  3049. prop_chartype == ucp_Lt) ==
  3050. prop_fail_result)
  3051. RRETURN(MATCH_NOMATCH);
  3052. }
  3053. /* Control never gets here */
  3054. case PT_GC:
  3055. for (fi = min;; fi++) {
  3056. RMATCH(eptr, ecode, offset_top, md, ims,
  3057. eptrb, 0, RM38);
  3058. if (rrc != MATCH_NOMATCH)
  3059. RRETURN(rrc);
  3060. if (fi >= max || eptr >= md->end_subject)
  3061. RRETURN(MATCH_NOMATCH);
  3062. GETCHARINC(c, eptr);
  3063. prop_category = _pcre_ucp_findprop(
  3064. c, &prop_chartype, &prop_script);
  3065. if ((prop_category == prop_value) ==
  3066. prop_fail_result)
  3067. RRETURN(MATCH_NOMATCH);
  3068. }
  3069. /* Control never gets here */
  3070. case PT_PC:
  3071. for (fi = min;; fi++) {
  3072. RMATCH(eptr, ecode, offset_top, md, ims,
  3073. eptrb, 0, RM39);
  3074. if (rrc != MATCH_NOMATCH)
  3075. RRETURN(rrc);
  3076. if (fi >= max || eptr >= md->end_subject)
  3077. RRETURN(MATCH_NOMATCH);
  3078. GETCHARINC(c, eptr);
  3079. prop_category = _pcre_ucp_findprop(
  3080. c, &prop_chartype, &prop_script);
  3081. if ((prop_chartype == prop_value) ==
  3082. prop_fail_result)
  3083. RRETURN(MATCH_NOMATCH);
  3084. }
  3085. /* Control never gets here */
  3086. case PT_SC:
  3087. for (fi = min;; fi++) {
  3088. RMATCH(eptr, ecode, offset_top, md, ims,
  3089. eptrb, 0, RM40);
  3090. if (rrc != MATCH_NOMATCH)
  3091. RRETURN(rrc);
  3092. if (fi >= max || eptr >= md->end_subject)
  3093. RRETURN(MATCH_NOMATCH);
  3094. GETCHARINC(c, eptr);
  3095. prop_category = _pcre_ucp_findprop(
  3096. c, &prop_chartype, &prop_script);
  3097. if ((prop_script == prop_value) ==
  3098. prop_fail_result)
  3099. RRETURN(MATCH_NOMATCH);
  3100. }
  3101. /* Control never gets here */
  3102. default:
  3103. RRETURN(PCRE_ERROR_INTERNAL);
  3104. }
  3105. }
  3106. /* Match extended Unicode sequences. We will get here only
  3107. if the support is in the binary; otherwise a compile-time
  3108. error occurs. */
  3109. else if (ctype == OP_EXTUNI) {
  3110. for (fi = min;; fi++) {
  3111. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  3112. RM41);
  3113. if (rrc != MATCH_NOMATCH)
  3114. RRETURN(rrc);
  3115. if (fi >= max || eptr >= md->end_subject)
  3116. RRETURN(MATCH_NOMATCH);
  3117. GETCHARINCTEST(c, eptr);
  3118. prop_category = _pcre_ucp_findprop(
  3119. c, &prop_chartype, &prop_script);
  3120. if (prop_category == ucp_M)
  3121. RRETURN(MATCH_NOMATCH);
  3122. while (eptr < md->end_subject) {
  3123. int len = 1;
  3124. if (!utf8)
  3125. c = *eptr;
  3126. else {
  3127. GETCHARLEN(c, eptr, len);
  3128. }
  3129. prop_category = _pcre_ucp_findprop(
  3130. c, &prop_chartype, &prop_script);
  3131. if (prop_category != ucp_M)
  3132. break;
  3133. eptr += len;
  3134. }
  3135. }
  3136. }
  3137. else
  3138. #endif /* SUPPORT_UCP */
  3139. #ifdef SUPPORT_UTF8
  3140. /* UTF-8 mode */
  3141. if (utf8) {
  3142. for (fi = min;; fi++) {
  3143. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  3144. 0, RM42);
  3145. if (rrc != MATCH_NOMATCH)
  3146. RRETURN(rrc);
  3147. if (fi >= max || eptr >= md->end_subject ||
  3148. (ctype == OP_ANY &&
  3149. (ims & PCRE_DOTALL) == 0 &&
  3150. IS_NEWLINE(eptr)))
  3151. RRETURN(MATCH_NOMATCH);
  3152. GETCHARINC(c, eptr);
  3153. switch (ctype) {
  3154. case OP_ANY: /* This is the DOTALL case */
  3155. break;
  3156. case OP_ANYBYTE:
  3157. break;
  3158. case OP_ANYNL:
  3159. switch (c) {
  3160. default:
  3161. RRETURN(MATCH_NOMATCH);
  3162. case 0x000d:
  3163. if (eptr < md->end_subject &&
  3164. *eptr == 0x0a)
  3165. eptr++;
  3166. break;
  3167. case 0x000a:
  3168. break;
  3169. case 0x000b:
  3170. case 0x000c:
  3171. case 0x0085:
  3172. case 0x2028:
  3173. case 0x2029:
  3174. if (md->bsr_anycrlf)
  3175. RRETURN(MATCH_NOMATCH);
  3176. break;
  3177. }
  3178. break;
  3179. case OP_NOT_HSPACE:
  3180. switch (c) {
  3181. default:
  3182. break;
  3183. case 0x09: /* HT */
  3184. case 0x20: /* SPACE */
  3185. case 0xa0: /* NBSP */
  3186. case 0x1680: /* OGHAM SPACE MARK */
  3187. case 0x180e: /* MONGOLIAN VOWEL
  3188. SEPARATOR */
  3189. case 0x2000: /* EN QUAD */
  3190. case 0x2001: /* EM QUAD */
  3191. case 0x2002: /* EN SPACE */
  3192. case 0x2003: /* EM SPACE */
  3193. case 0x2004: /* THREE-PER-EM SPACE
  3194. */
  3195. case 0x2005: /* FOUR-PER-EM SPACE */
  3196. case 0x2006: /* SIX-PER-EM SPACE */
  3197. case 0x2007: /* FIGURE SPACE */
  3198. case 0x2008: /* PUNCTUATION SPACE */
  3199. case 0x2009: /* THIN SPACE */
  3200. case 0x200A: /* HAIR SPACE */
  3201. case 0x202f: /* NARROW NO-BREAK
  3202. SPACE */
  3203. case 0x205f: /* MEDIUM MATHEMATICAL
  3204. SPACE */
  3205. case 0x3000: /* IDEOGRAPHIC SPACE */
  3206. RRETURN(MATCH_NOMATCH);
  3207. }
  3208. break;
  3209. case OP_HSPACE:
  3210. switch (c) {
  3211. default:
  3212. RRETURN(MATCH_NOMATCH);
  3213. case 0x09: /* HT */
  3214. case 0x20: /* SPACE */
  3215. case 0xa0: /* NBSP */
  3216. case 0x1680: /* OGHAM SPACE MARK */
  3217. case 0x180e: /* MONGOLIAN VOWEL
  3218. SEPARATOR */
  3219. case 0x2000: /* EN QUAD */
  3220. case 0x2001: /* EM QUAD */
  3221. case 0x2002: /* EN SPACE */
  3222. case 0x2003: /* EM SPACE */
  3223. case 0x2004: /* THREE-PER-EM SPACE
  3224. */
  3225. case 0x2005: /* FOUR-PER-EM SPACE */
  3226. case 0x2006: /* SIX-PER-EM SPACE */
  3227. case 0x2007: /* FIGURE SPACE */
  3228. case 0x2008: /* PUNCTUATION SPACE */
  3229. case 0x2009: /* THIN SPACE */
  3230. case 0x200A: /* HAIR SPACE */
  3231. case 0x202f: /* NARROW NO-BREAK
  3232. SPACE */
  3233. case 0x205f: /* MEDIUM MATHEMATICAL
  3234. SPACE */
  3235. case 0x3000: /* IDEOGRAPHIC SPACE */
  3236. break;
  3237. }
  3238. break;
  3239. case OP_NOT_VSPACE:
  3240. switch (c) {
  3241. default:
  3242. break;
  3243. case 0x0a: /* LF */
  3244. case 0x0b: /* VT */
  3245. case 0x0c: /* FF */
  3246. case 0x0d: /* CR */
  3247. case 0x85: /* NEL */
  3248. case 0x2028: /* LINE SEPARATOR */
  3249. case 0x2029: /* PARAGRAPH SEPARATOR
  3250. */
  3251. RRETURN(MATCH_NOMATCH);
  3252. }
  3253. break;
  3254. case OP_VSPACE:
  3255. switch (c) {
  3256. default:
  3257. RRETURN(MATCH_NOMATCH);
  3258. case 0x0a: /* LF */
  3259. case 0x0b: /* VT */
  3260. case 0x0c: /* FF */
  3261. case 0x0d: /* CR */
  3262. case 0x85: /* NEL */
  3263. case 0x2028: /* LINE SEPARATOR */
  3264. case 0x2029: /* PARAGRAPH SEPARATOR
  3265. */
  3266. break;
  3267. }
  3268. break;
  3269. case OP_NOT_DIGIT:
  3270. if (c < 256 &&
  3271. (md->ctypes[c] & ctype_digit) != 0)
  3272. RRETURN(MATCH_NOMATCH);
  3273. break;
  3274. case OP_DIGIT:
  3275. if (c >= 256 ||
  3276. (md->ctypes[c] & ctype_digit) == 0)
  3277. RRETURN(MATCH_NOMATCH);
  3278. break;
  3279. case OP_NOT_WHITESPACE:
  3280. if (c < 256 &&
  3281. (md->ctypes[c] & ctype_space) != 0)
  3282. RRETURN(MATCH_NOMATCH);
  3283. break;
  3284. case OP_WHITESPACE:
  3285. if (c >= 256 ||
  3286. (md->ctypes[c] & ctype_space) == 0)
  3287. RRETURN(MATCH_NOMATCH);
  3288. break;
  3289. case OP_NOT_WORDCHAR:
  3290. if (c < 256 &&
  3291. (md->ctypes[c] & ctype_word) != 0)
  3292. RRETURN(MATCH_NOMATCH);
  3293. break;
  3294. case OP_WORDCHAR:
  3295. if (c >= 256 ||
  3296. (md->ctypes[c] & ctype_word) == 0)
  3297. RRETURN(MATCH_NOMATCH);
  3298. break;
  3299. default:
  3300. RRETURN(PCRE_ERROR_INTERNAL);
  3301. }
  3302. }
  3303. } else
  3304. #endif
  3305. /* Not UTF-8 mode */
  3306. {
  3307. for (fi = min;; fi++) {
  3308. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  3309. 0, RM43);
  3310. if (rrc != MATCH_NOMATCH)
  3311. RRETURN(rrc);
  3312. if (fi >= max || eptr >= md->end_subject ||
  3313. ((ims & PCRE_DOTALL) == 0 &&
  3314. IS_NEWLINE(eptr)))
  3315. RRETURN(MATCH_NOMATCH);
  3316. c = *eptr++;
  3317. switch (ctype) {
  3318. case OP_ANY: /* This is the DOTALL case */
  3319. break;
  3320. case OP_ANYBYTE:
  3321. break;
  3322. case OP_ANYNL:
  3323. switch (c) {
  3324. default:
  3325. RRETURN(MATCH_NOMATCH);
  3326. case 0x000d:
  3327. if (eptr < md->end_subject &&
  3328. *eptr == 0x0a)
  3329. eptr++;
  3330. break;
  3331. case 0x000a:
  3332. break;
  3333. case 0x000b:
  3334. case 0x000c:
  3335. case 0x0085:
  3336. if (md->bsr_anycrlf)
  3337. RRETURN(MATCH_NOMATCH);
  3338. break;
  3339. }
  3340. break;
  3341. case OP_NOT_HSPACE:
  3342. switch (c) {
  3343. default:
  3344. break;
  3345. case 0x09: /* HT */
  3346. case 0x20: /* SPACE */
  3347. case 0xa0: /* NBSP */
  3348. RRETURN(MATCH_NOMATCH);
  3349. }
  3350. break;
  3351. case OP_HSPACE:
  3352. switch (c) {
  3353. default:
  3354. RRETURN(MATCH_NOMATCH);
  3355. case 0x09: /* HT */
  3356. case 0x20: /* SPACE */
  3357. case 0xa0: /* NBSP */
  3358. break;
  3359. }
  3360. break;
  3361. case OP_NOT_VSPACE:
  3362. switch (c) {
  3363. default:
  3364. break;
  3365. case 0x0a: /* LF */
  3366. case 0x0b: /* VT */
  3367. case 0x0c: /* FF */
  3368. case 0x0d: /* CR */
  3369. case 0x85: /* NEL */
  3370. RRETURN(MATCH_NOMATCH);
  3371. }
  3372. break;
  3373. case OP_VSPACE:
  3374. switch (c) {
  3375. default:
  3376. RRETURN(MATCH_NOMATCH);
  3377. case 0x0a: /* LF */
  3378. case 0x0b: /* VT */
  3379. case 0x0c: /* FF */
  3380. case 0x0d: /* CR */
  3381. case 0x85: /* NEL */
  3382. break;
  3383. }
  3384. break;
  3385. case OP_NOT_DIGIT:
  3386. if ((md->ctypes[c] & ctype_digit) != 0)
  3387. RRETURN(MATCH_NOMATCH);
  3388. break;
  3389. case OP_DIGIT:
  3390. if ((md->ctypes[c] & ctype_digit) == 0)
  3391. RRETURN(MATCH_NOMATCH);
  3392. break;
  3393. case OP_NOT_WHITESPACE:
  3394. if ((md->ctypes[c] & ctype_space) != 0)
  3395. RRETURN(MATCH_NOMATCH);
  3396. break;
  3397. case OP_WHITESPACE:
  3398. if ((md->ctypes[c] & ctype_space) == 0)
  3399. RRETURN(MATCH_NOMATCH);
  3400. break;
  3401. case OP_NOT_WORDCHAR:
  3402. if ((md->ctypes[c] & ctype_word) != 0)
  3403. RRETURN(MATCH_NOMATCH);
  3404. break;
  3405. case OP_WORDCHAR:
  3406. if ((md->ctypes[c] & ctype_word) == 0)
  3407. RRETURN(MATCH_NOMATCH);
  3408. break;
  3409. default:
  3410. RRETURN(PCRE_ERROR_INTERNAL);
  3411. }
  3412. }
  3413. }
  3414. /* Control never gets here */
  3415. }
  3416. /* If maximizing, it is worth using inline code for speed, doing
  3417. the type test once at the start (i.e. keep it out of the loop).
  3418. Again, keep the UTF-8 and UCP stuff separate. */
  3419. else {
  3420. pp = eptr; /* Remember where we started */
  3421. #ifdef SUPPORT_UCP
  3422. if (prop_type >= 0) {
  3423. switch (prop_type) {
  3424. case PT_ANY:
  3425. for (i = min; i < max; i++) {
  3426. int len = 1;
  3427. if (eptr >= md->end_subject)
  3428. break;
  3429. GETCHARLEN(c, eptr, len);
  3430. if (prop_fail_result)
  3431. break;
  3432. eptr += len;
  3433. }
  3434. break;
  3435. case PT_LAMP:
  3436. for (i = min; i < max; i++) {
  3437. int len = 1;
  3438. if (eptr >= md->end_subject)
  3439. break;
  3440. GETCHARLEN(c, eptr, len);
  3441. prop_category = _pcre_ucp_findprop(
  3442. c, &prop_chartype, &prop_script);
  3443. if ((prop_chartype == ucp_Lu ||
  3444. prop_chartype == ucp_Ll ||
  3445. prop_chartype == ucp_Lt) ==
  3446. prop_fail_result)
  3447. break;
  3448. eptr += len;
  3449. }
  3450. break;
  3451. case PT_GC:
  3452. for (i = min; i < max; i++) {
  3453. int len = 1;
  3454. if (eptr >= md->end_subject)
  3455. break;
  3456. GETCHARLEN(c, eptr, len);
  3457. prop_category = _pcre_ucp_findprop(
  3458. c, &prop_chartype, &prop_script);
  3459. if ((prop_category == prop_value) ==
  3460. prop_fail_result)
  3461. break;
  3462. eptr += len;
  3463. }
  3464. break;
  3465. case PT_PC:
  3466. for (i = min; i < max; i++) {
  3467. int len = 1;
  3468. if (eptr >= md->end_subject)
  3469. break;
  3470. GETCHARLEN(c, eptr, len);
  3471. prop_category = _pcre_ucp_findprop(
  3472. c, &prop_chartype, &prop_script);
  3473. if ((prop_chartype == prop_value) ==
  3474. prop_fail_result)
  3475. break;
  3476. eptr += len;
  3477. }
  3478. break;
  3479. case PT_SC:
  3480. for (i = min; i < max; i++) {
  3481. int len = 1;
  3482. if (eptr >= md->end_subject)
  3483. break;
  3484. GETCHARLEN(c, eptr, len);
  3485. prop_category = _pcre_ucp_findprop(
  3486. c, &prop_chartype, &prop_script);
  3487. if ((prop_script == prop_value) ==
  3488. prop_fail_result)
  3489. break;
  3490. eptr += len;
  3491. }
  3492. break;
  3493. }
  3494. /* eptr is now past the end of the maximum run */
  3495. if (possessive)
  3496. continue;
  3497. for (;;) {
  3498. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  3499. RM44);
  3500. if (rrc != MATCH_NOMATCH)
  3501. RRETURN(rrc);
  3502. if (eptr-- == pp)
  3503. break; /* Stop if tried at original pos */
  3504. if (utf8)
  3505. BACKCHAR(eptr);
  3506. }
  3507. }
  3508. /* Match extended Unicode sequences. We will get here only
  3509. if the support is in the binary; otherwise a compile-time
  3510. error occurs. */
  3511. else if (ctype == OP_EXTUNI) {
  3512. for (i = min; i < max; i++) {
  3513. if (eptr >= md->end_subject)
  3514. break;
  3515. GETCHARINCTEST(c, eptr);
  3516. prop_category = _pcre_ucp_findprop(
  3517. c, &prop_chartype, &prop_script);
  3518. if (prop_category == ucp_M)
  3519. break;
  3520. while (eptr < md->end_subject) {
  3521. int len = 1;
  3522. if (!utf8)
  3523. c = *eptr;
  3524. else {
  3525. GETCHARLEN(c, eptr, len);
  3526. }
  3527. prop_category = _pcre_ucp_findprop(
  3528. c, &prop_chartype, &prop_script);
  3529. if (prop_category != ucp_M)
  3530. break;
  3531. eptr += len;
  3532. }
  3533. }
  3534. /* eptr is now past the end of the maximum run */
  3535. if (possessive)
  3536. continue;
  3537. for (;;) {
  3538. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0,
  3539. RM45);
  3540. if (rrc != MATCH_NOMATCH)
  3541. RRETURN(rrc);
  3542. if (eptr-- == pp)
  3543. break; /* Stop if tried at original pos */
  3544. for (;;) /* Move back over one extended */
  3545. {
  3546. int len = 1;
  3547. if (!utf8)
  3548. c = *eptr;
  3549. else {
  3550. BACKCHAR(eptr);
  3551. GETCHARLEN(c, eptr, len);
  3552. }
  3553. prop_category = _pcre_ucp_findprop(
  3554. c, &prop_chartype, &prop_script);
  3555. if (prop_category != ucp_M)
  3556. break;
  3557. eptr--;
  3558. }
  3559. }
  3560. }
  3561. else
  3562. #endif /* SUPPORT_UCP */
  3563. #ifdef SUPPORT_UTF8
  3564. /* UTF-8 mode */
  3565. if (utf8) {
  3566. switch (ctype) {
  3567. case OP_ANY:
  3568. if (max < INT_MAX) {
  3569. if ((ims & PCRE_DOTALL) == 0) {
  3570. for (i = min; i < max; i++) {
  3571. if (eptr >= md->end_subject ||
  3572. IS_NEWLINE(eptr))
  3573. break;
  3574. eptr++;
  3575. while (eptr < md->end_subject &&
  3576. (*eptr & 0xc0) == 0x80)
  3577. eptr++;
  3578. }
  3579. } else {
  3580. for (i = min; i < max; i++) {
  3581. if (eptr >= md->end_subject)
  3582. break;
  3583. eptr++;
  3584. while (eptr < md->end_subject &&
  3585. (*eptr & 0xc0) == 0x80)
  3586. eptr++;
  3587. }
  3588. }
  3589. }
  3590. /* Handle unlimited UTF-8 repeat */
  3591. else {
  3592. if ((ims & PCRE_DOTALL) == 0) {
  3593. for (i = min; i < max; i++) {
  3594. if (eptr >= md->end_subject ||
  3595. IS_NEWLINE(eptr))
  3596. break;
  3597. eptr++;
  3598. while (eptr < md->end_subject &&
  3599. (*eptr & 0xc0) == 0x80)
  3600. eptr++;
  3601. }
  3602. } else {
  3603. eptr = md->end_subject;
  3604. }
  3605. }
  3606. break;
  3607. /* The byte case is the same as non-UTF8 */
  3608. case OP_ANYBYTE:
  3609. c = max - min;
  3610. if (c >
  3611. (unsigned int)(md->end_subject - eptr))
  3612. c = md->end_subject - eptr;
  3613. eptr += c;
  3614. break;
  3615. case OP_ANYNL:
  3616. for (i = min; i < max; i++) {
  3617. int len = 1;
  3618. if (eptr >= md->end_subject)
  3619. break;
  3620. GETCHARLEN(c, eptr, len);
  3621. if (c == 0x000d) {
  3622. if (++eptr >= md->end_subject)
  3623. break;
  3624. if (*eptr == 0x000a)
  3625. eptr++;
  3626. } else {
  3627. if (c != 0x000a &&
  3628. (md->bsr_anycrlf ||
  3629. (c != 0x000b && c != 0x000c &&
  3630. c != 0x0085 && c != 0x2028 &&
  3631. c != 0x2029)))
  3632. break;
  3633. eptr += len;
  3634. }
  3635. }
  3636. break;
  3637. case OP_NOT_HSPACE:
  3638. case OP_HSPACE:
  3639. for (i = min; i < max; i++) {
  3640. BOOL gotspace;
  3641. int len = 1;
  3642. if (eptr >= md->end_subject)
  3643. break;
  3644. GETCHARLEN(c, eptr, len);
  3645. switch (c) {
  3646. default:
  3647. gotspace = FALSE;
  3648. break;
  3649. case 0x09: /* HT */
  3650. case 0x20: /* SPACE */
  3651. case 0xa0: /* NBSP */
  3652. case 0x1680: /* OGHAM SPACE MARK */
  3653. case 0x180e: /* MONGOLIAN VOWEL
  3654. SEPARATOR */
  3655. case 0x2000: /* EN QUAD */
  3656. case 0x2001: /* EM QUAD */
  3657. case 0x2002: /* EN SPACE */
  3658. case 0x2003: /* EM SPACE */
  3659. case 0x2004: /* THREE-PER-EM SPACE
  3660. */
  3661. case 0x2005: /* FOUR-PER-EM SPACE */
  3662. case 0x2006: /* SIX-PER-EM SPACE */
  3663. case 0x2007: /* FIGURE SPACE */
  3664. case 0x2008: /* PUNCTUATION SPACE */
  3665. case 0x2009: /* THIN SPACE */
  3666. case 0x200A: /* HAIR SPACE */
  3667. case 0x202f: /* NARROW NO-BREAK
  3668. SPACE */
  3669. case 0x205f: /* MEDIUM MATHEMATICAL
  3670. SPACE */
  3671. case 0x3000: /* IDEOGRAPHIC SPACE */
  3672. gotspace = TRUE;
  3673. break;
  3674. }
  3675. if (gotspace ==
  3676. (ctype == OP_NOT_HSPACE))
  3677. break;
  3678. eptr += len;
  3679. }
  3680. break;
  3681. case OP_NOT_VSPACE:
  3682. case OP_VSPACE:
  3683. for (i = min; i < max; i++) {
  3684. BOOL gotspace;
  3685. int len = 1;
  3686. if (eptr >= md->end_subject)
  3687. break;
  3688. GETCHARLEN(c, eptr, len);
  3689. switch (c) {
  3690. default:
  3691. gotspace = FALSE;
  3692. break;
  3693. case 0x0a: /* LF */
  3694. case 0x0b: /* VT */
  3695. case 0x0c: /* FF */
  3696. case 0x0d: /* CR */
  3697. case 0x85: /* NEL */
  3698. case 0x2028: /* LINE SEPARATOR */
  3699. case 0x2029: /* PARAGRAPH SEPARATOR
  3700. */
  3701. gotspace = TRUE;
  3702. break;
  3703. }
  3704. if (gotspace ==
  3705. (ctype == OP_NOT_VSPACE))
  3706. break;
  3707. eptr += len;
  3708. }
  3709. break;
  3710. case OP_NOT_DIGIT:
  3711. for (i = min; i < max; i++) {
  3712. int len = 1;
  3713. if (eptr >= md->end_subject)
  3714. break;
  3715. GETCHARLEN(c, eptr, len);
  3716. if (c < 256 &&
  3717. (md->ctypes[c] & ctype_digit) != 0)
  3718. break;
  3719. eptr += len;
  3720. }
  3721. break;
  3722. case OP_DIGIT:
  3723. for (i = min; i < max; i++) {
  3724. int len = 1;
  3725. if (eptr >= md->end_subject)
  3726. break;
  3727. GETCHARLEN(c, eptr, len);
  3728. if (c >= 256 ||
  3729. (md->ctypes[c] & ctype_digit) == 0)
  3730. break;
  3731. eptr += len;
  3732. }
  3733. break;
  3734. case OP_NOT_WHITESPACE:
  3735. for (i = min; i < max; i++) {
  3736. int len = 1;
  3737. if (eptr >= md->end_subject)
  3738. break;
  3739. GETCHARLEN(c, eptr, len);
  3740. if (c < 256 &&
  3741. (md->ctypes[c] & ctype_space) != 0)
  3742. break;
  3743. eptr += len;
  3744. }
  3745. break;
  3746. case OP_WHITESPACE:
  3747. for (i = min; i < max; i++) {
  3748. int len = 1;
  3749. if (eptr >= md->end_subject)
  3750. break;
  3751. GETCHARLEN(c, eptr, len);
  3752. if (c >= 256 ||
  3753. (md->ctypes[c] & ctype_space) == 0)
  3754. break;
  3755. eptr += len;
  3756. }
  3757. break;
  3758. case OP_NOT_WORDCHAR:
  3759. for (i = min; i < max; i++) {
  3760. int len = 1;
  3761. if (eptr >= md->end_subject)
  3762. break;
  3763. GETCHARLEN(c, eptr, len);
  3764. if (c < 256 &&
  3765. (md->ctypes[c] & ctype_word) != 0)
  3766. break;
  3767. eptr += len;
  3768. }
  3769. break;
  3770. case OP_WORDCHAR:
  3771. for (i = min; i < max; i++) {
  3772. int len = 1;
  3773. if (eptr >= md->end_subject)
  3774. break;
  3775. GETCHARLEN(c, eptr, len);
  3776. if (c >= 256 ||
  3777. (md->ctypes[c] & ctype_word) == 0)
  3778. break;
  3779. eptr += len;
  3780. }
  3781. break;
  3782. default:
  3783. RRETURN(PCRE_ERROR_INTERNAL);
  3784. }
  3785. /* eptr is now past the end of the maximum run */
  3786. if (possessive)
  3787. continue;
  3788. for (;;) {
  3789. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  3790. 0, RM46);
  3791. if (rrc != MATCH_NOMATCH)
  3792. RRETURN(rrc);
  3793. if (eptr-- == pp)
  3794. break; /* Stop if tried at original pos */
  3795. BACKCHAR(eptr);
  3796. }
  3797. } else
  3798. #endif /* SUPPORT_UTF8 */
  3799. /* Not UTF-8 mode */
  3800. {
  3801. switch (ctype) {
  3802. case OP_ANY:
  3803. if ((ims & PCRE_DOTALL) == 0) {
  3804. for (i = min; i < max; i++) {
  3805. if (eptr >= md->end_subject ||
  3806. IS_NEWLINE(eptr))
  3807. break;
  3808. eptr++;
  3809. }
  3810. break;
  3811. }
  3812. /* For DOTALL case, fall through and treat
  3813. * as \C */
  3814. case OP_ANYBYTE:
  3815. c = max - min;
  3816. if (c >
  3817. (unsigned int)(md->end_subject - eptr))
  3818. c = md->end_subject - eptr;
  3819. eptr += c;
  3820. break;
  3821. case OP_ANYNL:
  3822. for (i = min; i < max; i++) {
  3823. if (eptr >= md->end_subject)
  3824. break;
  3825. c = *eptr;
  3826. if (c == 0x000d) {
  3827. if (++eptr >= md->end_subject)
  3828. break;
  3829. if (*eptr == 0x000a)
  3830. eptr++;
  3831. } else {
  3832. if (c != 0x000a &&
  3833. (md->bsr_anycrlf ||
  3834. (c != 0x000b && c != 0x000c &&
  3835. c != 0x0085)))
  3836. break;
  3837. eptr++;
  3838. }
  3839. }
  3840. break;
  3841. case OP_NOT_HSPACE:
  3842. for (i = min; i < max; i++) {
  3843. if (eptr >= md->end_subject)
  3844. break;
  3845. c = *eptr;
  3846. if (c == 0x09 || c == 0x20 || c == 0xa0)
  3847. break;
  3848. eptr++;
  3849. }
  3850. break;
  3851. case OP_HSPACE:
  3852. for (i = min; i < max; i++) {
  3853. if (eptr >= md->end_subject)
  3854. break;
  3855. c = *eptr;
  3856. if (c != 0x09 && c != 0x20 && c != 0xa0)
  3857. break;
  3858. eptr++;
  3859. }
  3860. break;
  3861. case OP_NOT_VSPACE:
  3862. for (i = min; i < max; i++) {
  3863. if (eptr >= md->end_subject)
  3864. break;
  3865. c = *eptr;
  3866. if (c == 0x0a || c == 0x0b ||
  3867. c == 0x0c || c == 0x0d || c == 0x85)
  3868. break;
  3869. eptr++;
  3870. }
  3871. break;
  3872. case OP_VSPACE:
  3873. for (i = min; i < max; i++) {
  3874. if (eptr >= md->end_subject)
  3875. break;
  3876. c = *eptr;
  3877. if (c != 0x0a && c != 0x0b &&
  3878. c != 0x0c && c != 0x0d && c != 0x85)
  3879. break;
  3880. eptr++;
  3881. }
  3882. break;
  3883. case OP_NOT_DIGIT:
  3884. for (i = min; i < max; i++) {
  3885. if (eptr >= md->end_subject ||
  3886. (md->ctypes[*eptr] & ctype_digit) !=
  3887. 0)
  3888. break;
  3889. eptr++;
  3890. }
  3891. break;
  3892. case OP_DIGIT:
  3893. for (i = min; i < max; i++) {
  3894. if (eptr >= md->end_subject ||
  3895. (md->ctypes[*eptr] & ctype_digit) ==
  3896. 0)
  3897. break;
  3898. eptr++;
  3899. }
  3900. break;
  3901. case OP_NOT_WHITESPACE:
  3902. for (i = min; i < max; i++) {
  3903. if (eptr >= md->end_subject ||
  3904. (md->ctypes[*eptr] & ctype_space) !=
  3905. 0)
  3906. break;
  3907. eptr++;
  3908. }
  3909. break;
  3910. case OP_WHITESPACE:
  3911. for (i = min; i < max; i++) {
  3912. if (eptr >= md->end_subject ||
  3913. (md->ctypes[*eptr] & ctype_space) ==
  3914. 0)
  3915. break;
  3916. eptr++;
  3917. }
  3918. break;
  3919. case OP_NOT_WORDCHAR:
  3920. for (i = min; i < max; i++) {
  3921. if (eptr >= md->end_subject ||
  3922. (md->ctypes[*eptr] & ctype_word) !=
  3923. 0)
  3924. break;
  3925. eptr++;
  3926. }
  3927. break;
  3928. case OP_WORDCHAR:
  3929. for (i = min; i < max; i++) {
  3930. if (eptr >= md->end_subject ||
  3931. (md->ctypes[*eptr] & ctype_word) ==
  3932. 0)
  3933. break;
  3934. eptr++;
  3935. }
  3936. break;
  3937. default:
  3938. RRETURN(PCRE_ERROR_INTERNAL);
  3939. }
  3940. /* eptr is now past the end of the maximum run */
  3941. if (possessive)
  3942. continue;
  3943. while (eptr >= pp) {
  3944. RMATCH(eptr, ecode, offset_top, md, ims, eptrb,
  3945. 0, RM47);
  3946. eptr--;
  3947. if (rrc != MATCH_NOMATCH)
  3948. RRETURN(rrc);
  3949. }
  3950. }
  3951. /* Get here if we can't make it match with any permitted
  3952. * repetitions */
  3953. RRETURN(MATCH_NOMATCH);
  3954. }
  3955. /* Control never gets here */
  3956. /* There's been some horrible disaster. Arrival here can only
  3957. mean there is something seriously wrong in the code above or the
  3958. OP_xxx definitions. */
  3959. default:
  3960. DPRINTF(("Unknown opcode %d\n", *ecode));
  3961. RRETURN(PCRE_ERROR_UNKNOWN_OPCODE);
  3962. }
  3963. /* Do not stick any code in here without much thought; it is assumed
  3964. that "continue" in the code above comes out to here to repeat the main
  3965. loop. */
  3966. } /* End of main loop */
  3967. /* Control never reaches here */
  3968. /* When compiling to use the heap rather than the stack for recursive calls
  3969. to match(), the RRETURN() macro jumps here. The number that is saved in
  3970. frame->Xwhere indicates which label we actually want to return to. */
  3971. #ifdef NO_RECURSE
  3972. #define LBL(val) \
  3973. case val: \
  3974. goto L_RM##val;
  3975. HEAP_RETURN:
  3976. switch (frame->Xwhere) {
  3977. LBL(1)
  3978. LBL(2)
  3979. LBL(3)
  3980. LBL(4)
  3981. LBL(5)
  3982. LBL(6)
  3983. LBL(7)
  3984. LBL(8)
  3985. LBL(9)
  3986. LBL(10)
  3987. LBL(11)
  3988. LBL(12)
  3989. LBL(13)
  3990. LBL(14) LBL(15) LBL(17) LBL(19) LBL(24) LBL(25) LBL(26) LBL(27) LBL(29)
  3991. LBL(31) LBL(33) LBL(35) LBL(43) LBL(47) LBL(48) LBL(49) LBL(50)
  3992. LBL(51) LBL(52) LBL(53) LBL(54)
  3993. #ifdef SUPPORT_UTF8
  3994. LBL(16) LBL(18) LBL(20) LBL(21) LBL(22) LBL(23) LBL(28)
  3995. LBL(30) LBL(32) LBL(34) LBL(42) LBL(46)
  3996. #ifdef SUPPORT_UCP
  3997. LBL(36) LBL(37) LBL(38) LBL(39) LBL(40) LBL(41)
  3998. LBL(44) LBL(45)
  3999. #endif /* SUPPORT_UCP */
  4000. #endif /* SUPPORT_UTF8 */
  4001. default
  4002. : DPRINTF(("jump error in pcre match: label %d non-existent\n",
  4003. frame->Xwhere));
  4004. return PCRE_ERROR_INTERNAL;
  4005. }
  4006. #undef LBL
  4007. #endif /* NO_RECURSE */
  4008. }
  4009. /***************************************************************************
  4010. ****************************************************************************
  4011. RECURSION IN THE match() FUNCTION
  4012. Undefine all the macros that were defined above to handle this. */
  4013. #ifdef NO_RECURSE
  4014. #undef eptr
  4015. #undef ecode
  4016. #undef mstart
  4017. #undef offset_top
  4018. #undef ims
  4019. #undef eptrb
  4020. #undef flags
  4021. #undef callpat
  4022. #undef charptr
  4023. #undef data
  4024. #undef next
  4025. #undef pp
  4026. #undef prev
  4027. #undef saved_eptr
  4028. #undef new_recursive
  4029. #undef cur_is_word
  4030. #undef condition
  4031. #undef prev_is_word
  4032. #undef original_ims
  4033. #undef ctype
  4034. #undef length
  4035. #undef max
  4036. #undef min
  4037. #undef number
  4038. #undef offset
  4039. #undef op
  4040. #undef save_capture_last
  4041. #undef save_offset1
  4042. #undef save_offset2
  4043. #undef save_offset3
  4044. #undef stacksave
  4045. #undef newptrb
  4046. #endif
  4047. /* These two are defined as macros in both cases */
  4048. #undef fc
  4049. #undef fi
  4050. /***************************************************************************
  4051. ***************************************************************************/
  4052. /*************************************************
  4053. * Execute a Regular Expression *
  4054. *************************************************/
  4055. /* This function applies a compiled re to a subject string and picks out
  4056. portions of the string if it matches. Two elements in the vector are set for
  4057. each substring: the offsets to the start and end of the substring.
  4058. Arguments:
  4059. argument_re points to the compiled expression
  4060. extra_data points to extra data or is NULL
  4061. subject points to the subject string
  4062. length length of subject string (may contain binary zeros)
  4063. start_offset where to start in the subject string
  4064. options option bits
  4065. offsets points to a vector of ints to be filled in with offsets
  4066. offsetcount the number of elements in the vector
  4067. Returns: > 0 => success; value is the number of elements filled in
  4068. = 0 => success, but offsets is not big enough
  4069. -1 => failed to match
  4070. < -1 => some kind of unexpected problem
  4071. */
  4072. int pcre_exec(const pcre* argument_re,
  4073. const pcre_extra* extra_data,
  4074. PCRE_SPTR subject,
  4075. int length,
  4076. int start_offset,
  4077. int options,
  4078. int* offsets,
  4079. int offsetcount) {
  4080. int rc, resetcount, ocount;
  4081. int first_byte = -1;
  4082. int req_byte = -1;
  4083. int req_byte2 = -1;
  4084. int newline;
  4085. unsigned long int ims;
  4086. BOOL using_temporary_offsets = FALSE;
  4087. BOOL anchored;
  4088. BOOL startline;
  4089. BOOL firstline;
  4090. BOOL first_byte_caseless = FALSE;
  4091. BOOL req_byte_caseless = FALSE;
  4092. BOOL utf8;
  4093. match_data match_block;
  4094. match_data* md = &match_block;
  4095. const uschar* tables;
  4096. const uschar* start_bits = NULL;
  4097. USPTR start_match = (USPTR)subject + start_offset;
  4098. USPTR end_subject;
  4099. USPTR req_byte_ptr = start_match - 1;
  4100. pcre_study_data internal_study;
  4101. const pcre_study_data* study;
  4102. real_pcre internal_re;
  4103. const real_pcre* external_re = (const real_pcre*)argument_re;
  4104. const real_pcre* re = external_re;
  4105. /* Plausibility checks */
  4106. if ((options & ~PUBLIC_EXEC_OPTIONS) != 0)
  4107. return PCRE_ERROR_BADOPTION;
  4108. if (re == NULL || subject == NULL || (offsets == NULL && offsetcount > 0))
  4109. return PCRE_ERROR_NULL;
  4110. if (offsetcount < 0)
  4111. return PCRE_ERROR_BADCOUNT;
  4112. /* Fish out the optional data from the extra_data structure, first setting
  4113. the default values. */
  4114. study = NULL;
  4115. md->match_limit = MATCH_LIMIT;
  4116. md->match_limit_recursion = MATCH_LIMIT_RECURSION;
  4117. md->callout_data = NULL;
  4118. /* The table pointer is always in native byte order. */
  4119. tables = external_re->tables;
  4120. if (extra_data != NULL) {
  4121. register unsigned int flags = extra_data->flags;
  4122. if ((flags & PCRE_EXTRA_STUDY_DATA) != 0)
  4123. study = (const pcre_study_data*)extra_data->study_data;
  4124. if ((flags & PCRE_EXTRA_MATCH_LIMIT) != 0)
  4125. md->match_limit = extra_data->match_limit;
  4126. if ((flags & PCRE_EXTRA_MATCH_LIMIT_RECURSION) != 0)
  4127. md->match_limit_recursion = extra_data->match_limit_recursion;
  4128. if ((flags & PCRE_EXTRA_CALLOUT_DATA) != 0)
  4129. md->callout_data = extra_data->callout_data;
  4130. if ((flags & PCRE_EXTRA_TABLES) != 0)
  4131. tables = extra_data->tables;
  4132. }
  4133. /* If the exec call supplied NULL for tables, use the inbuilt ones. This
  4134. is a feature that makes it possible to save compiled regex and re-use them
  4135. in other programs later. */
  4136. if (tables == NULL)
  4137. tables = _pcre_default_tables;
  4138. /* Check that the first field in the block is the magic number. If it is
  4139. not, test for a regex that was compiled on a host of opposite endianness. If
  4140. this is the case, flipped values are put in internal_re and internal_study
  4141. if there was study data too. */
  4142. if (re->magic_number != MAGIC_NUMBER) {
  4143. re = _pcre_try_flipped(re, &internal_re, study, &internal_study);
  4144. if (re == NULL)
  4145. return PCRE_ERROR_BADMAGIC;
  4146. if (study != NULL)
  4147. study = &internal_study;
  4148. }
  4149. /* Set up other data */
  4150. anchored = ((re->options | options) & PCRE_ANCHORED) != 0;
  4151. startline = (re->flags & PCRE_STARTLINE) != 0;
  4152. firstline = (re->options & PCRE_FIRSTLINE) != 0;
  4153. /* The code starts after the real_pcre block and the capture name table. */
  4154. md->start_code = (const uschar*)external_re + re->name_table_offset +
  4155. re->name_count * re->name_entry_size;
  4156. md->start_subject = (USPTR)subject;
  4157. md->start_offset = start_offset;
  4158. md->end_subject = md->start_subject + length;
  4159. end_subject = md->end_subject;
  4160. md->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;
  4161. utf8 = md->utf8 = (re->options & PCRE_UTF8) != 0;
  4162. md->notbol = (options & PCRE_NOTBOL) != 0;
  4163. md->noteol = (options & PCRE_NOTEOL) != 0;
  4164. md->notempty = (options & PCRE_NOTEMPTY) != 0;
  4165. md->partial = (options & PCRE_PARTIAL) != 0;
  4166. md->hitend = FALSE;
  4167. md->recursive = NULL; /* No recursion at top level */
  4168. md->lcc = tables + lcc_offset;
  4169. md->ctypes = tables + ctypes_offset;
  4170. /* Handle different \R options. */
  4171. switch (options & (PCRE_BSR_ANYCRLF | PCRE_BSR_UNICODE)) {
  4172. case 0:
  4173. if ((re->options & (PCRE_BSR_ANYCRLF | PCRE_BSR_UNICODE)) != 0)
  4174. md->bsr_anycrlf = (re->options & PCRE_BSR_ANYCRLF) != 0;
  4175. else
  4176. #ifdef BSR_ANYCRLF
  4177. md->bsr_anycrlf = TRUE;
  4178. #else
  4179. md->bsr_anycrlf = FALSE;
  4180. #endif
  4181. break;
  4182. case PCRE_BSR_ANYCRLF:
  4183. md->bsr_anycrlf = TRUE;
  4184. break;
  4185. case PCRE_BSR_UNICODE:
  4186. md->bsr_anycrlf = FALSE;
  4187. break;
  4188. default:
  4189. return PCRE_ERROR_BADNEWLINE;
  4190. }
  4191. /* Handle different types of newline. The three bits give eight cases. If
  4192. nothing is set at run time, whatever was used at compile time applies. */
  4193. switch ((((options & PCRE_NEWLINE_BITS) == 0) ? re->options
  4194. : (pcre_uint32)options) &
  4195. PCRE_NEWLINE_BITS) {
  4196. case 0:
  4197. newline = NEWLINE;
  4198. break; /* Compile-time default */
  4199. case PCRE_NEWLINE_CR:
  4200. newline = '\r';
  4201. break;
  4202. case PCRE_NEWLINE_LF:
  4203. newline = '\n';
  4204. break;
  4205. case PCRE_NEWLINE_CR + PCRE_NEWLINE_LF:
  4206. newline = ('\r' << 8) | '\n';
  4207. break;
  4208. case PCRE_NEWLINE_ANY:
  4209. newline = -1;
  4210. break;
  4211. case PCRE_NEWLINE_ANYCRLF:
  4212. newline = -2;
  4213. break;
  4214. default:
  4215. return PCRE_ERROR_BADNEWLINE;
  4216. }
  4217. if (newline == -2) {
  4218. md->nltype = NLTYPE_ANYCRLF;
  4219. } else if (newline < 0) {
  4220. md->nltype = NLTYPE_ANY;
  4221. } else {
  4222. md->nltype = NLTYPE_FIXED;
  4223. if (newline > 255) {
  4224. md->nllen = 2;
  4225. md->nl[0] = (newline >> 8) & 255;
  4226. md->nl[1] = newline & 255;
  4227. } else {
  4228. md->nllen = 1;
  4229. md->nl[0] = newline;
  4230. }
  4231. }
  4232. /* Partial matching is supported only for a restricted set of regexes at the
  4233. moment. */
  4234. if (md->partial && (re->flags & PCRE_NOPARTIAL) != 0)
  4235. return PCRE_ERROR_BADPARTIAL;
  4236. /* Check a UTF-8 string if required. Unfortunately there's no way of
  4237. passing back the character offset. */
  4238. #ifdef SUPPORT_UTF8
  4239. if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0) {
  4240. if (_pcre_valid_utf8((uschar*)subject, length) >= 0)
  4241. return PCRE_ERROR_BADUTF8;
  4242. if (start_offset > 0 && start_offset < length) {
  4243. int tb = ((uschar*)subject)[start_offset];
  4244. if (tb > 127) {
  4245. tb &= 0xc0;
  4246. if (tb != 0 && tb != 0xc0)
  4247. return PCRE_ERROR_BADUTF8_OFFSET;
  4248. }
  4249. }
  4250. }
  4251. #endif
  4252. /* The ims options can vary during the matching as a result of the presence
  4253. of (?ims) items in the pattern. They are kept in a local variable so that
  4254. restoring at the exit of a group is easy. */
  4255. ims = re->options & (PCRE_CASELESS | PCRE_MULTILINE | PCRE_DOTALL);
  4256. /* If the expression has got more back references than the offsets supplied
  4257. can hold, we get a temporary chunk of working store to use during the
  4258. matching. Otherwise, we can use the vector supplied, rounding down its size
  4259. to a multiple of 3. */
  4260. ocount = offsetcount - (offsetcount % 3);
  4261. if (re->top_backref > 0 && re->top_backref >= ocount / 3) {
  4262. ocount = re->top_backref * 3 + 3;
  4263. md->offset_vector = (int*)(pcre_malloc)(ocount * sizeof(int));
  4264. if (md->offset_vector == NULL)
  4265. return PCRE_ERROR_NOMEMORY;
  4266. using_temporary_offsets = TRUE;
  4267. DPRINTF(("Got memory to hold back references\n"));
  4268. } else
  4269. md->offset_vector = offsets;
  4270. md->offset_end = ocount;
  4271. md->offset_max = (2 * ocount) / 3;
  4272. md->offset_overflow = FALSE;
  4273. md->capture_last = -1;
  4274. /* Compute the minimum number of offsets that we need to reset each time.
  4275. Doing this makes a huge difference to execution time when there aren't many
  4276. brackets in the pattern. */
  4277. resetcount = 2 + re->top_bracket * 2;
  4278. if (resetcount > offsetcount)
  4279. resetcount = ocount;
  4280. /* Reset the working variable associated with each extraction. These should
  4281. never be used unless previously set, but they get saved and restored, and so
  4282. we initialize them to avoid reading uninitialized locations. */
  4283. if (md->offset_vector != NULL) {
  4284. register int* iptr = md->offset_vector + ocount;
  4285. register int* iend = iptr - resetcount / 2 + 1;
  4286. while (--iptr >= iend)
  4287. *iptr = -1;
  4288. }
  4289. /* Set up the first character to match, if available. The first_byte value
  4290. is never set for an anchored regular expression, but the anchoring may be
  4291. forced at run time, so we have to test for anchoring. The first char may be
  4292. unset for an unanchored pattern, of course. If there's no first char and the
  4293. pattern was studied, there may be a bitmap of possible first characters. */
  4294. if (!anchored) {
  4295. if ((re->flags & PCRE_FIRSTSET) != 0) {
  4296. first_byte = re->first_byte & 255;
  4297. if ((first_byte_caseless =
  4298. ((re->first_byte & REQ_CASELESS) != 0)) == TRUE)
  4299. first_byte = md->lcc[first_byte];
  4300. } else if (!startline && study != NULL &&
  4301. (study->options & PCRE_STUDY_MAPPED) != 0)
  4302. start_bits = study->start_bits;
  4303. }
  4304. /* For anchored or unanchored matches, there may be a "last known required
  4305. character" set. */
  4306. if ((re->flags & PCRE_REQCHSET) != 0) {
  4307. req_byte = re->req_byte & 255;
  4308. req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0;
  4309. req_byte2 = (tables + fcc_offset)[req_byte]; /* case flipped */
  4310. }
  4311. /* ==========================================================================*/
  4312. /* Loop for handling unanchored repeated matching attempts; for anchored
  4313. regexs the loop runs just once. */
  4314. for (;;) {
  4315. USPTR save_end_subject = end_subject;
  4316. USPTR new_start_match;
  4317. /* Reset the maximum number of extractions we might see. */
  4318. if (md->offset_vector != NULL) {
  4319. register int* iptr = md->offset_vector;
  4320. register int* iend = iptr + resetcount;
  4321. while (iptr < iend)
  4322. *iptr++ = -1;
  4323. }
  4324. /* Advance to a unique first char if possible. If firstline is TRUE, the
  4325. start of the match is constrained to the first line of a multiline
  4326. string. That is, the match must be before or at the first newline.
  4327. Implement this by temporarily adjusting end_subject so that we stop
  4328. scanning at a newline. If the match fails at the newline, later code
  4329. breaks this loop. */
  4330. if (firstline) {
  4331. USPTR t = start_match;
  4332. while (t < md->end_subject && !IS_NEWLINE(t))
  4333. t++;
  4334. end_subject = t;
  4335. }
  4336. /* Now test for a unique first byte */
  4337. if (first_byte >= 0) {
  4338. if (first_byte_caseless)
  4339. while (start_match < end_subject &&
  4340. md->lcc[*start_match] != first_byte) {
  4341. NEXTCHAR(start_match);
  4342. }
  4343. else
  4344. while (start_match < end_subject &&
  4345. *start_match != first_byte) {
  4346. NEXTCHAR(start_match);
  4347. }
  4348. }
  4349. /* Or to just after a linebreak for a multiline match if possible */
  4350. else if (startline) {
  4351. if (start_match > md->start_subject + start_offset) {
  4352. while (start_match <= end_subject &&
  4353. !WAS_NEWLINE(start_match)) {
  4354. NEXTCHAR(start_match);
  4355. }
  4356. /* If we have just passed a CR and the newline option is ANY or
  4357. ANYCRLF, and we are now at a LF, advance the match position by
  4358. one more character.
  4359. */
  4360. if (start_match[-1] == '\r' &&
  4361. (md->nltype == NLTYPE_ANY ||
  4362. md->nltype == NLTYPE_ANYCRLF) &&
  4363. start_match < end_subject && *start_match == '\n')
  4364. start_match++;
  4365. }
  4366. }
  4367. /* Or to a non-unique first char after study */
  4368. else if (start_bits != NULL) {
  4369. while (start_match < end_subject) {
  4370. register unsigned int c = *start_match;
  4371. if ((start_bits[c / 8] & (1 << (c & 7))) == 0) {
  4372. NEXTCHAR(start_match);
  4373. } else
  4374. break;
  4375. }
  4376. }
  4377. /* Restore fudged end_subject */
  4378. end_subject = save_end_subject;
  4379. #ifdef DEBUG /* Sigh. Some compilers never learn. */
  4380. printf(">>>> Match against: ");
  4381. pchars(start_match, end_subject - start_match, TRUE, md);
  4382. printf("\n");
  4383. #endif
  4384. /* If req_byte is set, we know that that character must appear in the
  4385. subject for the match to succeed. If the first character is set,
  4386. req_byte must be later in the subject; otherwise the test starts at the
  4387. match point. This optimization can save a huge amount of backtracking in
  4388. patterns with nested unlimited repeats that aren't going to match.
  4389. Writing separate code for cased/caseless versions makes it go faster, as
  4390. does using an autoincrement and backing off on a match.
  4391. HOWEVER: when the subject string is very, very long, searching to its
  4392. end can take a long time, and give bad performance on quite ordinary
  4393. patterns. This showed up when somebody was matching something like
  4394. /^\d+C/ on a 32-megabyte string... so we don't do this when the string
  4395. is sufficiently long.
  4396. ALSO: this processing is disabled when partial matching is requested.
  4397. */
  4398. if (req_byte >= 0 && end_subject - start_match < REQ_BYTE_MAX &&
  4399. !md->partial) {
  4400. register USPTR p = start_match + ((first_byte >= 0) ? 1 : 0);
  4401. /* We don't need to repeat the search if we haven't yet reached the
  4402. place we found it at last time. */
  4403. if (p > req_byte_ptr) {
  4404. if (req_byte_caseless) {
  4405. while (p < end_subject) {
  4406. register int pp = *p++;
  4407. if (pp == req_byte || pp == req_byte2) {
  4408. p--;
  4409. break;
  4410. }
  4411. }
  4412. } else {
  4413. while (p < end_subject) {
  4414. if (*p++ == req_byte) {
  4415. p--;
  4416. break;
  4417. }
  4418. }
  4419. }
  4420. /* If we can't find the required character, break the matching
  4421. loop, forcing a match failure. */
  4422. if (p >= end_subject) {
  4423. rc = MATCH_NOMATCH;
  4424. break;
  4425. }
  4426. /* If we have found the required character, save the point where
  4427. we found it, so that we don't search again next time round the
  4428. loop if the start hasn't passed this character yet. */
  4429. req_byte_ptr = p;
  4430. }
  4431. }
  4432. /* OK, we can now run the match. */
  4433. md->start_match_ptr = start_match;
  4434. md->match_call_count = 0;
  4435. rc = match(start_match, md->start_code, start_match, 2, md, ims, NULL,
  4436. 0, 0);
  4437. switch (rc) {
  4438. /* NOMATCH and PRUNE advance by one character. THEN at this
  4439. level acts exactly like PRUNE. */
  4440. case MATCH_NOMATCH:
  4441. case MATCH_PRUNE:
  4442. case MATCH_THEN:
  4443. new_start_match = start_match + 1;
  4444. #ifdef SUPPORT_UTF8
  4445. if (utf8)
  4446. while (new_start_match < end_subject &&
  4447. (*new_start_match & 0xc0) == 0x80)
  4448. new_start_match++;
  4449. #endif
  4450. break;
  4451. /* SKIP passes back the next starting point explicitly. */
  4452. case MATCH_SKIP:
  4453. new_start_match = md->start_match_ptr;
  4454. break;
  4455. /* COMMIT disables the bumpalong, but otherwise behaves as
  4456. * NOMATCH. */
  4457. case MATCH_COMMIT:
  4458. rc = MATCH_NOMATCH;
  4459. goto ENDLOOP;
  4460. /* Any other return is some kind of error. */
  4461. default:
  4462. goto ENDLOOP;
  4463. }
  4464. /* Control reaches here for the various types of "no match at this
  4465. point" result. Reset the code to MATCH_NOMATCH for subsequent checking.
  4466. */
  4467. rc = MATCH_NOMATCH;
  4468. /* If PCRE_FIRSTLINE is set, the match must happen before or at the
  4469. first newline in the subject (though it may continue over the newline).
  4470. Therefore, if we have just failed to match, starting at a newline, do
  4471. not continue. */
  4472. if (firstline && IS_NEWLINE(start_match))
  4473. break;
  4474. /* Advance to new matching position */
  4475. start_match = new_start_match;
  4476. /* Break the loop if the pattern is anchored or if we have passed the
  4477. end of the subject. */
  4478. if (anchored || start_match > end_subject)
  4479. break;
  4480. /* If we have just passed a CR and we are now at a LF, and the pattern
  4481. does not contain any explicit matches for \r or \n, and the newline
  4482. option is CRLF or ANY or ANYCRLF, advance the match position by one more
  4483. character. */
  4484. if (start_match[-1] == '\r' && start_match < end_subject &&
  4485. *start_match == '\n' && (re->flags & PCRE_HASCRORLF) == 0 &&
  4486. (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF ||
  4487. md->nllen == 2))
  4488. start_match++;
  4489. } /* End of for(;;) "bumpalong" loop */
  4490. /* ==========================================================================*/
  4491. /* We reach here when rc is not MATCH_NOMATCH, or if one of the stopping
  4492. conditions is true:
  4493. (1) The pattern is anchored or the match was failed by (*COMMIT);
  4494. (2) We are past the end of the subject;
  4495. (3) PCRE_FIRSTLINE is set and we have failed to match at a newline, because
  4496. this option requests that a match occur at or before the first newline
  4497. in the subject.
  4498. When we have a match and the offset vector is big enough to deal with any
  4499. backreferences, captured substring offsets will already be set up. In the
  4500. case where we had to get some local store to hold offsets for backreference
  4501. processing, copy those that we can. In this case there need not be overflow
  4502. if certain parts of the pattern were not used, even though there are more
  4503. capturing parentheses than vector slots. */
  4504. ENDLOOP:
  4505. if (rc == MATCH_MATCH) {
  4506. if (using_temporary_offsets) {
  4507. if (offsetcount >= 4) {
  4508. memcpy(offsets + 2, md->offset_vector + 2,
  4509. (offsetcount - 2) * sizeof(int));
  4510. DPRINTF(("Copied offsets from temporary memory\n"));
  4511. }
  4512. if (md->end_offset_top > offsetcount)
  4513. md->offset_overflow = TRUE;
  4514. DPRINTF(("Freeing temporary memory\n"));
  4515. (pcre_free)(md->offset_vector);
  4516. }
  4517. /* Set the return code to the number of captured strings, or 0 if there
  4518. are too many to fit into the vector. */
  4519. rc = md->offset_overflow ? 0 : md->end_offset_top / 2;
  4520. /* If there is space, set up the whole thing as substring 0. The value
  4521. of md->start_match_ptr might be modified if \K was encountered on the
  4522. success matching path. */
  4523. if (offsetcount < 2)
  4524. rc = 0;
  4525. else {
  4526. offsets[0] = md->start_match_ptr - md->start_subject;
  4527. offsets[1] = md->end_match_ptr - md->start_subject;
  4528. }
  4529. DPRINTF((">>>> returning %d\n", rc));
  4530. return rc;
  4531. }
  4532. /* Control gets here if there has been an error, or if the overall match
  4533. attempt has failed at all permitted starting positions. */
  4534. if (using_temporary_offsets) {
  4535. DPRINTF(("Freeing temporary memory\n"));
  4536. (pcre_free)(md->offset_vector);
  4537. }
  4538. if (rc != MATCH_NOMATCH) {
  4539. DPRINTF((">>>> error: returning %d\n", rc));
  4540. return rc;
  4541. } else if (md->partial && md->hitend) {
  4542. DPRINTF((">>>> returning PCRE_ERROR_PARTIAL\n"));
  4543. return PCRE_ERROR_PARTIAL;
  4544. } else {
  4545. DPRINTF((">>>> returning PCRE_ERROR_NOMATCH\n"));
  4546. return PCRE_ERROR_NOMATCH;
  4547. }
  4548. }
  4549. /* End of pcre_exec.c */